fork download
  1. #include <iostream>
  2. #include <cstdint>
  3.  
  4. struct Addr {
  5. uint32_t offset : 6;
  6. uint32_t index : 10;
  7. uint32_t tag : 16;
  8.  
  9. Addr(uint32_t addr);
  10.  
  11. operator uint32_t();
  12.  
  13. void output();
  14. };
  15.  
  16. Addr::Addr(uint32_t addr)
  17. : offset(addr & 0x3F),
  18. index((addr & 0xFFC0) >> 6),
  19. tag((addr & 0xFFFF0000) >> 16)
  20. {}
  21.  
  22.  
  23. Addr::operator uint32_t() {
  24. return ((offset) | (index << 6) | (tag << 16));
  25. }
  26.  
  27. void Addr::output() {
  28. std::cout << "Offset: " << std::dec << offset << "\t\t(" << std::hex << offset << ")\n"
  29. << "Index: " << std::dec << index << " \t(" << std::hex << index << ")\n"
  30. << "Tag: " << std::dec << tag << (tag > 999 ? "\t(" : "\t\t(")
  31. << std::hex << tag << ")\n";
  32. }
  33.  
  34. int main() {
  35. Addr addr = 0xFFFFFFFF;
  36. addr.output();
  37. std::cout << std::hex << static_cast<uint32_t>(addr) << std::endl;
  38.  
  39. std::cout << "And now, a couple changes...\n";
  40. addr.offset = 0x1A;
  41. addr.index = 0x32F;
  42. addr.output();
  43. std::cout << std::hex << static_cast<uint32_t>(addr) << std::endl;
  44.  
  45. std::cout << "Once more...\n";
  46. addr = 0x12345678;
  47. addr.output();
  48. std::cout << std::hex << static_cast<uint32_t>(addr) << std::endl;
  49.  
  50. std::cout << "And finally...\n";
  51. addr = 0x00000001;
  52. addr.output();
  53. std::cout << std::hex << static_cast<uint32_t>(addr) << std::endl;
  54. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Offset: 63		(3f)
Index:  1023   	(3ff)
Tag:    65535	(ffff)
ffffffff
And now, a couple changes...
Offset: 26		(1a)
Index:  815   	(32f)
Tag:    65535	(ffff)
ffffcbda
Once more...
Offset: 56		(38)
Index:  345   	(159)
Tag:    4660	(1234)
12345678
And finally...
Offset: 1		(1)
Index:  0   	(0)
Tag:    0		(0)
1