I've had some experience in C before, however I have never seen the bitfields feature before. I know one can use bitmasks to isolate certain bits in a data structure, but why then bother with bitfields?
For example, say the bits we want to isolate are the first 3 least significant bits. Then we can write:
/* our bitmasks */
#define FIELD_A (1 << 0)
#define FIELD_B (1 << 1)
#define FIELD_C (1 << 2)
int main(void)
{
/* the data structure that contains our three fields */
uint8_t flags;
/* accessing field A (as a boolean) */
int bool_a = !!(flags & FIELD_A);
/* accessing field B (as a boolean) */
int bool_b = !!(flags & FIELD_B);
/* accessing field C (as a boolean) */
int bool_c = !!(flags & FIELD_C);
return 0;
}
Why would we choose to write this as:
static struct fields {
int field_a : 1;
int field_b : 1;
int field_c : 1;
};
int main(void)
{
/* the data structure that contains our three fields */
struct fields flags;
/* accessing field A */
int bit_a = flags.a;
/* accessing field B */
int bit_b = flags.b;
/* accessing field C */
int bit_c = flags.c;
return 0;
}