bitset是C++标准库中的一个类,用来处理位操作及运算。可以通过下标访问。bitset在内存中以倒序存储,如存入的数据为1000,用for(int i=0;i
#include <iostream>
#include <stdlib.h>
#include <string>
#include <bitset>
using namespace std;
int main()
{
bitset<8>bint(1);//16位的二进制数据
for (int i = 0; i < bint.size(); i++)
cout << bint[i];
cout << endl;
cout << "输入初始状态:" << endl;
cin >> bint;
cout<<"大小:"<<bint.size()<<endl;
cout <<"all:" <<bint.all()<<endl;//全为1返回1,否则返回0
cout << "any:" << bint.any()<<endl;//全为0返回0,否则返回1
cout << "count:"<<bint.count() << endl;//返回1的个数
//cout << "flip:" << bint.flip() << endl;//全部取反
for (int i = 0; i < bint.size(); i++)
cout << bint[i];
cout << endl;
cout << "flip(n=1),将bint[1]取反:" << bint.flip(1) << endl;
//返回它转换为unsigned long的结果,如果超出范围则报错
cout << "to_ulong:" << bint.to_ulong() << endl;
//返回它转换为unsigned long long的结果,如果超出范围则报错
cout << "to_ullong:" << bint.to_ulong() << endl;
//转换为string类型
cout << "to_string:" << bint.to_string() << endl;
cout << "set:" << bint.set() << endl;//全部置为1
cout << "set(n):" << bint.set(2) << endl;//把bint[2]位置为1
cout << "reset():" << bint.reset() << endl;//全部置为0
cout << "none():"<<bint.none() << endl;//不存在1则返回1
cout << "hash:" << bint.hash() << endl;
/*for (int i=0;i<bint.size();i++)
{
cout << bint[i];
}*/
system("pause");
return 0;
}