C++20中头文件bit的使用

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的个数。

以下为测试代码:

cpp 复制代码
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;
}

执行结果如下图所示:

GitHubhttps://github.com/fengbingchun/Messy_Test