10.bitset

 1 #include <iostream>
 2 //位运算,处理二进制非常方便,线性存储
 3 #include <bitset>
 4 #include <string>
 5 using namespace std;
 6 
 7 void main()
 8 {
 9     //bitset<8> mybit(255);//1111 1111
10     //for (int i = 7; i >= 0; i--)
11     //{
12     //    cout << mybit[i] << endl;
13     //}
14     int num = -1;
15     bitset<32> mybit(num);
16     for (int i = 31; i >= 0; i--)
17     {
18         cout << mybit[i];
19     }
20 
21     string strl = mybit.to_string();//转化字符串
22     unsigned long longl = mybit.to_ulong();//转化为无符号整数
23 
24     cout << "str1:" << strl << endl;
25     cout << "longl:" << longl << endl;
26 
27     mybit.reset();//重置
28     mybit.set(13, 1);//设置二进制位
29     cin.get();
30 }
原文地址:https://www.cnblogs.com/xiaochi/p/8627176.html