fork download
  1. #include <iostream>
  2. #include <cstdint>
  3.  
  4.  
  5. // -----
  6.  
  7. /*
  8.  * Hexadecimal number formatting helper.
  9.  */
  10.  
  11. //#include <iostream>
  12. #include <string>
  13. #include <iomanip>
  14. #include <sstream>
  15. #include <climits>
  16. #include <type_traits>
  17.  
  18. namespace hex_out_helper {
  19. const int HEX_DIGIT_BITS = 4; // One hex digit = 4 bits.
  20. const int HEX_BASE_CHARS = 2; // For the "0x".
  21.  
  22. template<typename T> struct CharCheck {
  23. typedef T type;
  24. };
  25.  
  26. template<> struct CharCheck<signed char> {
  27. typedef char type;
  28. };
  29.  
  30. template<> struct CharCheck<unsigned char> {
  31. typedef char type;
  32. };
  33.  
  34. template<typename T> using CharChecker = typename CharCheck<T>::type;
  35. } // namespace hex_out_helper
  36.  
  37. // Output a number in hexadecimal, with proper formatting based on its type.
  38. template<typename T> std::string hex_out_s(T val) {
  39. using namespace hex_out_helper;
  40.  
  41. std::stringstream sformatter;
  42. sformatter << std::hex
  43. << std::internal
  44. << "0x"
  45. << std::setfill('0')
  46. << std::setw((sizeof(T) * CHAR_BIT / HEX_DIGIT_BITS))
  47. << (std::is_same<CharChecker<T>, char>::value ? static_cast<int>(val) : val);
  48. return sformatter.str();
  49. }
  50.  
  51. // -----
  52.  
  53. struct Addr {
  54. uint32_t offset : 6;
  55. uint32_t index : 10;
  56. uint32_t tag : 16;
  57.  
  58. Addr(uint32_t addr);
  59.  
  60. operator uint32_t();
  61.  
  62. void output();
  63. };
  64.  
  65. Addr::Addr(uint32_t addr)
  66. : offset(addr & 0x3F),
  67. index((addr & 0xFFC0) >> 6),
  68. tag((addr & 0xFFFF0000) >> 16)
  69. {}
  70.  
  71.  
  72. Addr::operator uint32_t() {
  73. return ((offset) | (index << 6) | (tag << 16));
  74. }
  75.  
  76. void Addr::output() {
  77. std::cout << std::dec
  78. << "Offset: " << offset << "\t\t(" << hex_out_s(offset) << ")\n"
  79. << "Index: " << index << " \t(" << hex_out_s(index << 6) << ")\n"
  80. << "Tag: " << tag << (tag > 999 ? "\t(" : "\t\t(")
  81. << hex_out_s(tag << 16) << ")\n";
  82. }
  83.  
  84. int main() {
  85. Addr addr = 0xFFFFFFFF;
  86. addr.output();
  87. std::cout << std::hex << hex_out_s(static_cast<uint32_t>(addr)) << std::endl;
  88.  
  89. std::cout << "And now, a couple changes...\n";
  90. addr.offset = 0x1A;
  91. addr.index = 0x32F;
  92. addr.output();
  93. std::cout << std::hex << hex_out_s(static_cast<uint32_t>(addr)) << std::endl;
  94.  
  95. std::cout << "Once more...\n";
  96. addr = 0x12345678;
  97. addr.output();
  98. std::cout << std::hex << hex_out_s(static_cast<uint32_t>(addr)) << std::endl;
  99.  
  100. std::cout << "And finally...\n";
  101. addr = 0x00000001;
  102. addr.output();
  103. std::cout << std::hex << hex_out_s(static_cast<uint32_t>(addr)) << std::endl;
  104. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Offset: 63		(0x0000003f)
Index:  1023   	(0x0000ffc0)
Tag:    65535	(0xffff0000)
0xffffffff
And now, a couple changes...
Offset: 26		(0x0000001a)
Index:  815   	(0x0000cbc0)
Tag:    65535	(0xffff0000)
0xffffcbda
Once more...
Offset: 56		(0x00000038)
Index:  345   	(0x00005640)
Tag:    4660	(0x12340000)
0x12345678
And finally...
Offset: 1		(0x00000001)
Index:  0   	(0x00000000)
Tag:    0		(0x00000000)
0x00000001