picostitch
crafting (and) JavaScript
#C

Bit-Fields in C

I had not yet heard of a type (not sure if it's a type?) in C called bit-fields, or at least I can't remember anymore. Let's look at one:

struct {
   unsigned int isLoggedIn : 1;
   unsigned int isCustomer : 1;
   unsigned int isAbove18 : 1;
} person;

each member in this struct takes up 1 bit (the : 1 indicated that). This allows for optimizing the memory needed.

As far as I read the structure will use 4 bytes, and can contain up to 32 one-bit fields, above it will use any multiple of it.

I learned this one tutorialspoint "C - Bit Fields".