bit-fields

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

[亡魂溺海] 提交于 2019-12-05 23:39:52
问题 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

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

試著忘記壹切 提交于 2019-12-05 23:13:47
问题 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

A bug in GCC implementation of bit-fields

柔情痞子 提交于 2019-12-05 20:25:52
问题 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

Size of packed struct with union of bit fields less than 8 bits in C

橙三吉。 提交于 2019-12-05 16:21:52
Is it possible in C to get the size of the following structure to be 2? #include <stdio.h> struct union_struct { char foo; char bar : 2; union { char foobar1 : 6; char foobar2 : 6; }; }; int main(void) { printf("size of union struct: %d\n", sizeof(struct union_struct)); return 0; } output, compiled with gcc: size of union struct: 3 If you are relying on implementation defined behavior, then yes, but you have to organize it a bit differently: #ifdef UNNAMED_BITFIELDS_ARE_WELL_DEFINED #define ANON #else #define ANON3(X) anonymous__## X ##__ #define ANON2(X) ANON3(X) #define ANON ANON2(__LINE__)

Will this bitfield work the way I expect?

北战南征 提交于 2019-12-05 12:11:37
I've been doing some reading about bitfields in C, how the C standard doesn't enforce any particular ordering of the fields in a machine word, and so on. I hope this question appropriately fits the format of SO. My question is whether my struct (definition following) will actually perform in the way I expect. Here's the definition I came up with, and then I'll discuss what I want: typedef enum { STATE_ONE, STATE_TWO, STATE_THREE, STATE_FOUR } __attribute__ ((packed)) State; typedef struct MyStruct { // One of State enum (maximum of 4 states). unsigned state : 2; // Remaining 30 bits are used

how to declare an unsigned int in a C program

房东的猫 提交于 2019-12-05 07:44:04
On this link I came across http://lxr.linux.no/#linux+v2.6.36/include/linux/pci.h#L299 integer declaration unsigned int is_added:1; I have made C programs and declared integers in them but in the above I see use of : What sort of syntax is that? I think you have come across a bit-field :) It's part of a struct , which means that it indicates that the field should only use a certain number of bits instead of an entire byte or more. This is bit field declaration in an array. The number post ":" denotes number of bits to allocate to this particular field of the structure. Although you need to be

How to work with bitfields longer than 64 bits?

懵懂的女人 提交于 2019-12-05 04:33:01
Question says it all. If I have this for a 96-bit field: uint32_t flags[3]; //(thanks @jalf!) How do I best go about accessing this, given that my subfields therein may lie over the 32-bit boundaries (eg. a field that runs from bit 29 to 35)? I need my accesses to be as fast as possible, so I'd rather not iterate over them as 32-bit elements of an array. [This answer is valid for C (and by extension, for C++ as well).] The platform-independent way is to apply bit-masks and bit-shifts as appropriate. So to get your field from 29 to 35 (inclusive): (flags[1] & 0xF) << 3 | (flags[0] & 0xE0000000)

Is Aggregate Initialization of Bit-Fields Allowed?

五迷三道 提交于 2019-12-05 03:04: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++. 回答1: 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

Offset in a struct with bit fields

大憨熊 提交于 2019-12-05 02:34:53
问题 If we have a struct with bit fields, then how are the subsequent members aligned in the struct? Consider the following code: struct A{ int a:1; char b; // at offset 1 }; struct B{ int a:16; int b: 17; char c; // at offset 7 }; printf("Size of A: %d\n", (int)sizeof(struct A)); printf("Offset of b in A: %d\n", (int)offsetof(struct A, b)); printf("Size of B: %d\n", (int)sizeof(struct B)); printf("Offset of c in B: %d\n", (int)offsetof(struct B, c)); Output: Size of A: 4 Offset of b in A: 1 Size

How can I use Bit-Fields to save memory?

ⅰ亾dé卋堺 提交于 2019-12-05 02:18:38
This is about ANSI-C (C90). This is what I know: I can directly tell the compiler how many bits I want for a specific variable. If I want 1 bit which can have the values zero or one. or 2 bits for the values 0,1,2,3, and so on...; I'm familiar with the syntax. I have problem concerning bitfields: I want to define a SET structure. It can have maximum 1024 elements (it can have less, but the maximum is 1024 elements). The domain of the set is from 1 to 1024. So an element could have any value 1-1024. I'm trying to create a structure for a SET, and it must be efficient as possible for the memory