Defined in header <bitset> | ||
---|---|---|
template< class CharT, class Traits, std::size_t N > std::basic_ostream<CharT, Traits>& operator<<( std::basic_ostream<CharT, Traits>& os, const std::bitset<N>& x ); | (1) | |
template< class CharT, class Traits, std::size_t N > std::basic_istream<CharT, Traits>& operator>>( std::basic_istream<CharT, Traits>& is, std::bitset<N>& x ); | (2) |
Inserts or extracts a bitset from a character stream.
x
to the character stream os
as if by first converting it to a std::basic_string<CharT,Traits>
using to_string()
, and then writing it into os
using the operator<<
(which is a FormattedOutputFunction for strings). The characters to use for ones and zeroes are obtained from the currently-imbued locale by calling std::use_facet<std::ctype<CharT>>(os.getloc()).widen()
with '1'
and '0'
as arguments.N
characters from is
and stores the characters in the bitset x
.Characters are extracted until either.
N
characters have been read, is
, or is.widen('0')
nor is.widen('1')
. If N > 0
and no characters are extracted, is.setstate(ios_base::failbit)
is called.
os | - | the character stream to write to |
is | - | the character stream to read from |
x | - | the bitset to be read or written |
The character stream that was operated on, e.g. os
or is
.
#include <bitset> #include <iostream> #include <sstream> int main() { std::string bit_string = "001101"; std::istringstream bit_stream(bit_string); std::bitset<3> b1; bit_stream >> b1; // reads "001", stream still holds "101" std::cout << b1 << '\n'; std::bitset<8> b2; bit_stream >> b2; // reads "101", populates the 8-bit set as "00000101" std::cout << b2 << '\n'; }
Output:
001 00000101
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 3199 | C++98 | extracting a std::bitset<0> always set failbit | such extraction never sets failbit |
performs binary shift left and shift right (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_ltltgtgt2