I have two bytes containing a 14-bit left-justified two's complement value, and I need to convert it to a signed short value (ranging from -8192 to +8191, I guess?)
What would be the fastest way to do that?
Oliver Charlesworth
Simply divide by 4.
(Note, right-shift leads to implementation/undefined behaviour.)
A portable solution:
short convert(unsigned char hi, unsigned char lo)
{
int s = (hi << 6) | (lo >> 2);
if (s >= 8192)
s -= 16384;
return s;
}
来源:https://stackoverflow.com/questions/14710764/14-bit-left-justified-twos-complement-to-a-signed-short