#include <boost/dynamic_bitset.hpp>
using namespace boost;
int main()
{
// 构造函数
dynamic_bitset<> db; // 空
dynamic_bitset<> db1(10); // 大小为10
dynamic_bitset<> db2(0x16, BOOST_BINARY(11101));// 大小为22
dynamic_bitset<> db3(std::string("0100")); // 大小为4
dynamic_bitset<> db4(db2);
dynamic_bitset<> db5 = db3;
cout << db2.to_ulong() << endl;// 29 转换为整数
cout << db3.to_ulong() << endl;// 4
// dynamic_bitset内部由高到底存储二进制位
// 访问
cout << db3[0];// 0
cout << db3[1];// 0
cout << db3[2];// 1
cout << db3[3] << endl;// 0
db3[0] = 1;// 非0就会置为1
cout << db3[0];// 1
cout << db3[1];// 0
cout << db3[2];// 1
cout << db3[3] << endl;// 0
db3[0] = 0;
cout << db3[0];// 0
cout << db3[1];// 0
cout << db3[2];// 1
cout << db3[3] << endl;// 0
cout << db3[0].flip();// 将该位置反,并返回
cout << db3[0] << endl;// 1
cout << db3.flip(0);//0100
cout << db3[0] << endl;;// 0
db3.flip();// 置反
cout << db3 << endl;// 1011
db3 = ~db3;// 置反
cout << db3 << endl;// 0100
db3.set(0);
cout << db3[0] << endl;// 1
db3.reset(0);
cout << db3[0] << endl;// 0
db.resize(5, 1);// 重置大小,每个位置为1 11111
cout << db.to_ulong() << endl;// 31
cout << db.size() << endl;// 5
db.reset(0);
cout << db.test(0) << endl;// 0
cout << db.to_ulong() << endl;// 30 11110
cout << db.count() << endl;// 4
db.push_back(true);// 111110
cout << db.to_ulong() << endl;// 62
dynamic_bitset<> db6(5, BOOST_BINARY(01001));
cout << db6 << endl;// 01001
cout << db6.num_blocks() << endl;// 1
cout << db6.size() << endl;// 5
db6.append(BOOST_BINARY(1001));// 100101001
cout << db6.to_ulong() << endl;// 297
cout << db6.num_blocks() << endl;// 增加一个block
cout << db6.size() << endl;// 37 = 4 * 8(一个block大小,字节数*8位/字节) + 5(原来的5位)
cout << db6 << endl;// 0000000000000000000000000000100101001
dynamic_bitset<> db7(4, BOOST_BINARY(1001));
dynamic_bitset<> db8(4, BOOST_BINARY(1011));
cout << (db7 ^ db8) << endl;//0010
cout << (db7 | db8) << endl;//1011
cout << (db7 & db8) << endl;//1001
cout << db8.find_next(3) << endl;//npos 从db8[3]开始找下一个1的位置,找不到返回npos
return 0;
}