1 struct s1
2 {
3 int i: 8;
4 int j: 4;
5 double b;
6 int a:3;
7 };
8
9 struct s2
10 {
11 int i;
12 int j;
13 double b;
14 int a;
15 };
16
17 struct s3
18 {
19 int i;
20 int j;
21 int a;
22 double b;
23 };
24
25 struct s4
26 {
27 int i: 8;
28 int j: 4;
29 int a:3;
30 double b;
31 };
32
33 struct s4
34 {
35 double b;
36 int i: 8;
37 int j: 4;
38 int a:3;
39 };
40
41 cout<<sizeof(s1)<<endl; // 24
42 cout<<sizeof(s2)<<endl; // 24
43 cout<<sizeof(s3)<<endl; // 24
44 cout<<sizeof(s4)<<endl; // 16
45 cout<<sizeof(s5)<<endl; // 16
可以看到,有double存在会干涉到位域(sizeof的算法参考上一节),所以使用位域的的时候,最好把float类型和double类型放在程序的开始或者最后。不要让double干扰你的位域。