fork(5) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. template <std::size_t N>
  6. constexpr char bin_string[] = {'0', 'b',
  7. ((N >> 7) & 1) ? '1' : '0',
  8. ((N >> 6) & 1) ? '1' : '0',
  9. ((N >> 5) & 1) ? '1' : '0',
  10. ((N >> 4) & 1) ? '1' : '0',
  11. ((N >> 3) & 1) ? '1' : '0',
  12. ((N >> 2) & 1) ? '1' : '0',
  13. ((N >> 1) & 1) ? '1' : '0',
  14. ((N >> 0) & 1) ? '1' : '0',
  15. '\0'
  16. };
  17.  
  18. template <std::size_t N>
  19. constexpr char hex_string[] = {'0', 'x',
  20. "0123456789ABCDEF"[(N >> 8u) & 0x0F],
  21. "0123456789ABCDEF"[(N >> 0u) & 0x0F],
  22. '\0'
  23. };
  24.  
  25.  
  26.  
  27. int main() {
  28. std::cout << bin_string<(0x1 << 2)> << std::endl;
  29. std::cout << hex_string<(0x1 << 2)> << std::endl;
  30. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
0b00000100
0x04