Share via


bitset::test

Tests whether the bit at a specified position in a bitset is set to 1.

bool test(
   size_t _Pos,
) const;

Parameters

  • _Pos
    The position of the bit in the bitset to be tested for its value.

Return Value

true if the bit specified by the argument position is set to 1; otherwise, false.

Remarks

The member function throws an out_of_range exception that is the size of the bitset is less than or equal to the position specified in the argument.

Example

// bitset_test.cpp
// compile with: /EHsc
#include <bitset>
#include <iostream>

int main( )
{
   using namespace std;

   bitset<5> b1 ( 6 );
   bool b;
   bitset<5>::element_type e;

   cout << "Original bitset b1(6) is: ( "<< b1 << " )"
        << endl;
   cout << "Note: positions in a bitset are numbered\n"
         << " from the right starting with 0.\n" << endl;

   b = b1.test ( 3 );

   if ( b )
      cout << "The bit at position 3 of bitset b1 "
           << "has a value of 1." << endl;
   else
      cout << "The bit at position 3 of bitset b1 "
           << "has a value of 0." << endl;

   b1[3] = 1;
   cout << "Bitset b1 modified by b1[3] = 1 is: ( "<< b1 << " )"
        << endl;
   
   e = b1.test ( 3 );
   if ( e )
      cout << "The bit at position 3 of bitset b1 "
           << "has a value of 1." << endl;
   else
      cout << "The bit at position 3 of bitset b1 "
           << "has a value of 0." << endl;
}
Original bitset b1(6) is: ( 00110 )
Note: positions in a bitset are numbered
 from the right starting with 0.

The bit at position 3 of bitset b1 has a value of 0.
Bitset b1 modified by b1[3] = 1 is: ( 01110 )
The bit at position 3 of bitset b1 has a value of 1.

Requirements

Header: <bitset>

Namespace: std

See Also

Reference

bitset Class

Other Resources

bitset Members