C++ bitset

搬運,原文地址:http://www.javashuo.com/article/p-ffwlqhwp-gy.htmlhtml

1.C++的bitset存在於 #include<bitset> 頭文件中,它是一種相似於數組的結構,每個元素只能是 0 或 1,若不是,則會拋出異常。每一個元素僅用1 bit空間。經常使用構造方法以下:數組

 1     bitset<4> b1;  //無參構造,長度大小爲4 
 2     cout << b1 << endl; //0000 
 3     
 4     bitset<8> b2(12);  //長度爲8,12會轉換成二進制保存,前面用 0 補充
 5     cout << b2 << endl; //00001100
 6     
 7     /*
 8     用char s[],會報錯,也不知道爲何。 
 9     */
10     string s = "10101"; //只能包含 0 或 1 
11     bitset<10> b3(s);
12     cout << b3 << endl; //0000010101   

2.在進行有參構造時,若參數的二進制表示比bitset的size小,則在前面用0補充(如上面的例子);若比bitsize大,參數爲整數時取後面部分,參數爲字符串時取前面部分(以下面例子):安全

1     bitset<2> b2(12); //12的二進制爲 1100 
2     cout << b2 << endl; //00
3     
4     string s = "111000000";
5     bitset<4> b3(s);
6     cout << b3 << endl; //1110

3.可用的操做符(前提是:bitset的size必須同樣大函數

 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 ,以下:spa

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

4.可用函數code

 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

補充說明一下:test函數會對下標越界做出檢查,而經過 [ ] 訪問元素卻不會通過下標檢查,因此,在兩種方式通用的狀況下,選擇test函數更安全一些htm

另外,含有一些函數:blog

 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

一樣,它們也都會檢查下標是否越界,若是越界就會拋出異常ip

最後,還有一些類型轉換的函數,以下:字符串

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
相關文章
相關標籤/搜索