结构体声明中的冒号是什么意思,例如:1、:7、:16 或:32?

发布于 2024-08-08 17:59:13 字数 148 浏览 6 评论 0原文

下面的C++代码是什么意思?

unsigned char a : 1; 
unsigned char b : 7;

我猜它创建了两个字符 a 和 b,它们都应该是一个字节长,但我不知道“:1”和“:7”部分的作用。

What does the following C++ code mean?

unsigned char a : 1; 
unsigned char b : 7;

I guess it creates two char a and b, and both of them should be one byte long, but I have no idea what the ": 1" and ": 7" part does.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏九 2024-08-15 17:59:13

1 和 7 是限制值范围的位大小。它们通常出现在结构和工会中。例如,在某些系统上(取决于char宽度和打包规则等),代码:

typedef struct {
    unsigned char a : 1;
    unsigned char b : 7;
} tOneAndSevenBits;

创建一个8位值,一位用于a,7位用于b

通常在 C 中用于访问“压缩”值,例如可能包含在 8 位字符的上半部分中的 4 位 nybble:

typedef struct {
    unsigned char leftFour  : 4;
    unsigned char rightFour : 4;
} tTwoNybbles;

对于我们当中的语言律师,C++11 标准的 9.6 部分解释了详细解释一下:


位域 [class.bit]

形式的成员声明符

标识符 opt   属性说明符opt   :   常量表达式

指定位域;它的长度由冒号与位字段名称分开。可选的属性说明符属于所声明的实体。位域属性不是类成员类型的一部分。

常量表达式应为值大于或等于零的整型常量表达式。整型常量表达式的值可能大于位域类型的对象表示中的位数;在这种情况下,额外的位用作填充位,并且不参与位字段的值表示。

类对象内位域的分配是实现定义的。位字段的对齐是实现定义的。位字段被打包到一些可寻址的分配单元中。

注意:位域在某些机器上跨接分配单元,而在其他机器上则不然。位字段在某些机器上从右到左分配,而在其他机器上从左到右分配。 - 尾注

The 1 and the 7 are bit sizes to limit the range of the values. They're typically found in structures and unions. For example, on some systems (depends on char width and packing rules, etc), the code:

typedef struct {
    unsigned char a : 1;
    unsigned char b : 7;
} tOneAndSevenBits;

creates an 8-bit value, one bit for a and 7 bits for b.

Typically used in C to access "compressed" values such as a 4-bit nybble which might be contained in the top half of an 8-bit char:

typedef struct {
    unsigned char leftFour  : 4;
    unsigned char rightFour : 4;
} tTwoNybbles;

For the language lawyers amongst us, the 9.6 section of the C++11 standard explains this in detail, slightly paraphrased:


Bit-fields [class.bit]

A member-declarator of the form

     identifieropt   attribute-specifieropt   :   constant-expression

specifies a bit-field; its length is set off from the bit-field name by a colon. The optional attribute-specifier appertains to the entity being declared. The bit-field attribute is not part of the type of the class member.

The constant-expression shall be an integral constant expression with a value greater than or equal to zero. The value of the integral constant expression may be larger than the number of bits in the object representation of the bit-field’s type; in such cases the extra bits are used as padding bits and do not participate in the value representation of the bit-field.

Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into some addressable allocation unit.

Note: bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others. - end note

向日葵 2024-08-15 17:59:13

我相信那些将是位字段。

I believe those would be bitfields.

感情洁癖 2024-08-15 17:59:13

严格来说,位域必须是 int、unsigned int 或 _Bool。尽管大多数编译器都会采用任何整型。

参考 C11 6.7.2.1:

位字段应具有合格或不合格的类型
_Bool、signed int、unsigned int 或其他版本的版本
实现定义的类型。

您的编译器可能会分配 1 个字节的存储空间,但可以自由地获取更多字节的存储空间。

参考 C11 6.7.2.1:

实现可以分配任何大的可寻址存储单元
足以容纳一个位字段。

当您有多个相继声明的位域时,就会节省开支。在这种情况下,如果可能的话,分配的存储将被打包。

参考 C11 6.7.2.1:

如果还有足够的空间,则位字段
紧随结构中另一个位字段之后的位字段应被打包
分成同一单元的相邻位。如果剩余空间不足,
是否将不适合的位域放入下一个单元或
重叠相邻单元是实现定义的。

Strictly speaking, a bitfield must be a int, unsigned int, or _Bool. Although most compilers will take any integral type.

Ref C11 6.7.2.1:

A bit-field shall have a type that is a qualified or unqualified
version of _Bool, signed int, unsigned int, or some other
implementation-defined type.

Your compiler will probably allocate 1 byte of storage, but it is free to grab more.

Ref C11 6.7.2.1:

An implementation may allocate any addressable storage unit large
enough to hold a bit- field.

The savings comes when you have multiple bitfields that are declared one after another. In this case, the storage allocated will be packed if possible.

Ref C11 6.7.2.1:

If enough space remains, a bit-field that
immediately follows another bit-field in a structure shall be packed
into adjacent bits of the same unit. If insufficient space remains,
whether a bit-field that does not fit is put into the next unit or
overlaps adjacent units is implementation-defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文