C++ Standard Library C++ STL Library

C++ <bitset> - operator& Function



The C++ <bitset> operator& function returns a bitset which contains the result of bitwise AND operation on given bitsets.

Syntax

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) noexcept;

Parameters

lhs Specify the first bitset object.
rhs Specify the second bitset object.

Return Value

Returns the bitset which contains the result of AND operation.

Exception

Never throws exceptions.

Example:

In the example below, the operator& function is used to perform bitwise AND operation on the given bitsets.

#include <iostream>
#include <bitset>
using namespace std;
 
int main (){
  bitset<4> bset1("1001");
  bitset<4> bset2("1100");

  cout<<"bset1 is: "<<bset1;
  cout<<"\nbset2 is: "<<bset2;

  //performing bitwise AND operation
  auto bset3 = bset1 & bset2;

  cout<<"\nbset1 & bset2 = "<<bset3;

  return 0;
}

The output of the above code will be:

bset1 is: 1001
bset2 is: 1100
bset1 & bset2 = 1000

❮ C++ <bitset> Library