在c++中的bit field位域

The following properties of bit fields are implementation-defined

1、 The value that results from assigning or initializing a bit field with a value out of range, or from incrementing a bit field past its range.
在C++中,如果位域赋值值超过范围,结果由编译器实现。
2、Everything about the actual allocation details of bit fields within the class object
   For example, on some platforms, bit fields don't straddle bytes, on others they do
   Also, on some platforms, bit fields are packed left-to-right, on others right-to-left
在c++中,位域根据系统大端或者小端的不同,内存布局不同。
3、
 Whether char, short, int, long, and long long bit fields that aren't explicitly signed or unsigned are signed or unsigned.
 For example, int b:3; may have the range of values 0..7 or -4..3.

在c++中,char short int long long long等没有强制指定是否有符号,那么范围大小由编译器实现。


struct S_CppUint
{
 // three-bit unsigned field,
 // allowed values are 0...7
 unsigned int b : 3;
};



struct S_CppZero
{
    // 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;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值