class template
<functional>

std::bit_and

template <class T> struct bit_and;
Bitwise AND function object class
Binary function object class whose call returns the result of applying the bitwise "and" operation between its two arguments (as returned by operator &).

Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a function call.

It is defined with the same behavior as:

1
2
3
4
5
6
template <class T> struct bit_and {
  T operator() (const T& x, const T& y) const {return x&y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};

Objects of this class can be used on standard algorithms such as transform or accumulate.

Template parameters

T
Type of the arguments and return type of the functional call.
The type shall support the operation (operator&).

Member types

member typedefinitionnotes
first_argument_typeTType of the first argument in member operator()
second_argument_typeTType of the second argument in member operator()
result_typeTType returned by member operator()

Member functions

T operator() (const T& x, const T& y)
Member function returning the bitwise "and" of its arguments (x&y).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// bit_and example
#include <iostream>     // std::cout
#include <functional>   // std::bit_and
#include <algorithm>    // std::transform
#include <iterator>     // std::end


int main () {
  int values[] = {100,200,300,400,500};
  int masks[] = {0xf,0xf,0xf,255,255};
  int results[5];

  std::transform (values, std::end(values), masks, results, std::bit_and<int>());

  std::cout << "results:";
  for (const int& x: results)
    std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

Output:

results: 4 8 12 144 244


See also