我试图得到一个明确的答案来回答这个问题。 考虑以下内容:
struct MyStruct {
uint16_t a : 4;
uint8_t b : 4;
}
这句话正确吗?
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| | b | a |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
这是否正确?
+----+----+----+----+----+----+----+----+-----+----+----+----+----+----+----+----+----+
| | 22 | 21 | 20 | 19 | 18 | 17 | 16 | ... | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+----+----+----+----+----+----+----+----+-----+----+----+----+----+----+----+----+----+
| | b | ... | | a |
+----+----+----+----+----+----+----+----+-----+----+----+----+----+----+----+----+----+
我想我的问题有三个方面。
1. 改变声明顺序中的类型是否会将“光标”移动到下一个类型边界?
2. 这是一个非法声明吗?
3. 如果我希望类型表示其内容的最小大小怎么办?假设我有一个64位的块,我想将其拆分为位域。即使myVar
适合于uint8_t
,我是否需要将每个位字段声明为uint64_t myVar:4
?
b
后面有额外的填充(总共是32位而不是您所提出的16位或24位)。 - Jarod42