函式
<bitset>

std::bitset 運算子

成員函式
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;
非成員函式
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 插入器/提取器
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& 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;
非成員函式
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 插入器/提取器
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
要移位的位數。
is,os
從中分別提取或插入 bitset 物件的 basic_istreambasic_ostream 物件。插入/提取 bitset 的格式是(已適當拓寬的)'0''1' 字元序列。

返回值

如果為引用:左側物件(*thisisos)。
否則:操作的結果(對於關係運算符,是 bitset 物件,或 truefalse)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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 物件中的所有位,並且(如果是複合賦值)也修改它們。

異常安全

在發生異常時,流插入器/提取器會使所有物件保持有效狀態(基本保證)。
其他操作從不丟擲異常(無異常保證)。

另見