std::bitset<N>::to_string

Converts the contents of the bitset to a string. Uses zero to represent bits with value of false and one to represent bits with value of true.

# Declarations

template< class CharT, class Traits, class Allocator >
std::basic_string<CharT, Traits, Allocator>
to_string( CharT zero = CharT('0'),
CharT one = CharT('1') ) const;

(until C++11)

template<
class CharT = char,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
>
std::basic_string<CharT, Traits, Allocator>
to_string( CharT zero = CharT('0'),
CharT one = CharT('1') ) const;

(since C++11) (constexpr since C++23)

template< class CharT, class Traits >
std::basic_string<CharT, Traits>
to_string( CharT zero = CharT('0'),
CharT one = CharT('1') ) const;

(until C++11)

template< class CharT >
std::basic_string<CharT> to_string( CharT zero = CharT('0'),
CharT one = CharT('1') ) const;

(until C++11)

std::string to_string( char zero = '0', char one = '1' ) const;

(until C++11)

# Parameters

# Notes

Since C++11, functions templates can have default template arguments. LWG issue 1113 removed the helper overloads (2-4) and added the corresponding default template arguments in (1).

# Example

#include <bitset>
#include <iostream>
 
int main()
{
    std::bitset<8> b{42};
    std::cout << b.to_string() << '\n'
              << b.to_string('*') << '\n'
              << b.to_string('O', 'X') << '\n';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 396C++98zero and one bits were converted to characters 0and 1 (which do not correspond to ‘0’ and ‘1’)added parameters to providevalues for these characters
LWG 434C++98all template arguments needed to be providedadded overloads (2-4)
LWG 853C++98overloads (2-4) did not have the defaultarguments added by LWG issue 396also added

# See also