Namespaces
Variants
Views
Actions

std::bitset::operator[]

From cppreference.com
< cpp‎ | utility‎ | bitset
Revision as of 16:06, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
bool operator[]( std::size_t pos ) const;
constexpr bool operator[]( std::size_t pos ) const;
</td>

<td > (1) </td> <td > (until C++11)
(since C++11) </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
reference operator[]( std::size_t pos );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Accesses the bit at position pos. The first version returns the value of the bit, the second version returns an object of type std::bitset::reference that allows modification of the value.

Unlike Template:rlpf, does not throw exceptions: the behavior is undefined if pos is out of bounds.

Contents

Parameters

pos - position of the bit to return

Return value

1) the value of the requested bit

2) an object of type std::bitset::reference, which allows writing to the requested bit.

Exceptions

None

Example

#include <iostream>
#include <bitset>
 
int main()
{
    std::bitset<8> b1(42);
    for (std::size_t i = 0; i < b1.size(); ++i) {
        std::cout << "b1[" << i << "]: " << b1[i] << '\n';
    }
    b1[0] = true; // modifies the first bit through bitset::refence
    std::cout << "After setting bit 0, the bitset holds " << b1 << '\n';
}

Output:

b1[0]: 0
b1[1]: 1
b1[2]: 0
b1[3]: 1
b1[4]: 0
b1[5]: 1
b1[6]: 0
b1[7]: 0
After setting bit 0, bitset is 00101011

See also

accesses specific bit
(public member function) [edit]