- 通常, 我们用下面的语句来说明这些变量:
- char f1,f2,f3;
- unsigned int type;
- unsigned int index;
- 但是, 实际上标志 f1, f2, f3 分别只需要 1 位。变量 type 只需要 4 位, 而变量 index 只需要 9 位。 总共是 16位 ---- 2 个字节。我们用两个字节就够了。
- 我们可这样来做:
- struct packed_struct
- {
- unsigned int f1 :1;
- unsigned int f2 :1;
- unsigned int f3 :1;
- unsigned int type :4;
- unsigned int index :9;
- };
|