fork download
  1. #include <cstdint>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. struct Q_23_8
  7. {
  8. int32_t fractional : 8;
  9. int32_t integral : 23;
  10. };
  11.  
  12. static_assert(sizeof(Q_23_8) == sizeof(int32_t), "Sizes differ!");
  13.  
  14. Q_23_8 operator*(const Q_23_8& lhs, const Q_23_8& rhs)
  15. {
  16. // Math is incorrect here, of course!
  17. return {
  18. lhs.fractional * rhs.fractional,
  19. lhs.integral * rhs.integral };
  20. }
  21.  
  22. int main()
  23. {
  24. int32_t encoded[2] = { (321 << 8) + 11, 0 };
  25. printf("encoded = %08x\n", encoded[0]);
  26.  
  27. Q_23_8* decoded = reinterpret_cast<Q_23_8*>(encoded);
  28. decoded[1] = decoded[0] * decoded[0];
  29. printf("decoded = %06x.%02x\n", decoded[0].integral, decoded[0].fractional);
  30. printf("result = %06x.%02x\n", decoded[1].integral, decoded[1].fractional);
  31. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
encoded = 0001410b
decoded = 000141.0b
result  = 019281.79