C++ 对位排序有什么规定吗?我特别致力于协议(protocol)数据包布局,我怀疑是否有一种可移植的方法来指定将某个数字写入位 5、6、7,其中位 5 是“最重要的”。
我的问题:
- 0x01 是否总是表示为第 7 位已设置的字节?
- 是
bitset<8>().set(7).to_ulong()
总是等于 1?
最佳答案
自 20.5/3 (ISO/IEC 14882:2011)
When converting between an object of class bitset and a value of some integral type, bit position pos corresponds to the bit value 1 << pos.
即bitset<8>().set(7).to_ulong()
保证是 (1 << 7) == 128
.
关于c++ - std::bitset 位顺序是可移植的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7889956/