4

I'd like to have an array inside of a bit-packed struct. I statically know the size of the array (32), and I'd like each element in the array to be a single bit. For example, I would like to be able to say something like:

struct example_s {
  // ...
  unsigned int flags[32] : 32;
} __attribute__((__packed__));

I've tried a couple things, but gcc won't budge. It would be nice to be able to do this so that I could write clean code that iterated over the elements in the packed array. Ideas?

mhahnenb
  • 78
  • 6

2 Answers2

7

If you simply put it into a (32-bit) int, then you can cleanly iterate over the bits with a for loop like this:

for (bit = 0; bit < 32; bit++)
    flagValue = ((flags & (1<<bit)) != 0;

Not much harder to write than an array indexing syntax.

If you wish to hide the bit-twiddling to make the code more readable you could even use a function or macro to access the bits - e.g. GetFlag(bit)

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
1

Bitfield member elements do not have addresses, so even if you could declare an array of them, there'd be no way to use it (all array access in C is pointer arithmetic and dereferencing). It's easy to code your own bit array using the bits of a larger type though; Jason has explained the basics. Generally you should avoid using bitfields unless you have a really good reason. They're usually more trouble than they're worth.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • You could say the same thing about struct members - the compiler *normally* generates offsets from the base address of the struct. It has to do a little additional magic to get and set values in bit-packed structs. Thus I see no reason why it couldn't do the same sort of behind-the-scenes magic for arrays. And I have a good reason to use bitfields :-) – mhahnenb Nov 28 '10 at 22:41
  • No. If `struct foo` has a member `char bar[10]`, then `foo.bar` is an expression that evaluates to type `char *`, and `foo.bar[2]` evaluates as `*(foo.bar + 2)`, making it all work. Array syntax `a[b]` is just syntactic sugar (and convenient grouping without parentheses) for `*(a+b)`. – R.. GitHub STOP HELPING ICE Nov 28 '10 at 22:57
  • If you have a "good reason" to use bitfields, I think you failed to understand Jason's answer. Try looking up bit array implementations - either on google or in past questions on SO - and I think you'll find satisfactory answers. – R.. GitHub STOP HELPING ICE Nov 28 '10 at 22:58
  • Sorry, I think I misunderstood your point initially. Are you saying that an array bar in foo might not be able to be addressed if foo is bit-packed? – mhahnenb Nov 28 '10 at 23:38
  • @mhahnenb: The problem is that the explicit semantics of an array dereference in C is that the array is converted to a pointer to its first element, an offset is added, and the result is dereferenced. This is how the `[]` operator is defined in the language. The same is *not* true of the `.` operator, which is why the same problem does not occur there. – caf Nov 29 '10 at 01:27
  • If the C Standard were to specify that [] on an array is different from decomposition followed by [] on the resulting pointer (which would clean up a lot of other issues), then arrays of bitfields would pose no semantic problem. – supercat Aug 26 '16 at 23:38