bit-fields

Where can I find a reference for what every bit of the CorFlags value means?

一笑奈何 提交于 2019-12-04 17:49:55
问题 I'm messing around with some rather low level things and trying to determine why I get different outputs with the CorFlags.exe utility. For reference, the outputs are as so: $ corflags test2.exe Microsoft (R) .NET Framework CorFlags Conversion Tool. Version 4.0.30319.17929 Copyright (c) Microsoft Corporation. All rights reserved. Version : v4.0.30319 CLR Header: 2.5 PE : PE32 CorFlags : 0x1 ILONLY : 1 32BITREQ : 0 32BITPREF : 0 Signed : 0 $ corflags test.exe Microsoft (R) .NET Framework

Is it guaranteed that the padding bits of “zeroed” structure will be zeroed in C?

独自空忆成欢 提交于 2019-12-04 17:33:31
This statement in the article made me embarrassed: C permits an implementation to insert padding into structures (but not into arrays) to ensure that all fields have a useful alignment for the target. If you zero a structure and then set some of the fields, will the padding bits all be zero? According to the results of the survey, 36 percent were sure that they would be, and 29 percent didn't know. Depending on the compiler (and optimization level), it may or may not be . It was not completely clear, so I turned to the standard. The ISO/IEC 9899 in §6.2.6.1 states: When a value is stored in an

C bitfields memory usage

老子叫甜甜 提交于 2019-12-04 10:00:14
I need to deal with some data in the following form: typedef struct{ unsigned n1 : 12; unsigned n2 : 12; unsigned n3 : 12; unsigned n4 : 1; unsigned n5 : 35; } data; I made sure that in total they count up to 9 bytes. But they don't.. Writing 9 bytes of that struct to a file and reading it back doesn't restore all the data, and sizeof(data) returns 16. What's the problem here ? Himadri Choudhury The problem is some padding is being added by the compiler for efficiency reasons. This behavior can be overridden. For how to do this with gcc see forcing alignment in GCC For how to do this with

What are the pros and cons of using a flags enum?

旧街凉风 提交于 2019-12-04 04:59:37
I'm receiving several bit fields from hardware. My code was originally: public readonly byte LowByte; public bool Timer { get { return (LowByte & 1) == 1; } } Then I remembered the flags enum and am considering changing it to: [Flags] public enum LowByteReasonValues : byte { Timer = 1, DistanceTravelledExceeded = 2, Polled = 4, GeofenceEvent = 8, PanicSwitchActivated = 16, ExternalInputEvent = 32, JourneyStart = 64, JourneyStop = 128 } public readonly LowByteReasonValues LowByte; public bool Timer { get { return (LowByte & LowByteReasonValues.Timer) == LowByteReasonValues.Timer; } } and so on.

What's the correct way of using bitfields in C?

孤街浪徒 提交于 2019-12-04 04:46:55
I am using bitfields to get easy access on a float library I am trying to make for a microcontroller with no FPU. The problem is that I can't seem to make it work with bitfields. Take a look: typedef struct { union{ unsigned long mantissa: 23; unsigned long exponent: 8; unsigned long sign: 1; float all; }; }_float __attribute__((__packed__)); The problem is that when I try to access or change anything it considers the bitfields as 1,8,23 bits from the end respectively. While it should be 23 bits from the end, then 8 bits and then the last bit. Unless I have totally misunderstood the use of

Maximum size of a bit field in C or C++? [duplicate]

自作多情 提交于 2019-12-04 03:08:47
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: struct bitfield max size (C99, C++) Is there a limit to the number of bits that I can specify in a bit field in C or C++? For example, could I do this: struct HugeInt { int myInt: 1000; }; I'm asking about both C and C++, since I know that the language specs sometimes differ and wanted to see if the above example was guaranteed to work / not work in C or C++. 回答1: In C11, section 6.7.2.1, clause 4: The

A bug in GCC implementation of bit-fields

隐身守侯 提交于 2019-12-04 03:02:45
Working in C11, the following struct: struct S { unsigned a : 4; _Bool b : 1; }; Gets layed out by GCC as an unsigned (4 bytes) of which 4 bits are used, followed by a _Bool (4 bytes) of which 1 bit is used, for a total size of 8 bytes. Note that C99 and C11 specifically permit _Bool as a bit-field member. The C11 standard (and probably C99 too) also states under §6.7.2.1 'Structure and union specifiers' ¶11 that: An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a

Unsigned integer bit field shift yields signed integer

*爱你&永不变心* 提交于 2019-12-04 01:10:59
Let consider the following program test.c : #include <stdio.h> struct test { unsigned int a:5; }; int main () { unsigned int i; struct test t = {1}; for (i = 0; i < t.a << 1; i++) printf("%u\n", i); return 0; } When compiled with gcc -Wsign-compare test.c the following warning is produced (tested with gcc 4.8.1): test.c:9:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for (i = 0; i < t.a << 1; i++) ^ clang -Wsign-compare test.c produces the following (tested with clang 3.2): test.c:9:19: warning: comparison of integers of different signs: 'unsigned int

Implementing a C style bitfield in Java

混江龙づ霸主 提交于 2019-12-04 01:09:40
问题 I have a problem that I am a bit stuck on and I was informed by a colleague that this would be a good place to seek help. I am trying to implement a C style bitfield in Java. Here is a rough example (I do not have the actual code in front of me at this moment). typedef union { typedef struct { unsigned short a :1; unsigned short b :1; unsigned short c :2; unsigned short d :10; } bitfield; unsigned short bitmap; }example_bitfield; I have a good bit of similar style bitfields from legacy code.

Is Aggregate Initialization of Bit-Fields Allowed?

喜欢而已 提交于 2019-12-03 18:13:15
I have a struct which contains bit-fields: struct Foo { unsigned a : 16, b : 16; }; And I want to know if I can use aggregate initialization on it's bit-fields. For example: struct Foo bar = {13, 42}; I note that this does work in gcc 5.1 and Visual Studio 2015. I'd just like something to certify this was standard approved initialization for both C and C++. From C++14 [dcl.init.aggr] we have An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions