14-bit left-justified two's complement to a signed short

霸气de小男生 提交于 2019-12-07 17:07:39

问题


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?


回答1:


Simply divide by 4.

(Note, right-shift leads to implementation/undefined behaviour.)




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!