std::bitset<N>::operator[] (3) - Linux Manuals
std::bitset<N>::operator[]: std::bitset<N>::operator[]
Command to display std::bitset<N>::operator[]
manual in Linux: $ man 3 std::bitset<N>::operator[]
NAME
std::bitset<N>::operator[] - std::bitset<N>::operator[]
Synopsis
bool operator[]( std::size_t pos ) const; (until C++11)
constexpr bool operator[]( std::size_t pos ) const; (1) (since C++11)
reference operator[]( std::size_t pos ); (2)
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 test(), does not throw exceptions: the behavior is undefined if pos is out of bounds.
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
// Run this code
#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::reference
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, the bitset holds 00101011
See also
accesses specific bit
test (public member function)
Pages related to std::bitset<N>::operator[]
- std::bitset<N>::operator&=,|=,^=,~ (3) - std::bitset<N>::operator&=,|=,^=,~
- std::bitset<N>::operator<<,<<=,>>,>>= (3) - std::bitset<N>::operator<<,<<=,>>,>>=
- std::bitset<N>::all,std::bitset<N>::any,std::bitset<N>::none (3) - std::bitset<N>::all,std::bitset<N>::any,std::bitset<N>::none
- std::bitset<N>::bitset (3) - std::bitset<N>::bitset
- std::bitset<N>::count (3) - std::bitset<N>::count
- std::bitset<N>::flip (3) - std::bitset<N>::flip
- std::bitset<N>::reference (3) - std::bitset<N>::reference
- std::bitset<N>::reset (3) - std::bitset<N>::reset
- std::bitset<N>::set (3) - std::bitset<N>::set
- std::bitset<N>::size (3) - std::bitset<N>::size
- std::bitset<N>::test (3) - std::bitset<N>::test
- std::bitset<N>::to_string (3) - std::bitset<N>::to_string
- std::bitset<N>::to_ullong (3) - std::bitset<N>::to_ullong
- std::bitset<N>::to_ulong (3) - std::bitset<N>::to_ulong
- std::bitset (3) - std::bitset