fork download
  1. #include <iostream>
  2. #include <climits>
  3. #include <cstddef>
  4.  
  5. #include <cstdint>
  6.  
  7. template <typename T>
  8. void to_binary(T x)
  9. {
  10. const unsigned char* byte = reinterpret_cast<const unsigned char*>(&x + 1);
  11.  
  12. for (std::size_t i = sizeof(x); i > 0; --i)
  13. {
  14. --byte;
  15. for (std::size_t j = CHAR_BIT; j > 0; --j)
  16. std::cout << ( (*byte >> (j - 1)) & 1u );
  17. std::cout << ' ';
  18. }
  19. std::cout << std::endl;
  20. }
  21.  
  22. int main()
  23. {
  24. volatile std::int_least32_t x = 255L * 256 * 256 + (128L + 1L) * 256 + 2 + 4 + 8;
  25.  
  26. to_binary(x);
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 4400KB
stdin
Standard input is empty
stdout
00000000 11111111 10000001 00001110