C++20中头文件bit是数字库(numeric library)的一部分,界说用于访问、操作和处理单个位和位序列(individual bits and sequences of bits)的函数。
1.std::endian:指示标量类型的字节序(byte order),支持little(小端序)、big(大端序)、native:
如果所有标量类型都是小端序,则std::endian::native即是std::endian::little。如果所有标量类型都是大端序,则std::endian::native即是 std::endian::big。
如果所有标量类型的sizeof都即是1,则字节序可有可无,而且所有三个值std::endian::little、std::endian::big和std::endian::native都相同。
如果平台利用混合字节序,则std::endian::native既不即是std::endian::big,也不即是std::endian::little。
2.std::bit_cast:将一种类型的对象体现重新表明为另一种类型的对象体现
3.std::has_single_bit:检查一个数字是否是2的整数幂
4.std::bit_ceil:找到不小于给定值的最小的2的整数幂
5.std::bit_floor:找到不大于给定值的最大2的整数幂
6.std::bit_width:找到体现给定值所需的最小位数
7.std::rotl:计算按位左循环(left-rotation)的效果
8.std::rotr:计算按位右循环(right-rotation)的效果
9.std::countl_zero:从最高有效位开始计算连续0 bits的个数
10.std::countl_one:从最高有效位开始计算连续1 bits的个数
11.std::countr_zero:从最低有效位开始计算连续0 bits的个数
12.std::countr_one:从最低有效位开始计算连续1 bits的个数
13.std::popcount:计算无符号整数中1 bits的个数。
以下为测试代码:
- int test_bit()
- {
- // 1.std::endian
- if constexpr (std::endian::native == std::endian::big)
- std::cout << "big-endian\n";
- else if constexpr (std::endian::native == std::endian::little)
- std::cout << "little-endian\n"; // little-endian
- else
- std::cout << "mixed-endian\n";
- // 2.std::bit_cast
- static_assert(sizeof(float) == sizeof(uint32_t));
- constexpr float value{ 123.456f };
- uint32_t v1 = std::bit_cast<uint32_t>(value);
- float v2 = std::bit_cast<float>(v1);
- std::cout << std::hex << "v1: " << v1 << ", v2: " << v2 << std::dec << std::endl; // v1: 42f6e979, v2: 123.456
- // 3.std::has_single_bit
- constexpr auto v3{ 4u }, v4{ 6u };
- std::cout << std::has_single_bit(v3) << "," << std::has_single_bit(v4) << std::endl; // 1,0
- // 4.std::bit_ceil
- using bin = std::bitset<8>;
- std::cout << bin(std::bit_ceil(3u)) << "," << bin(std::bit_ceil(9u)) << std::endl; // 00000100,00010000
- // 5.std::bit_floor
- std::cout << bin(std::bit_floor(3u)) << "," << bin(std::bit_floor(9u)) << std::endl; // 00000010,00001000
- // 6.std::bit_width
- std::cout << std::bit_width(0u) << "," << std::bit_width(3u) << std::endl; // 0,2
-
- // 7.std::rotl
- constexpr std::uint8_t i{ 0b00011101 };
- std::cout << bin(std::rotl(i, 1)) << "," << bin(std::rotl(i, 4)) << std::endl; // 00111010,11010001
-
- // 8.std::rotr
- std::cout << bin(std::rotr(i, 1)) << "," << bin(std::rotr(i, 4)) << std::endl; // 10001110,11010001
-
- // 9.std::countl_zero
- constexpr std::uint16_t s{ 0b00100011 };
- std::cout << std::countl_zero(i) << "," << std::countl_zero(s) << std::endl; // 3, 10
- // 10.std::countl_one
- constexpr std::uint32_t l{ 0b11111111101100110000000011111100 };
- std::cout << std::dec << std::countl_one(i) << "," << std::countl_one(l) << std::endl; // 0,9
- // 11.std::countr_zero
- std::cout << std::countr_zero(i) << "," << std::countr_zero(l) << std::endl; // 0,2
- // 12.std::countr_one
- std::cout << std::countr_one(i) << "," << std::countr_one(s) << std::endl; // 1,2
- // 13.std::popcount
- std::cout << std::popcount(s) << "," << std::popcount(l) << std::endl; // 3,19
- return 0;
- }
复制代码 实行效果如下图所示:
GitHub:https://github.com/fengbingchun/Messy_Test
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |