位域的存储Storage of Bit Fields

ANSI 3.5.2.1 int 内的位域的分配顺序ANSI 3.5.2.1 The order of allocation of bit fields within an int

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

struct mybitfields  
{  
   unsigned a : 4;  
   unsigned b : 5;  
   unsigned 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  

由于 80x86 处理器将整数值的低字节存储在高字节之前,因此上面的整数 0x01F2 将按 0xF2 后跟 0x01 的形式存储在物理内存中。Since the 80x86 processors store 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.

另请参阅See Also

结构、联合、枚举和位域Structures, Unions, Enumerations, and Bit Fields