fork download
  1. #include <iostream>
  2. #include <cstdint>
  3. using namespace std;
  4.  
  5. int32_t encode(int32_t a, int32_t b, int32_t c, int32_t d) {
  6. return (a << 12) + (b << 8) + (c << 4) + d;
  7. }
  8.  
  9. void decode(int32_t value, int32_t *result) {
  10. const int32_t mask = 0xf;
  11. result[0] = (value >> 12) & mask; // a
  12. result[1] = (value >> 8) & mask; // b
  13. result[2] = (value >> 4) & mask; // c
  14. result[3] = value & mask; // d
  15. }
  16.  
  17. int main() {
  18. int32_t result[] = {0, 0, 0, 0};
  19.  
  20. int32_t encoded = encode(6, 4, 2, 9);
  21. decode(encoded, result);
  22.  
  23. cout << "a: " << result[0] << endl
  24. << "b: " << result[1] << endl
  25. << "c: " << result[2] << endl
  26. << "d: " << result[3] << endl;
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
a: 6
b: 4
c: 2
d: 9