bit-field in CXX

bit-field in CXX

Declares a class data member with explicit size, in bits. Adjacent bit field members may be packed to share and straddle the individual bytes.

A bit filed declaration is a class data member declaration which uses the following declarator:

identifier(optional) attr(optional) : size (1)

identifier(optional) attr(optional) : size brace-or-equal-initializer (2) (since C++ 20)

The type of the bit field is introduced by the decl-specifier-seq of the declaration syntax.

  • attr(C++11) - optional sequence of any number of attributes.
  • identifier - the name of the bit field that it being declared. The name is optional: nameless bit-fields introduce the specified number of bits of padding.
  • size - an integral constant expression with a value greater or equal to zero. When greater than zero. this is the number of bits that this bit field will occupy. The value zero is only allowed for nameless bit-fields and has special meaning: it specifics that the next bit field in the class definition will begin at an allocation unit’s boundary.
  • brace-or-equal-initialize - default member initializer to be used with this bit field.

Let’s take an example:

struct S {
 // three-bit unsigned field,
 // allowed values are 0...7
 unsigned int b : 3;
};
struct S {
    // will usually occupy 2 bytes:
    // 3 bits: value of b1
    // 2 bits: unused
    // 6 bits: value of b2
    // 2 bits: value of b3
    // 3 bits: unused
    unsigned char b1 : 3, : 2, b2 : 6, b3 : 2;
};
struct S {
    // will usually occupy 2 bytes:
    // 3 bits: value of b1
    // 5 bits: unused
    // 6 bits: value of b2
    // 2 bits: value of b3
    unsigned char b1 : 3;
    unsigned char :0; // start a new byte
    unsigned char b2 : 6;
    unsigned char b3 : 2;
};

For more information:

https://en.cppreference.com/w/cpp/language/bit_field

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值