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.
pos | - | position of the bit to return |
std::bitset::reference
, which allows writing to the requested bit.None.
#include <iostream> #include <bitset> 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'; }
Output:
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 11 | C++98 | (1) the description was missing in the C++ standard (2) there was only the non-const overload | (1) description added (2) added the const overload |
accesses specific bit (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/bitset/operator_at