In this case you should use union
typedef union _BYTE_OF_DATA { uint8_t data; struct { uint8_t padding1 : 2; uint8_t BitWithSomeMeaning : 1; uint8_t BitWithSomeOtherMeaning : 1; uint8_t padding 2 : 4; } BitsWithMeaning; } BYTE_OF_DATA, *PBYTE_OF_DATA; static_assert(sizeof(BYTE_OF_DATA) == 1, "Incorrect size");
So, you can fill in the data with one shot:
BYTE_OF_DATA myByte; myByte.data = someotherbyte;
And get a bit with the value:
int meaning1 = myByte.BitWithSomeMeaning; int meaning2 = myByte.BitWithSomeOtherMeaning;
Or do the opposite:
myByte.data = 0; // Put all fields to 0 myByte.BitWithSomeMeaning = 1; myByte.BitWithSomeOtherMeaning = 0; int data = myByte.data;
source share