C++ Bitset Library - operator& Function



Description

The C++ function std::bitset::operator& performs bitwise AND operation on bitset.

Declaration

Following is the declaration for std::bitset::operator& function form std::bitset header.

C++98

template<size_t N>
bitset<N> operator& (const bitset<N>& first, const bitset<N>& second);

C++11

template<size_t N>
bitset<N> operator& (const bitset<N>& first, const bitset<N>& second) noexcept;

Parameters

  • first − First bitset object.

  • second − Second bitset object.

Return value

Returns bitset which contains result of bitwise AND operation.

Exceptions

This member function never throws exception.

Example

The following example shows the usage of std::bitset::operator& function.

#include <iostream>
#include <bitset>

using namespace std;

int main(void) {

   bitset<4> b("1010");
   bitset<4> mask("1000");

   /* Trun off all bits except 3rd bit */
   auto result = b & mask;

   cout << result << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

1000
bitset.htm
Advertisements