You can use something like:
typedef struct { unsigned char SixBits:6; unsigned char TwoBits:2; } tEightBits;
and then use:
tEightBits eight; eight.SixBits = 31; eight.TwoBits = 3;
But, to be honest, if you donโt need to follow the packed data external to your application, or if you are in a situation with limited memory, this kind of memory saving is usually not worth it. You will find that your code is much faster if it does not need to constantly pack and unpack data using bitwise and bit-operations.
Also keep in mind that using any type other than _Bool , signed int or unsigned int is an implementation issue. In particular, unsigned char may not work everywhere.
source share