Tag: bit fields

c结构中的压缩位字段 – GCC

我在linux上使用c中的结构。 我开始使用位字段和“打包”属性,我遇到了一个奇怪的行为: struct t1 { int a:12; int b:32; int c:4; }__attribute__((packed)); struct t2 { int a:12; int b; int c:4; }__attribute__((packed)); void main() { printf(“%d\n”,sizeof(t1)); //output – 6 printf(“%d\n”,sizeof(t2)); //output – 7 } 为什么两个结构 – 完全相同 – 采用不同的字节数?

结构变量不会因赋值而改变

struct st { int a1 : 3; int a2 : 2; int a3 : 1; } void main(void) { x.a3 = -1; if (x.a3 == -1) printf(“TRUE\n”); else printf(“FALSE\n”); x.a3 = 1; if (x.a3 == 1) printf(“TRUE\n”); else printf(“FALSE\n”); } 如果是‘x.a3 = -1;’ 首先,如果是真 。 但是,为什么‘x.a3 = 1’在第二个没有变化? 它仍然是x.a3 = -1。 和 如果我输入‘x.a3 = 1;’ […]

C或C ++中位域的最大大小?

可能重复: struct bitfield max size(C99,C ++) 我可以在C或C ++的位字段中指定的位数是否有限制? 例如,我可以这样做: struct HugeInt { int myInt: 1000; }; 我问的是C和C ++,因为我知道语言规范有时会有所不同,并希望看看上面的例子是否可以保证在C或C ++中工作/不工作。