fork(1) download
  1. #include <cstdint>
  2. #include <iostream>
  3.  
  4. int32_t interpret24bitAsInt32(unsigned char* bytes) {
  5. int32_t const number =
  6. (bytes[0] << INT32_C(16))
  7. | (bytes[1] << INT32_C(8))
  8. | bytes[2];
  9. int32_t const correction =
  10. (bytes[0] >> UINT8_C(7)) << INT32_C(24);
  11. return number - correction;
  12. }
  13.  
  14. int main() {
  15. unsigned char n1[] = { 0x80, 0, 0};
  16. std::cout << interpret24bitAsInt32(n1) << std::endl;
  17. unsigned char n2[] = { 0x7F, 0xFF, 0xFF};
  18. std::cout << interpret24bitAsInt32(n2) << std::endl;
  19. }
Success #stdin #stdout 0s 4500KB
stdin
Standard input is empty
stdout
-8388608
8388607