function
<bitset>

std::bitset operators

** bitset member functions: ***
bitset<N>& operator&= (const bitset<N>& rhs);
bitset<N>& operator|= (const bitset<N>& rhs);
bitset<N>& operator^= (const bitset<N>& rhs);
bitset<N>& operator<<= (size_t pos);
bitset<N>& operator>>= (size_t pos);
bitset<N> operator~() const;
bitset<N> operator<<(size_t pos) const;
bitset<N> operator>>(size_t pos) const;
bool operator== (const bitset<N>& rhs) const;
bool operator!= (const bitset<N>& rhs) const;

*** global 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 global functions (extraction/insertion): ***
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, bitset<N>& rhs);
Bitset operators
Bitset containers support a different set of operators than the other container classes, mainly to support bitwise operators and to allow them to be insert/extracted directly to/from streams.

All these operators emulate for the bitset container the bitwise logic which applies to fundamental data types.

Parameters

lhs
Left-hand side bitset object (global functions). It must be of the same amount of bits as the right-hand side bitset object (i.e. with the same N template parameter).
rhs
Right-hand side bitset object.
For member functions, rhs must have the same amount of bits as the bitset object.
pos
Amount of bit locations to be shifted.
is,os
istream or ostream object from which a bitset object is respectively extracted or inserted. The format in which bitsets are inserted/extracted is binary (successions of 0's and 1's).

Return value

Member functions: either *this or the result of the comparison.
Global functions: The left-hand side object (lhs, is or os).

Example

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
31
// bitset operators
#include <iostream>
#include <string>
#include <bitset>
using namespace std;

int main ()
{
  bitset<4> first (string("1001"));
  bitset<4> second (string("0011"));

  cout << (first^=second) << endl;          // 1010 (XOR,assign)
  cout << (first&=second) << endl;          // 0010 (AND,assign)
  cout << (first|=second) << endl;          // 0011 (OR,assign)

  cout << (first<<=2) << endl;              // 1100 (SHL,assign)
  cout << (first>>=1) << endl;              // 0110 (SHR,assign)

  cout << (~second) << endl;                // 1100 (NOT)
  cout << (second<<1) << endl;              // 0110 (SHL)
  cout << (second>>1) << endl;              // 0001 (SHR)

  cout << (first==second) << endl;          // false (0110==0011)
  cout << (first!=second) << endl;          // true  (0110!=0011)

  cout << (first&second) << endl;           // 0010
  cout << (first|second) << endl;           // 0111
  cout << (first^second) << endl;           // 0101

  return 0;
}


See also