by Someone · 6y ago · 0 comments
struct S {
// will usually occupy 2 bytes:
// 3 bits: value of b1
// 2 bits: unused
// 6 bits: value of b2
// 2 bits: value of b3
// 3 bits: unused
unsigned char b1 : 3, : 2, b2 : 6, b3 : 2;
};
This is more aimed at large integers.I don't think the standard even guarantees which bits in an integer the bitfields will be stored. That's important for network protocols.
> Adjacent bit field members may be packed to share and straddle the individual bytes.
So it might not be packed. Ofc it usually is, at least on gcc and such.
The examples I gave are integers you may have to compare or manipulate not just bitfields to copy.
I also disagree this is aimed at large integers based on the examples of the syntax in the article commonly being <16 bits, sometimes <8, while the examples I cited had non power of 2 use cases up to 48 bits.