Menu Home

结构体中的冒号:位域

有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位。例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可。为了节省存储空间,并使处理简便,C语言又提供了一种数据结构,称为“位域”或“位段”。所谓“位域”是把一个字节中的二进位划分为几个不同的区域,并说明每个区域的位数。每个域有一个域名,允许在程序中按域名进行操作。这样就可以把几个不同的对象用一个字节的二进制位域来表示。一、位域的定义和位域变量的说明位域定义与结构定义相仿,其形式为:

域结构名

{ 位域列表 };

其中位域列表的形式为: 类型说明符 位域名:位域长度
例如:

struct bs 
{ 
    int a:8; 
    int b:2; 
    int c:6; 
};

位域变量的说明与结构变量说明的方式相同。 可采用先定义后说明,同时定义说明或者直接说明这三种方式。例如:

struct bs 
{ 
    int a:8; 
    int b:2; 
    int c:6; 
}data;

说明data为bs变量,共占两个字节。其中位域a占8位,位域b占2位,位域c占6位。对于位域的定义尚有以下几点说明:
1. 一个位域必须存储在同一个字节中,不能跨两个字节。如一个字节所剩空间不够存放另一位域时,应从下一单元起存放该位域。也可以有意使某位域从下一单元开始。例如:

struct bs 
{ 
    unsigned a:4 
    unsigned :0 /*空域*/ 
    unsigned b:4 /*从下一单元开始存放*/ 
    unsigned c:4 
}

这个位域定义中,a占第一字节的4位,后4位填0表示不使用,b从第二字节开始,占用4位,c占用4位。
2. 位域可以无位域名,这时它只用来作填充或调整位置。无名的位域是不能使用的。例如:

struct k 
{ 
    int a:1 
    int :2 /*该2位不能使用*/ 
    int b:3 
    int c:2 
};

从以上分析可以看出,位域在本质上就是一种结构类型, 不过其成员是按二进位分配的。
简而言之,言而简之
这是位域操作的表示方法,也就是说后面加上“:1”的意思是这个成员的大小占所定义类型的1 bit,“:2”占2 bit,依次类推。当然大小不能超过所定义类型包含的总bit数。
一个bytes(字节)是8 bit(bit)。例如你的结构中定义的类型是u_char,一个字节,共8bit,最大就不能超过8。
32位机下,
short是2字节,共16bit,最大就不能超过16.
int是4字节,共32bit,最大就不能超过32.
依次类推。
这样定义比较省空间。例如你上面的结构,定义的变量类型是u_char,是一字节类型,即8bit。
fc_subtype占了4bit,fc_type占2bit,fc_protocol_version占2bit,共8bit,正好是一个字节。
其他八个成员,各占1bit,共8bit,正好也是一个字节。
因此你的结构的大小如果用sizeof(struct frame_control)计算,就是2bytes.

4 replies

  1. I have to point out my gratitude for your kind-heartedness giving support to those individuals that really need help with this particular concept. Your real commitment to getting the solution all over appeared to be remarkably effective and have in most cases made guys much like me to attain their aims. This warm and friendly information entails a lot to me and far more to my office colleagues. Regards; from each one of us.

  2. I wish to point out my appreciation for your generosity giving support to individuals that have the need for guidance on the subject matter. Your special dedication to getting the message along became remarkably beneficial and have surely encouraged workers just like me to achieve their aims. Your entire informative tips and hints implies this much a person like me and additionally to my peers. Regards; from all of us.

  3. Thank you so much for providing individuals with remarkably breathtaking possiblity to read critical reviews from this website. It is usually so lovely and full of a lot of fun for me and my office peers to search the blog on the least 3 times weekly to read the fresh secrets you have. And indeed, we’re usually astounded with all the striking concepts you give. Selected 2 tips on this page are in reality the very best I have ever had.

  4. I as well as my friends came digesting the great techniques found on your web site and instantly came up with a terrible suspicion I never thanked the web site owner for them. All of the guys are already for this reason stimulated to learn all of them and now have pretty much been taking pleasure in these things. We appreciate you genuinely simply considerate and then for utilizing this form of perfect things most people are really eager to understand about. Our honest apologies for not expressing appreciation to sooner.

Leave a Reply

Your email address will not be published.