描述
bitset 提供位操作, 可以方便数值与二进制转换
1. 构造
(1).
bitset<bits>::bitset ()
bits指定包含多少个位, 例如 bitset<50> flags; flag共有50位数, 初始化都是0
(2)
bitset<bits>::bitset (unsigned long long value)
用value初始化bitset对象, bitset<50> flags(7); // flag: 00000...000111
(3)
explicit bitset<bits>::bitset (const string& str)
bitset<bits>::bitset (const string& str, string::size_type str_idx)
bitset<bits>::bitset (const string& str, string::size_type str_idx, string::size_type str_num)
bitset<bits>::bitset (const string& str, string::size_type str_idx, string::size_type str_num, string::charT zero)
bitset<bits>::bitset (const string& str, string::size_type str_idx, string::size_type str_num, string::charT zero, string::charT one)
用str的值初始化bitset对象,。str的值包含0或1,否则引发invalid_argument异常。str字符数小于bits,则前导置零,大于bits,则多余的部分忽略。
bitset<50> flags(string("1010101")); // flags: 0000...0001010101
bitset<50> flags(string("1111000"),2,3); // flags: 0000...0000000110
2. 操作
[1] 非修改操作
(1) 返回总位数
size_t bitset<bits>::size () const
(2) 获取值为1的位总数
size_t bitset<bits>::count () const
(3) 判断是否所有位都是1
bool bitset<bits>::all () const
(4) 判断是否有一个以上位值是1
bool bitset<bits>::any () const
(5) 判断是否所有位为0
bool bitset<bits>::none () const
(6) 判断 idx 指定的位是否为1
bool bitset<bits>::test (size_t idx) const
(7) 比较操作
bool bitset<bits>::operator == (const bitset<bits>& bits) const
bool bitset<bits>::operator != (const bitset<bits>& bits) const
[2] 修改操作, 返回修改后的对象
(8) 置1
bitset<bits>& bitset<bits>::set ()
bitset<bits>& bitset<bits>::set (size_t idx)
(9) 设置idx位的值
bitset<bits>& bitset<bits>::set (size_t idx, bool value)
(10) 置0
bitset<bits>& bitset<bits>::reset ()
bitset<bits>& bitset<bits>::reset (size_t idx)
(11) 取反
bitset<bits>& bitset<bits>::flip ()
bitset<bits>& bitset<bits>::flip (size_t idx)
(12) 操作符
bitset<bits>& bitset<bits>::operator ˆ= (const bitset<bits>& bits)
bitset<bits>& bitset<bits>::operator |= (const bitset<bits>& bits)
bitset<bits>& bitset<bits>::operator &= (const bitset<bits>& bits)
bitset<bits>& bitset<bits>::operator <<= (size_t num)
bitset<bits>& bitset<bits>::operator >>= (size_t num)
(13) 访问
bitset<bits>::reference bitset<bits>::operator[ ] (size_t idx)
bool bitset<bits>::operator[ ] (size_t idx) const
bitset<50> flags;
flags[42] = true; // set bit 42
flags[13] = flags[42]; // assign value of bit 42 to bit 13
flags[42].flip(); // toggle value of bit 42
if (flags[13]) { // if bit 13 is set,
flags[10] = ~flags[42]; // then assign complement of bit 42 to bit 10
}
3. 其他操作符
返回值是新的bitset对象
bitset<bits> bitset<bits>::operator ˜ () const
bitset<bits> bitset<bits>::operator << (size_t num) const
bitset<bits> bitset<bits>::operator >> (size_t num) const
bitset<bits> operator & (const bitset<bits>& bits1, const bitset<bits>& bits2)
bitset<bits> operator | (const bitset<bits>& bits1, const bitset<bits>& bits2)
bitset<bits> operator ˆ (const bitset<bits>& bits1, const bitset<bits>& bits2)
4. 数值转换
(1) bitset转换成数字
unsigned long bitset<bits>::to_ulong () const
unsigned long long bitset<bits>::to_ullong () const
(2) bitset转换成字符串
string bitset<bits>::to_string () const
string bitset<bits>::to_string (charT zero) const
string bitset<bits>::to_string (charT zero, charT one) const
5. 输入输出流
(1) 从字符流中读取0和1序列
istream& operator >> (istream& strm, bitset<bits>& bits)
(2) 作为0和1序列写入字符流
ostream& operator << (ostream& strm, const bitset<bits>& bits)
6. 应用实例
int main()
{
// print some numbers in binary representation
cout << "267 as binary short: "
<< bitset<numeric_limits<unsigned short>::digits>(267)
<< endl;
cout << "267 as binary long: "
<< bitset<numeric_limits<unsigned long>::digits>(267)
<< endl;
cout << "10,000,000 with 24 bits: "
<< bitset<24>(1e7) << endl;
// write binary representation into string
string s = bitset<42>(12345678).to_string();
cout << "12,345,678 with 42 bits: " << s << endl;
// transform binary representation into integral number
cout << "\"1000101011\" as number: "
<< bitset<100>("1000101011").to_ullong() << endl;
}
---------------------------------------------------------
267 as binary short: 0000000100001011
267 as binary long: 00000000000000000000000100001011
10,000,000 with 24 bits: 100110001001011010000000
12,345,678 with 42 bits: 000000000000000000101111000110000101001110
"1000101011" as number: 555