原文: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