一般而言,标准库提供的bitset操作更直接、更容易阅读和书写、正确使用的可能性更高。而且,bitset对象的大小不受unsigned数的位数限制。通常来说,bitset优于整形数据的低级位操作。
bitset的几个操作和位运算之间的转化。
bitset<32> bs(88);
unsigned long l=88;
bs.set(6);
l |= 1UL<<6;
bs.reset(6);
l &= ~(1UL<<6)
构造函数可以传入一个string或者unsigned long。
简单介绍bitset的方法,copy msdn(在这里不做翻译了)。
any | The member function tests whether any bit in the sequence is set to 1. |
count | The member function returns the number of bits set in the bit sequence. |
flip | Toggles the value of all the bits in a bitset or toggles a single bit at a specified position. 这个函数的作用就是将所有的bit求反或者将指定位的bit求反,注意位都是从右边0开始的。 |
none | Tests if no bit has been set to 1 in a bitset object. |
reset | Resets all the bits in a bitset to 0 or resets a bit at a specified position to 0. |
set | Sets all the bits in a bitset to 1 or sets a bit at a specified position to 1. |
size | Returns the number of bits in a bitset object. |
test | Tests whether the bit at a specified position in a bitset is set to 1. |
to_string | Converts a bitset object to a string representation. |
to_ulong | Converts a bitset object to the integer that would generate the sequence of bits contained if used to initialize the bitset . |