<iostream> Support

The following stream operators are available and work similar to built-in integer types.

#include <boost/int128/iostream.hpp>
namespace boost {
namespace int128 {

template <typename charT, typename traits>
std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const int128& v);

template <typename charT, typename traits>
std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, int128& v);

template <typename charT, typename traits>
std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& os, const uint128& v);

template <typename charT, typename traits>
std::basic_istream<charT, traits>& operator>>(std::basic_istream<charT, traits>& is, uint128& v);

} // namespace int128
} // namespace boost

Flags

The following flags from <ios> are honored. The base flags (std::oct, std::dec, std::hex) affect both input and output; the remaining flags affect output only.

  • std::oct - Octal numbers (input and output)

  • std::dec - Decimal numbers (input and output)

  • std::hex - Hexadecimal numbers (input and output)

  • std::uppercase - Uppercase hexadecimal digits on output (e.g. FFFF)

  • std::nouppercase - Lowercase hexadecimal digits on output (e.g. ffff)

  • std::showbase - Adds a leading base prefix for hex or oct numbers on output (e.g. 0xffff)

  • std::noshowbase - Omits the leading base prefix on output (e.g. ffff)

On extraction (operator>>) with std::hex a leading 0x or 0X prefix is accepted and consumed regardless of showbase, and the digits are parsed case-insensitively. A bare leading zero is an ordinary digit in every base, so 0f in hexadecimal is 15 and 017 in octal is 15. Extraction stops at the first character that is not a digit in the active base and leaves it in the stream. If no digit could be extracted, or the value does not fit in the target type, failbit is set, the target is set to zero, and every character is left in the stream, so while (is >> value) terminates as it does for the builtin integer types. On insertion (operator<<) showbase adds no prefix to a zero, again matching the builtin types.

See the IO streaming example for usage demonstrations.