Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有两个字节包含一个 14 位左对齐二进制补码值,我需要将其转换为有符号短值(我猜是从 -8192 到 +8191?)
最快的方法是什么?
只需除以 4。
(注意,右移会导致实现/未定义的行为。)
便携式解决方案:
short convert(unsigned char hi, unsigned char lo) { int s = (hi << 6) | (lo >> 2); if (s >= 8192) s -= 16384; return s; }