STL bitset
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<string> 5 #include<bitset> 6 using namespace std; 7 8 int main( ) 9 { 10 string str = "10101010"; 11 bitset<10>cha(str,str.size() - 5); // 将最后几个元素赋值进去; 12 cout<<cha<<endl; 13 bitset<100>temp(20); // 建立一个 容器 初始化为 20 14 temp.set(); // 全部化为1; 15 temp.reset(); // 全部化为0; 16 temp.set(1); // 指定位置化为1; 17 temp.reset(2); // 指定位置化为0; 18 temp.flip(); // 全部反转 19 temp.flip(1); // 指定位置反转 20 cout<<temp[1]<<endl;// 获取 1 位置的 二进制数 21 cout<<temp.test(1)<<endl; // 判断 1 位置的 二进制数 是否为1 22 cout<<temp.count()<<endl; // 二进制为 1 的个数 23 cout<<temp.size()<<endl; // 二进制位 的个数 24 cout<<temp.any()<<endl; // 判断是否存在存在二进制为1的位置 25 temp = temp<<10; // 二进制进行右移动 26 temp = temp^temp; // 进行亦或运算; 27 cout<<temp<<endl; 28 cout<<temp.to_ulong()<<endl; // 讲结果 以 无符号长整型返回 29 return 0; 30 }