std::bitset::to_string

From cppreference.com
< cpp‎ | utility‎ | bitset
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
(C++11)
Relational operators
Optional and any
(C++17)
(C++17)
Pairs and tuples
(C++11)
(C++17)
Swap, forward and move
(C++14)
(C++11)
(C++11)
Type operations
(C++11)
(C++17)
 
 
template<

    class CharT,
    class Traits,
    class Alloc

> std::basic_string<CharT,Traits,Allocator> to_string() 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)

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.

The resulting string contains N characters with the first character corresponds to the last (N-1th) bit and the last character corresponding to the first bit.

Contents

[edit] Parameters

zero - character to use to represent false
one - character to use to represent true

[edit] Return value

the converted string

[edit] Example

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

Output:

00101010
**1*1*1*
OOXOXOXO

[edit] See also

returns an unsigned long integer representation of the data
(public member function)
(C++11)
returns an unsigned long long integer representation of the data
(public member function)