赞
踩
位段的声明和结构是类似的,有两个不同:
int
、unsigned int
或signed int
//结构体
struct A {
int a;
int b;
int c;
int d;
};
//位段
struct B{
int _a : 2;
int _b : 5;
int _c : 10;
int _d : 30;
};
sizeof
去计算一下这个结构体的大小printf("结构体大小:%d\n", sizeof(struct A));
printf("位段大小:%d\n", sizeof(struct B));
可以看到,结构体的大小是16,位段是8,二者为何会存在区别呢?原因在于这个: 2
吗?
47b
,在内存中1B = 8b
,要存下这个47个比特位的话应该6个字节就够了,但是结果为什么是8呢?我们不得而知Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。