cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : bitset : operators
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
bitset
bitset::bitset
bitset operators
member functions:
· bitset::any
· bitset::count
· bitset::flip
· bitset::none
· bitset::operator[]
· bitset::reset
· bitset::set
· bitset::size
· bitset::test
· bitset::to_string
· bitset::to_ulong

-

bitset operators functions
** 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<<= (const bitset<N>& rhs);
bitset<N>& operator>>= (const bitset<N>& rhs);
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.
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

// 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

bitset::operator[] Access bit (public member function)
bitset::set Set bits (public member function)
bitset::reset Reset bits (public member function)
bitset::flip Flip bits (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us