【发布时间】:2011-01-16 16:34:01
【问题描述】:
【问题讨论】:
标签: c++ c programming-languages annotations
【问题讨论】:
标签: c++ c programming-languages annotations
这是bitfield。
这个特定的位域没有多大意义,因为您只能使用 16 位类型,并且您正在浪费一些空间,因为位域被填充到 int 的大小。
通常,您将它用于包含位大小元素的结构:
struct {
unsigned nibble1 : 4;
unsigned nibble2 : 4;
}
【讨论】:
struct name { int a:16; }
这意味着a被定义为16位内存空间。 int 的剩余位(16 位)可用于定义另一个变量,例如 b,如下所示:
struct name { int a:16; int b:16; }
所以如果int是32位(4字节),那么一个int的内存就分为a和b两个变量。
PS:我假设sizeof(int) = 4 字节,1 字节 = 8 位
【讨论】:
struct s
{
int a:1;
int b:2;
int c:7;
};/*size of structure s is 4 bytes and not 4*3=12 bytes since all share the same space provided by int declaration for the first variable.*/
struct s1
{
char a:1;
};/*size of struct s1 is 1byte had it been having any more char _var:_val it would have been the same.*/
【讨论】:
【讨论】: