fork download
  1. #include <iostream>
  2. #include <limits>
  3.  
  4. template <typename CharT, typename OutIt>
  5. class bin_num_put : public std::num_put<CharT, OutIt> {
  6. OutIt do_put(OutIt to, std::ios_base& fmt, CharT, unsigned long long v) const
  7. {
  8. using std::begin; using std::end;
  9. char narrow[] = "01";
  10. CharT wide[2];
  11. std::use_facet<std::ctype<CharT> >(fmt.getloc())
  12. .widen(begin(narrow), end(narrow) - 1, begin(wide));
  13. CharT buffer[std::numeric_limits<unsigned long long>::digits];
  14. std::fill(begin(buffer), end(buffer), wide[0]);
  15. for (CharT* e = end(buffer); v != 0; v /= 2)
  16. *--e = wide[v % 2];
  17. return std::copy(begin(buffer), end(buffer), to);
  18. }
  19. OutIt do_put(OutIt to, std::ios_base& fmt, CharT fill, long long v) const
  20. { return do_put(to, fmt, fill, static_cast<unsigned long long>(v)); }
  21. };
  22. template <typename CharT, typename traits>
  23. std::basic_ios<CharT, traits>& bin(std::basic_ios<CharT, traits>& ios)
  24. {
  25. ios.imbue(std::locale(ios.getloc(),
  26. new bin_num_put<CharT, std::ostreambuf_iterator<CharT> >()));
  27. return ios;
  28. }
  29.  
  30. int main()
  31. {
  32. std::cout << bin << 18446744073709551615ull << std::endl;
  33. }
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
1111111111111111111111111111111111111111111111111111111111111111