定义位字段时,可以在中间保留一些位空白,并将成员分配给特定位。为什么中间有些空位?
struct product {
unsigned int code : 6; // product code : 6 bit
unsigned int : 10; // not use 10 bit
unsigned int color : 5; // product color : 5 bit
unsigned int : 5; // not use 5 bit
unsigned int size : 6; // product size : 3 bit
};
Run Code Online (Sandbox Code Playgroud)
我不知道为什么我不使用中间的钻头
我正在学习C.在C Primer Plus中,我看到了一个字段示例如下:
struct box_props {
bool opaque : 1;
unsigned int fill_color : 3;
unsigned int : 4;
bool show_border : 1;
unsigned int border_color : 3;
unsigned int border_style : 2;
unsigned int : 2;
};
Run Code Online (Sandbox Code Playgroud)
我知道中间的4位未命名位字段用于让以下位从一个新字节开始.但是,我不明白为什么在结构的末尾有另一个未命名的位字段.它的目的是什么?有必要吗?