美文网首页
C语言位域问题

C语言位域问题

作者: Nothing_655f | 来源:发表于2020-07-15 18:30 被阅读0次

C语言位域问题

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

struct 位域结构名
{ 位域列表 };

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

例如:

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 char a:4 ;
    unsigned char  :0 ;/*空域*/ 
    unsigned char b:4 ;/*从下一单元开始存放*/ 
    unsigned char c:4 ;
} 

在这个位域定义中,a占第一字节的4位,后4位填0表示不使用,b从第二字节开始,占用4位,c占用4位。

  1. 位域可以无位域名,这时它只用来作填充或调整位置。无名的位域是不能使用的。例如:
struct k { 
    int a:1 ; 
    int  :2 ; /*该2位不能使用*/ 
    int b:3 ;
    int c:2 ;
};

从以上分析可以看出,位域在本质上就是一种结构类型, 不过其成员是按二进位分配的。
附上《The C Book》这本书的一段说法:

While we're on the subject of structures, we might as well look at bitfields. They can only be declared inside a structure or a union, and allow you to specify some very small objects of a given number of bits in length. Their usefulness is limited and they aren't seen in many programs, but we'll deal with them anyway. This example should help to make things clear:

struct {
       /* field 4 bits wide */
       unsigned field1 :4;
       /*
        * unnamed 3 bit field
        * unnamed fields allow for padding
        */
       unsigned         :3;
       /*
        * one-bit field
        * can only be 0 or -1 in two's complement!
        */
       signed field2    :1;
       /* align next field on a storage unit */
       unsigned         :0;
       unsigned field3 :6;
}full_of_fields;

Each field is accessed and manipulated as if it were an ordinary member of a structure. The keywords signed and unsigned mean what you would expect, except that it is interesting to note that a 1-bit signed field on a two's complement machine can only take the values 0 or -1. The declarations are permitted to include the const and volatile qualifiers.

The main use of bitfields is either to allow tight packing of data or to be able to specify the fields within some externally produced data files. C gives no guarantee of the ordering of fields within machine words, so if you do use them for the latter reason, you program will not only be non-portable, it will be compiler-dependent too. The

Standard says that fields are packed into ‘storage units’, which are typically machine words. The packing order, and whether or not a bitfield may cross a storage unit boundary, are implementation defined. To force alignment to a storage unit boundary, a zero width field is used before the one that you want to have aligned.

Be careful using them. It can require a surprising amount of run-time code to manipulate these things and you can end up using more space than they save.

Bit fields do not have addresses—you can't have pointers to them or arrays of them.

相关文章

  • C语言位域问题

    C语言位域问题 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位。例如在存放一个开关量时...

  • C语言位运算

    C语言位运算_C语言中文网 C语言位域(位段)_C语言中文网

  • C语言----位域

    为什么使用位域 如果程序的结构中包含多个开关量,只有 TRUE/FALSE 变量,如下: 这种结构需要 8 字节的...

  • 《C语言22—位域》

    2019年3月25日星期一 多云 (声明:理论知识部分来自菜鸟教程网站!)今日学习内容: 26、C位域 如果程序的...

  • 黑色C语言 - 位域

    一、概念 位域是指信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位。例如在存放一个开关量时,...

  • C语言union和位域

    举例 union:union {int a;char b;short c;}un;位域:struct bitDom...

  • C语言中的位运算

    C语言中的位运算 结构体是唯一一种允许控制内存位(bit)的数据结构,称作位域(Bit Field) 位域不能离开...

  • [C]基础语法

    [C语言]数据结构 STRUCT 结构体 位域 ERUM 枚举语法定义格式 回调函数 C语言中的回到函数 Defi...

  • Objective-C Rumtime-Class的结构

    Objective-C一门动态性比较强的编程语言,其动态性是由Runtime API来支撑 位域 什么是位域:有些...

  • 初识C语言中的位域

    说实话以前在阅读《C Primer Plus》的时候,似乎没有接触到位域,也可能是日久记忆些许有些模糊,但是今天在...

网友评论

      本文标题:C语言位域问题

      本文链接:https://www.haomeiwen.com/subject/qtmjhktx.html