#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<8> b1;
bitset<8> b2("00001111");//不能用非0\1字符初始化或赋值,运行期错误
bitset<8> b3(0xff);
bitset<16> b4(0xff);
if (b2.any())//has 1 bit ?
{
cout << "b2 has 1 in bit" << endl;
}
cout << "b2 is " << b2 << endl;
cout << "b2 size is " << b2.size() << endl;
cout << "b2 has " << b2.count() << " 1 in bitset" << endl;
cout << "the num b2 translate is " << b2.to_ulong() << endl;
for (int index = 0; index != 8; ++index)
cout << b2[index] << " ";
cout << endl;
b2.flip();
//for (int index = 0; index != 8; ++index)
// b2.flip(index);
for (int index = 0; index != 8; ++index)
cout << b2[index] << " ";
cout << endl;
b2.set();
//for (int index = 0; index != 8; ++index)
// b2.set(index);
for (int index = 0; index != 8; ++index)
cout << b2[index] << " ";
cout << endl;
b2.reset();
//for (int index = 0; index != 8; ++index)
// b2.reset(index);
for (int index = 0; index != 8; ++index)
cout << b2[index] << " ";
cout << endl;
cout << "b1 is " << b1 << endl;
cout << "b3 is " << b3 << endl;
cout << "b4 is " << b4 << endl;
return 0;
}
C++ biteset
最新推荐文章于 2023-01-29 00:19:39 发布