I long knew there are bit-fields in C and occasionally I use them for defining densely packed structs:
typedef struct Message_s {
unsigned int flag : 1;
unsigned int channel : 4;
unsigned int signal : 11;
} Message;
When I read open source code, I instead often find bit-masks and bit-shifting operations to store and retrieve such information in hand-rolled bit-fields. This is so common that I do not think the authors were not aware of the bit-field syntax, so I wonder if there are reasons to roll bit-fields via bit-masks and shifting operations your own instead of relying on the compiler to generate code for getting and setting such bit-fields.
unsigned
to store16
bits -- that can all be held in 1unsigned
, e.g.struct { unsigned flag : 1, channel : 4, signal : 11; } Message;
(note the use of','
not';'
following the field definitions) – Vilmavimunsigned
with one-field each, or one,unsigned
and 3-fields, the compiler just considers the bytes actually used when resolving thesizeof
the struct..You have been doing a lot of good work these past couple of days/weeks -- pushing for 50K? – Vilmavim