兩篇非常棒的文章。
1. 再談c語言位域
2. Section bitfields
簡要總結一下:
C99 guarentees that bit-fields will be packed as tightly as possible, provided they don’t cross storage unit boundaries
Note that the base type of a bit field is interpreted for signedness but not necessarily for size. It is up to implementors whether "short flip:1" or "long flip:1" are supported, and whether those base types change the size of the storage unit the field is packed into.就是說int a:2;並不是說就會分配int大小的空間,可能只分配可以容納2bits的空間。
大小端不僅影響byte order,也影響bit order。
對於一個byte,定義它的各個位爲:b7 b6 b5 b4 b3 b2 b1 b0. 其中 b7 是起始地址開始的地方。