Export (0) Print
Expand All

bitset::operator[]

Returns a reference to a bit at a specified position in a bitset if the bitset is modifiable; otherwise, it returns the value of the bit at that position.

bool operator[](
   size_t _Pos
) const;
reference operator[](
   size_t _Pos
);

_Pos

The position locating the bit within the bitset.

When compiling with _SECURE_SCL 1, a runtime error will occur if you attempt to access an element outside the bounds of the bitset. See Checked Iterators for more information.

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

int main( )
{
   using namespace std;
   bool b;
   bitset<5> b1 ( 6 );
   cout << "The initialized bitset<5> b1( 2 ) is: ( "<< b1 << " )."
        << endl;

   int i;
   for ( i = 0 ; i <= 4 ; i++ )
   {
      b = b1[ i ];
      cout << "  The bit in position "
           << i << " is " << b << ".\n";
   }
}

The initialized bitset<5> b1( 2 ) is: ( 00110 ).
  The bit in position 0 is 0.
  The bit in position 1 is 1.
  The bit in position 2 is 1.
  The bit in position 3 is 0.
  The bit in position 4 is 0.

Header: <bitset>

Namespace: std

Show:
© 2014 Microsoft