Search code examples
binarytype-conversionhexbytetwos-complement

Int calculation from byte representation


Given bytes as HEX: 710e00fe I do the following:

int value = (*(int*)([bytes bytes]));
int exp = value >> 24; 
int mantissa = value & 0x00FFFFFF;

And I get mantissa = 3697 and exponent = -2.

I tried doing this manually but I can't figure out how I got those values. I can actually get the exp right, but mantissa is a huge number when I manually calculate. Is there something wrong?


Solution

  • You have a little-endian CPU.
    So, the exponent is the last byte in bytes, i.e. 0xfe == -2 in two's complement.

    And the mantissa is 0x000e71, i.e. 3697.