C++的 bitset 在 bitset 頭文件中,它是一種類似數組的結構,它的每一個元素只能是0或1,每個元素僅用1bit空間。
bitset常用構造函數有四種,如下
bitset<4> bitset1; //無參構造,長度爲4,默認每一位爲0
bitset<8> bitset2(12); //長度爲8,二進制保存,前面用0補充
string s = "100101";
bitset<10> bitset3(s); //長度爲10,前面用0補充
char s2[] = "10101";
bitset<13> bitset4(s2); //長度爲13,前面用0補充
cout << bitset1 << endl; //0000
cout << bitset2 << endl; //00001100
cout << bitset3 << endl; //0000100101
cout << bitset4 << endl; //0000000010101
注意:
用字符串構造時,字符串只能包含 '0' 或 '1' ,否則會拋出異常。
構造時,需在<>中表明bitset 的大小(即size)。
在進行有參構造時,若參數的二進制表示比bitset的size小,則在前面用0補充(如上面的栗子);若比bitsize大,參數爲整數時取後面部分,參數爲字符串時取前面部分(如下面栗子):
bitset<2> bitset1(12); //12的二進制爲1100(長度爲4),但bitset1的size=2,只取後面部分,即00
string s = "100101";
bitset<4> bitset2(s); //s的size=6,而bitset的size=4,只取前面部分,即1001
char s2[] = "11101";
bitset<4> bitset3(s2); //與bitset2同理,只取前面部分,即1110
cout << bitset1 << endl; //00
cout << bitset2 << endl; //1001
cout << bitset3 << endl; //1110
可用的操作符
bitset對於二進制有位操作符,具體如下
bitset<4> foo (string("1001"));
bitset<4> bar (string("0011"));
cout << (foo^=bar) << endl; // 1010 (foo對bar按位異或後賦值給foo)
cout << (foo&=bar) << endl; // 0010 (按位與後賦值給foo)
cout << (foo|=bar) << endl; // 0011 (按位或後賦值給foo)
cout << (foo<<=2) << endl; // 1100 (左移2位,低位補0,有自身賦值)
cout << (foo>>=1) << endl; // 0110 (右移1位,高位補0,有自身賦值)
cout << (~bar) << endl; // 1100 (按位取反)
cout << (bar<<1) << endl; // 0110 (左移,不賦值)
cout << (bar>>1) << endl; // 0001 (右移,不賦值)
cout << (foo==bar) << endl; // false (0110==0011爲false)
cout << (foo!=bar) << endl; // true (0110!=0011爲true)
cout << (foo&bar) << endl; // 0010 (按位與,不賦值)
cout << (foo|bar) << endl; // 0111 (按位或,不賦值)
cout << (foo^bar) << endl; // 0101 (按位異或,不賦值)
此外,可以通過 [ ] 訪問元素(類似數組),注意最低位下標爲0,如下:
bitset<4> foo ("1011");
cout << foo[0] << endl; //1
cout << foo[1] << endl; //1
cout << foo[2] << endl; //0
當然,通過這種方式對某一位元素賦值也是可以的,栗子就不放了。
可用函數
bitset還支持一些有意思的函數,比如:
bitset<8> foo ("10011011");
cout << foo.count() << endl; //5 (count函數用來求bitset中1的位數,foo中共有5個1
cout << foo.size() << endl; //8 (size函數用來求bitset的大小,一共有8位
cout << foo.test(0) << endl; //true (test函數用來查下標處的元素是0還是1,並返回false或
true,此處foo[0]爲1,返回true
cout << foo.test(2) << endl; //false (同理,foo[2]爲0,返回false
cout << foo.any() << endl; //true (any函數檢查bitset中是否有1
cout << foo.none() << endl; //false (none函數檢查bitset中是否沒有1
cout << foo.all() << endl; //false (all函數檢查bitset中是全部爲1
補充說明一下:test函數會對下標越界作出檢查,而通過 [ ] 訪問元素卻不會經過下標檢查,所以,在兩種方式通用的情況下,選擇test函數更安全一些
另外,含有一些函數:
bitset<8> foo ("10011011");
cout << foo.flip(2) << endl; //10011111 (flip函數傳參數時,用於將參數位取反,本行代碼將
foo下標2處"反轉",即0變1,1變0
cout << foo.flip() << endl; //01100000 (flip函數不指定參數時,將bitset每一位全部取反
cout << foo.set() << endl; //11111111 (set函數不指定參數時,將bitset的每一位全部置爲1
cout << foo.set(3,0) << endl; //11110111 (set函數指定兩位參數時,將第一參數位的元素置爲第二
參數的值,本行對foo的操作相當於foo[3]=0
cout << foo.set(3) << endl; //11111111 (set函數只有一個參數時,將參數下標處置爲1
cout << foo.reset(4) << endl; //11101111 (reset函數傳一個參數時將參數下標處置爲0
cout << foo.reset() << endl; //00000000 (reset函數不傳參數時將bitset的每一位全部置爲0
同樣,它們也都會檢查下標是否越界,如果越界就會拋出異常
最後,還有一些類型轉換的函數,如下:
bitset<8> foo ("10011011");
string s = foo.to_string(); //將bitset轉換成string類型
unsigned long a = foo.to_ulong(); //將bitset轉換成unsigned long類型
unsigned long long b = foo.to_ullong(); //將bitset轉換成unsigned long long類型
cout << s << endl; //10011011
cout << a << endl; //155
cout << b << endl; //155