Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

bitset operators

公众成员函数  <bitset>

C++98; member functions
bitset& operator&= (const bitset& rhs);
bitset& operator|= (const bitset& rhs);
bitset& operator^= (const bitset& rhs);
bitset& operator<<= (size_t pos);
bitset& operator>>= (size_t pos);
bitset operator~() const;
bitset operator<<(size_t pos) const;
bitset operator>>(size_t pos) const;
bool operator== (const bitset& rhs) const;
bool operator!= (const bitset& rhs) const;
non-member functions
template<size_t N>
  bitset<N> operator& (const bitset<N>& lhs, const bitset<N>& rhs);
template<size_t N>
  bitset<N> operator| (const bitset<N>& lhs, const bitset<N>& rhs);
template<size_t N>
  bitset<N> operator^ (const bitset<N>& lhs, const bitset<N>& rhs);
iostream inserters/extractors
template<class charT, class traits, size_t N>
  basic_istream<charT, traits>&
    operator>> (basic_istream<charT,traits>& is, bitset<N>& rhs);
template<class charT, class traits, size_t N>
  basic_ostream<charT, traits>&
    operator<< (basic_ostream<charT,traits>& os, const bitset<N>& rhs);
C++11; member functions
bitset& operator&= (const bitset& rhs) noexcept;
bitset& operator|= (const bitset& rhs) noexcept;
bitset& operator^= (const bitset& rhs) noexcept;
bitset& operator<<= (size_t pos) noexcept;
bitset& operator>>= (size_t pos) noexcept;
bitset operator~() const noexcept;
bitset operator<<(size_t pos) const noexcept;
bitset operator>>(size_t pos) const noexcept;
bool operator== (const bitset& rhs) const noexcept;
bool operator!= (const bitset& rhs) const noexcept;
non-member functions
template<size_t N>
  bitset<N> operator& (const bitset<N>& lhs, const bitset<N>& rhs) noexcept;
template<size_t N>
  bitset<N> operator| (const bitset<N>& lhs, const bitset<N>& rhs) noexcept;
template<size_t N>
  bitset<N> operator^ (const bitset<N>& lhs, const bitset<N>& rhs) noexcept;
iostream inserters/extractors
template<class charT, class traits, size_t N>
  basic_istream<charT, traits>&
    operator>> (basic_istream<charT,traits>& is, bitset<N>& rhs);
template<class charT, class traits, size_t N>
  basic_ostream<charT, traits>&
    operator<< (basic_ostream<charT,traits>& os, const bitset<N>& rhs);


bitset操作
使用bitset对象的内容执行正确的按位操作。

☲  参数


lhs
左边的bitset对象(用于非成员函数)。

rhs
右边的bitset对象。
左边和右边的bitset对象必须具有相同数量的位(即具有相同的类模板形参N)。

pos
要移动的bit位置序号。

is,os
Basic_istream或basic_ostream对象,分别从中提取或插入bitset对象。 插入/提取bitsets的格式为(适当加宽)'0'和'1'字符。

☉  返回值



如果是引用: 左边的对象(*this, is或os)。
否则: 操作的结果(bitset对象,关系操作为true或false)。

☣  示例



// bitset operators
#include <iostream>       // std::cout
#include <string>         // std::string
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo (std::string("1001"));
  std::bitset<4> bar (std::string("0011"));

  std::cout << (foo^=bar) << '\n';       // 1010 (XOR,assign)
  std::cout << (foo&=bar) << '\n';       // 0010 (AND,assign)
  std::cout << (foo|=bar) << '\n';       // 0011 (OR,assign)

  std::cout << (foo<<=2) << '\n';        // 1100 (SHL,assign)
  std::cout << (foo>>=1) << '\n';        // 0110 (SHR,assign)

  std::cout << (~bar) << '\n';           // 1100 (NOT)
  std::cout << (bar<<1) << '\n';         // 0110 (SHL)
  std::cout << (bar>>1) << '\n';         // 0001 (SHR)

  std::cout << (foo==bar) << '\n';       // false (0110==0011)
  std::cout << (foo!=bar) << '\n';       // true  (0110!=0011)

  std::cout << (foo&bar) << '\n';        // 0010
  std::cout << (foo|bar) << '\n';        // 0111
  std::cout << (foo^bar) << '\n';        // 0101

  return 0;
}

输出:
1010
0010
0011
1100
0110
1100
0110
0001
0
1
0010
0111
0101

⇄ 数据竞争


操作涉及的bitset对象中的所有位都被访问,复合赋值中的-if -也被修改。

☂ 异常安全性



流的插入/提取在异常情况下保持所有对象处于有效状态(基本保证)。
其他操作从不抛出异常(no-throw保证)。

🍄  另请参阅



bitset::set 设置比特位 (函数模板)
bitset::reset 重置比特位(公众成员函数)
bitset::operator[] 访问比特位(公众成员函数)
flip 反转bit(公众成员函数)

联系我们 免责声明 关于CandCplus 网站地图