If I have a requirement to create a data structure that has the following fields:
16-bit Size field
3-bit Version field
1-bit CRC field
How would I code this struct? I know the Size field would be an unsigned short type, but what about the other two fields?
First,
unsigned shortisn’t guaranteed to be only 16 bits, just at least 16 bits.You could do this:
Assuming you want no padding between the fields, you’ll have to issue the appropriate instructions to your compiler. With gcc, you can decorate the structure with
__attribute__((packed)):In Visual C++, you can use
#pragma pack: