STL bitset

View Code
 1 #include<iostream>
2 #include<bitset>
3 #include<string>
4
5 using namespace std;
6
7 int main()
8
9 {
10
11 bitset<32> a;//a可以放32位二进制
12
13 cout << a <<endl;
14
15
16 bitset<16> b(0xffff);//1111 1111 ...
17
18 cout << b << endl;
19
20 bitset<32> c(0xffff);
21 cout << c << endl;
22
23 bitset<128> d(0xffff);//低位时1
24 cout << d << endl;
25
26 bitset<32> e(156);
27 cout << e << endl;
28
29 string str("111111100000111001101");
30 bitset<32> f(str,5,4);//从右数第五位开始用四位初始化bitset
31 cout << f << endl;
32
33
34 bitset<32> g(str,str.size()-4);//用最后四位
35 cout << g << endl;
36
37
38
39 return 0;
40
41 }
View Code
 1 #include<iostream>
2 #include<bitset>
3
4 using namespace std;
5
6 int main()
7
8 {
9
10 bitset<32> a(156);
11 cout << a << endl;
12
13 bool is_set = a.any();//检查有没有任何一位是1
14
15 if(is_set)
16 cout << "a里至少有一个1!" << endl;
17
18 bool is_not_set = a.none();//一个1都没有
19 if(is_not_set)
20 cout << "a里一个1都没有"<< endl;
21
22 size_t bit_set = a.count();
23 cout << "a里一共有" << bit_set << "个1"<< endl;
24
25 cout << "a的大小:"<< a.size() << endl;
26
27 cout << "a里有" << a.size() - bit_set << "个0" << endl;
28 return 0;
29 }

 

View Code
 1 #include<iostream>
2 #include<bitset>
3
4 using namespace std;
5
6 int main()
7
8 {
9
10 bitset<32> a;
11 cout << a << endl;
12
13 a[5] = 1;
14 cout << a << endl;
15
16 for(int index = 0; index!=32; index +=2)//偶数下标的变成1
17
18 a.set(index);//set函数是将index位置为1
19
20 //a[index] = 1;
21 cout << a << endl;
22
23 a.set();
24 cout << a << endl;
25
26 a.reset(5);//把第五位上的1变成0
27 cout << a << endl;
28
29 a.flip();//把1变成0 把0变成1 翻转
30 cout << a << endl;
31
32 a.flip(8);//第八位上翻转
33 cout << a << endl;
34
35 a.reset();
36 cout << a << endl;
37
38 a.set(8);
39 cout << a << endl;
40
41 a.flip();
42 cout << a << endl;
43
44 unsigned long b = a.to_ulong();//把a变成无符号十进制数
45 cout << b << endl;
46
47
48
49 return 0;
50
51 }

 

View Code
 1 #include<iostream>
2 #include<bitset>
3 #include<string>
4
5 using namespace std;
6
7 int main()
8 {
9
10 bitset<4> fourBits;
11 cout << fourBits << endl;
12
13 bitset<5> fiveBits(string("10101"));
14 cout << fiveBits << endl;
15
16 bitset<8> eightBits(255);
17 cout << eightBits << endl;
18
19 return 0;
20
21 }

 

View Code
 1 #include<iostream>
2 #include<bitset>
3
4 using namespace std;
5
6
7 int main()
8
9 {
10
11 //bitset大小不能变
12 bitset<8> eightBits;
13 cout << "Enter a 8-bit sequence: ";
14 cin >> eightBits;
15 cout << endl;
16
17 cout << eightBits << endl;
18
19 cout << "" << eightBits.count() << "个1"<< endl;
20
21 cout << "" << eightBits.size() - eightBits.count() << "个0"<< endl;
22
23 bitset<8> flipInput(eightBits);//用一个bitset初始化另一个新的bitset,并将其翻转
24 flipInput.flip();
25 cout << flipInput << endl;
26
27 bitset<8> eightMoreBits;
28 cout << "Enter another 8_bit sequence: ";
29 cin >> eightMoreBits;
30 cout << endl;
31
32 //常用运算
33 //与或优先级低,注意要用括号
34 cout << (eightBits & eightMoreBits )<< endl;//位与
35
36 cout << endl;
37 cout << (eightBits | eightMoreBits) << endl;//位或
38 cout << endl;
39
40 cout << (eightBits ^ eightMoreBits) << endl;//位异或
41
42
43
44
45
46 return 0;
47 }



posted @ 2012-03-27 23:36  uniquews  阅读(280)  评论(0编辑  收藏  举报