C 位域C Bit Fields

除了结构或联合的成员的声明符外,结构声明符也可以是指定数目的位,称为“位域”。In addition to declarators for members of a structure or union, a structure declarator can also be a specified number of bits, called a "bit field." 其长度从字段名称的声明符到冒号。Its length is set off from the declarator for the field name by a colon. 位域被解释为整型类型。A bit field is interpreted as an integral type.



type-specifier declarator opt: constant-expressiontype-specifier declarator opt: constant-expression

constant-expression 指定域的宽度(以位为单位)。The constant-expression specifies the width of the field in bits. declarator 的 type-specifier 必须为 unsigned int、signed int 或 int,而且 constant-expression 必须为非负整数值。The type-specifier for the declarator must be unsigned int, signed int, or int, and the constant-expression must be a nonnegative integer value. 如果值为零,则声明没有任何 declaratorIf the value is zero, the declaration has no declarator. 不允许位域的数组、指向位域的指针和返回位域的函数。Arrays of bit fields, pointers to bit fields, and functions returning bit fields are not allowed. 可选的 declarator 命名位域。The optional declarator names the bit field. 位域只能声明为结构的一部分。Bit fields can only be declared as part of a structure. Address-of 运算符 (&) 不能应用于位域组件。The address-of operator (&) cannot be applied to bit-field components.

未命名位域不可引用,且其内容在运行时是不可预测的。Unnamed bit fields cannot be referenced, and their contents at run time are unpredictable. 它们可以出于对齐目的用作“虚拟”字段。They can be used as "dummy" fields, for alignment purposes. 宽度指定为 0 的未命名位域确保在 struct-declaration-list 中跟在其后的成员的存储在 int 边界开始。An unnamed bit field whose width is specified as 0 guarantees that storage for the member following it in the struct-declaration-list begins on an int boundary.

位域还必须足够长以包含该位模式。Bit fields must also be long enough to contain the bit pattern. 例如,以下两个语句均不合法:For example, these two statements are not legal:

short a:17;        /* Illegal! */  
int long y:33;     /* Illegal! */  

以下示例定义了一个名为 screen 的二维结构数组。This example defines a two-dimensional array of structures named screen.

    unsigned short icon : 8;  
    unsigned short color : 4;  
    unsigned short underline : 1;  
    unsigned short blink : 1;  
} screen[25][80];  

该数组包含 2,000 个元素。The array contains 2,000 elements. 每个元素都是包含以下四个位域成员的单个结构:iconcolorunderlineblinkEach element is an individual structure containing four bit-field members: icon, color, underline, and blink. 每个结构的大小是两个字节。The size of each structure is two bytes.

位域与整数类型具有相同的语义。Bit fields have the same semantics as the integer type. 这意味着位域在表达式中的使用方式与同样基类型使用变量的方式完全相同,无论位域中有多少位。This means a bit field is used in expressions in exactly the same way as a variable of the same base type would be used, regardless of how many bits are in the bit field.

Microsoft 专用Microsoft Specific

定义为 int 的位域被视为带符号的位域。Bit fields defined as int are treated as signed. ANSI C 标准的 Microsoft 扩展允许位域的 char 和 long 类型(signed 和 unsigned)。A Microsoft extension to the ANSI C standard allows char and long types (both signed and unsigned) for bit fields. 带有基类型 long、short 或 char(signed 或 unsigned)的未命名位域强制与适于基类型的边界对齐。Unnamed bit fields with base type long, short, or char (signed or unsigned) force alignment to a boundary appropriate to the base type.

在整数中按照从最高有效位到最低有效位的顺序来分配位域。Bit fields are allocated within an integer from least-significant to most-significant bit. 在以下代码中In the following code

struct mybitfields  
    unsigned short a : 4;  
    unsigned short b : 5;  
    unsigned short c : 7;  
} test;  

int main( void );  
    test.a = 2;  
    test.b = 31;  
    test.c = 0;  

这些位将按如下所示排列:the bits would be arranged as follows:

00000001 11110010  
cccccccb bbbbaaaa  

由于 8086 系列处理器将整数值的低字节存储在高字节之前,因此上面的整数 0x01F2 将按 0xF2 后跟 0x01 的形式存储在物理内存中。Since the 8086 family of processors stores the low byte of integer values before the high byte, the integer 0x01F2 above would be stored in physical memory as 0xF2 followed by 0x01.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

结构声明Structure Declarations