operator<<,>>(std::bitset)

Header: <bitset>

Inserts or extracts a bitset from a character stream.

# Declarations

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 );
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 );

# Parameters

# Example

#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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 303C++98single-byte characters were extracted fromis, but CharT can have multiple bytesextracts CharT and comparesit with widened ‘0’ and ‘1’
LWG 396C++98the content written by operator« was locale-independentwrites widened ‘0’s and ‘1’s
LWG 3199C++98extracting a std::bitset<0> always sets failbitsuch extraction never sets failbit

# See also