标题: 一个微软面试题--关于位结构体 [打印本页] 作者: 51黑黑黑 时间: 2016-2-12 20:49 标题: 一个微软面试题--关于位结构体 写出下列程序在X86上的运行结果。
struct mybitfields
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
}test;
关于位结构体在内存中的存储问题
Kevin's Theory #2: In a C structure that contains bit fields,if field A is defined in front of field B, then field A alwaysoccupies a lower bit address than field B.
说的是,在C结构体中,如果一个位域A在另一个位域B之前定义,那么位域A将存储在比B小的位地址中。
如果一个位域有多个位时,各个位的排列顺序通常是按CPU的端模式(Endianess)来进行的,即在大端模式(bigendian)下,高有效位在低位地址,小端模式则相反。