• bitset的常用函数和用法


    原文:https://www.cnblogs.com/magisk/p/8809922.html

    C++的 bitset 在 bitset 头文件中,它是一种类似数组的结构,它的每一个元素只能是0或1,每个元素仅用1bit空间。

    bitset常用构造函数有四种,如下:

     1    bitset<4> bitset1;  //无参构造,长度为4,默认每一位为0
     2 
     3     bitset<8> bitset2(12);  //长度为8,二进制保存,前面用0补充
     4 
     5     string s = "100101";
     6     bitset<10> bitset3(s);  //长度为10,前面用0补充
     7     
     8     char s2[] = "10101";
     9     bitset<13> bitset4(s2);  //长度为13,前面用0补充
    10 
    11     cout << bitset1 << endl;  //0000
    12     cout << bitset2 << endl;  //00001100
    13     cout << bitset3 << endl;  //0000100101
    14     cout << bitset4 << endl;  //0000000010101

    注意:

    用字符串构造时,字符串只能包含 '0' 或 '1' ,否则会抛出异常。

    构造时,需在<>中表明bitset 的大小(即size)。

    在进行有参构造时,若参数的二进制表示比bitset的size小,则在前面用0补充(如上面的栗子);若比bitsize大,参数为整数时取后面部分,参数为字符串时取前面部分,例如:

     1    bitset<2> bitset1(12);  //12的二进制为1100(长度为4),但bitset1的size=2,只取后面部分,即00
     2 
     3     string s = "100101";  
     4     bitset<4> bitset2(s);  //s的size=6,而bitset的size=4,只取前面部分,即1001
     5 
     6     char s2[] = "11101";
     7     bitset<4> bitset3(s2);  //与bitset2同理,只取前面部分,即1110
     8 
     9     cout << bitset1 << endl;  //00
    10     cout << bitset2 << endl;  //1001
    11     cout << bitset3 << endl;  //1110

    bitset对于二进制有位操作符,具体如下

     1     bitset<4> foo (string("1001"));
     2     bitset<4> bar (string("0011"));
     3 
     4     cout << (foo^=bar) << endl;       // 1010 (foo对bar按位异或后赋值给foo)
     5     cout << (foo&=bar) << endl;       // 0010 (按位与后赋值给foo)
     6     cout << (foo|=bar) << endl;       // 0011 (按位或后赋值给foo)
     7 
     8     cout << (foo<<=2) << endl;        // 1100 (左移2位,低位补0,有自身赋值)
     9     cout << (foo>>=1) << endl;        // 0110 (右移1位,高位补0,有自身赋值)
    10 
    11     cout << (~bar) << endl;           // 1100 (按位取反)
    12     cout << (bar<<1) << endl;         // 0110 (左移,不赋值)
    13     cout << (bar>>1) << endl;         // 0001 (右移,不赋值)
    14 
    15     cout << (foo==bar) << endl;       // false (0110==0011为false)
    16     cout << (foo!=bar) << endl;       // true  (0110!=0011为true)
    17 
    18     cout << (foo&bar) << endl;        // 0010 (按位与,不赋值)
    19     cout << (foo|bar) << endl;        // 0111 (按位或,不赋值)
    20     cout << (foo^bar) << endl;        // 0101 (按位异或,不赋值)

    此外,可以通过 [ ] 访问元素(类似数组),注意最低位下标为0,如下:

    1      bitset<4> foo ("1011");
    2     
    3     cout << foo[0] << endl;  //1
    4     cout << foo[1] << endl;  //1
    5     cout << foo[2] << endl;  //0

    bitset还支持一些有意思的函数,比如:

     1     bitset<8> foo ("10011011");
     2 
     3     cout << foo.count() << endl;  //5  (count函数用来求bitset中1的位数,foo中共有5个1
     4     cout << foo.size() << endl;   //8  (size函数用来求bitset的大小,一共有8位
     5 
     6     cout << foo.test(0) << endl;  //true  (test函数用来查下标处的元素是0还是1,并返回false或true,此处foo[0]为1,返回true
     7     cout << foo.test(2) << endl;  //false  (同理,foo[2]为0,返回false
     8 
     9     cout << foo.any() << endl;  //true  (any函数检查bitset中是否有1
    10     cout << foo.none() << endl;  //false  (none函数检查bitset中是否没有1
    11     cout << foo.all() << endl;  //false  (all函数检查bitset中是全部为1

    另外,还有一些函数:

     1     bitset<8> foo ("10011011");
     2 
     3     cout << foo.flip(2) << endl;  //10011111  (flip函数传参数时,用于将参数位取反,本行代码将foo下标2处"反转",即0变1,1变0
     4     cout << foo.flip() << endl;   //01100000  (flip函数不指定参数时,将bitset每一位全部取反
     5 
     6     cout << foo.set() << endl;    //11111111  (set函数不指定参数时,将bitset的每一位全部置为1
     7     cout << foo.set(3,0) << endl;  //11110111  (set函数指定两位参数时,将第一参数位的元素置为第二参数的值,本行对foo的操作相当于foo[3]=0
     8     cout << foo.set(3) << endl;    //11111111  (set函数只有一个参数时,将参数下标处置为1
     9 
    10     cout << foo.reset(4) << endl;  //11101111  (reset函数传一个参数时将参数下标处置为0
    11     cout << foo.reset() << endl;   //00000000  (reset函数不传参数时将bitset的每一位全部置为0

    一些类型转换的函数,如下:

    1     bitset<8> foo ("10011011");
    2 
    3     string s = foo.to_string();  //将bitset转换成string类型
    4     unsigned long a = foo.to_ulong();  //将bitset转换成unsigned long类型
    5     unsigned long long b = foo.to_ullong();  //将bitset转换成unsigned long long类型
    6 
    7     cout << s << endl;  //10011011
    8     cout << a << endl;  //155
    9     cout << b << endl;  //155
  • 相关阅读:
    不要胡思乱想
    天天被思想教育
    带金属牙套
    要清楚自己的弱点
    打碎自己,理解自己,重塑自己
    我的侄子
    Proj CMI Paper Reading: Developing Trustworthy Hardware with SecurityDriven Design and Verification
    Proj CMI Paper Reading: Expanding the Reach of Fuzz Testing
    Proj CMI Paper Reading: Defect Prediction Guided SearchBased Software Testing
    Proj CMI Paper Reading: Detect Vulnerabilities Utilizing Fuzzing
  • 原文地址:https://www.cnblogs.com/very-beginning/p/12388556.html
Copyright © 2020-2023  润新知