std::bitset 讲解

介绍

std::bitset 是 C++ 提供的一种 n-bit 固定大小序列的模版,可以用于标准逻辑运算,并且可以与字符串和整数相互转换,还可以使用标准流输出。

1
2
template< std::size_t N >
class bitset;

模版形参 N :要为 bitset 分配储存的 bit 位数

头文件:<bitset>

构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <bitset>

int main() {

// 1. 无参构造
std::bitset<5> testCase_1;
std::cout << testCase_1 << std::endl; // 00000

// 2. 传入整数
std::bitset<4> testCase_2{0xb};
std::cout << testCase_2 << std::endl; // 1011

std::bitset<4> testCase_3{0x10}; // 溢出位自动截断
std::cout << testCase_3 << std::endl; // 0000

// 3. 传入字符串
std::bitset<8> testCase_4{"10010101"}; // 传入 0-1 字符串
std::cout << testCase_4 << std::endl; // 10010101

std::bitset<4> testCase_5{"10010101"}; // 溢出位截断,只取前 4 位
std::cout << testCase_5 << std::endl; // 1001

std::bitset<8> testCase_6{"ABABA", /* 读取长度 */4, /* 代表 0 的字符 */'A', /* 代表 1 的字符 */'B'}; // 00000101
std::cout << testCase_6 << std::endl;

// 4. 支持赋值构造以及整数和字符串到 bitset 的强制类型转化
std::bitset<8> testCase_7 = testCase_6;
std::cout << testCase_7 << std::endl; // 00000101

std::cout << (std::bitset<8>)0xf << std::endl; // 00001111
std::cout << (std::bitset<8>)"1111" << std::endl; // 00001111

std::bitset<10> testCase_8 = 0x7f;
std::cout << testCase_8 << std::endl; // 0001111111

return 0;
}

成员方法

  • 支持下标 operator[]

    1
    2
    3
    std::bitset<10> num = (std::bitset<10>)"111011";
    std::cout << num[2] << std::endl; // 0
    // 从低位开始

  • bitset 支持所有常规位运算操作

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    std::bitset<10> num = 0xf;

    std::cout << (num & (std::bitset<10>)0x12) << std::endl; // 0000000010
    std::cout << (num | (std::bitset<10>)0x12) << std::endl; // 0000011111
    std::cout << (num ^ (std::bitset<10>)0x12) << std::endl; // 0000011101
    std::cout << (num << 2) << std::endl; // 0000111100
    std::cout << (num >> 2) << std::endl; // 0000000011

    num = -1;
    std::cout << num << std::endl; // 1111111111
    std::cout << (num >> 2) << std::endl; // 0011111111
    // 对 bitset 的右移都是逻辑右移

  • set() :设置单独位的值

    1
    2
    3
    std::bitset<10> num = -1;
    num.set(0, false); // 将下标为 0 的位设置为 0
    std::cout << num << std::endl; // 1111111110

  • flip() :取反,无参数时默认对全部位

    1
    2
    3
    4
    5
    std::bitset<10> num = 0xf;
    num.flip();
    std::cout << num << std::endl; // 1111110000
    num.flip(1);
    std::cout << num << std::endl; // 1111110010

  • 检查位

    • all() :判断是否所有位都为 1
    • any() :判断是否有任意一位为 1
    • none() :判断是否所有位为 0

    1
    2
    3
    4
    5
    std::bitset<4> num = 0x7;

    std::cout << num.all() << std::endl; // 0
    std::cout << num.any() << std::endl; // 1
    std::cout << num.none() << std::endl; // 0

  • count() :返回 1 的数量

    1
    2
    3
    std::bitset<4> num = 0x7;

    std::cout << num.count() << std::endl; // 3

  • size() :返回位数

    1
    2
    std::bitset<10> num;
    std::cout << num.size() << std::endl; // 10

  • 转换:

    • to_string() :返回数据的字符串类型
    • to_ulong :返回数据的 unsigned long 整数表示
    • to_ullong :返回数据的 unsigned long long 整数表示

    1
    2
    3
    4
    5
    6
    7
    8
    std::bitset<8> num_bit = 0xf;
    unsigned long num_1 = num_bit.to_ulong();
    unsigned long long num_2 = num_bit.to_ullong();
    std::string str = num_bit.to_string();

    std::cout << num_1 << std::endl; // 15
    std::cout << num_2 << std::endl; // 15
    std::cout << str << std::endl; // 00001111


std::bitset 讲解
https://goer17.github.io/2023/03/08/std-bitset 详解/
作者
Captain_Lee
发布于
2023年3月8日
许可协议