public member function
<bitset>

std::bitset::test

bool test (size_t pos) const;
Return bit value
Returns whether the bit at position pos is set (i.e., whether it is one).

Unlike the access operator (operator[]), this function performs a range check on pos before retrieveing the bit value, throwing out_of_range if pos is equal or greater than the bitset size.

Parameters

pos
Order position of the bit whose value is retrieved.
Order positions are counted from the rightmost bit, which is order position 0.
size_t is an unsigned integral type.

Return value

true if the bit at position pos is set, and false if it is not set.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// bitset::test
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<5> foo (std::string("01011"));

  std::cout << "foo contains:\n";
  std::cout << std::boolalpha;
  for (std::size_t i=0; i<foo.size(); ++i)
    std::cout << foo.test(i) << '\n';

  return 0;
}

Output:

foo contains:
true
true
false
true
false


Data races

The bitset is accessed.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the bitset.
If pos is not a valid bit position, out_of_range is thrown.

See also