std::bitset<N>::operator[]

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.

# Declarations

bool operator[]( std::size_t pos ) const;

(constexpr since C++11)

reference operator[]( std::size_t pos );

(constexpr since C++23)

# Parameters

# Example

#include <bitset>
#include <cstddef>
#include <iostream>
 
int main()
{
    std::bitset<8> b1{0b00101010}; // binary literal for 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::reference
 
    std::cout << "After setting bit 0, b1 holds " << b1 << '\n';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 11C++981. the description was missing in the C++ standard2. there was only the non-const overload1. description added2. added the const overload
LWG 907C++98the behavior of reading the bit at pos was equivalentto that of test(pos), but test() may throw exceptionsavoids mentioning test()

# See also