出于某种原因,我在 StackOverflow 上找不到这个问题的任何答案。如果已经回答,请指点我重复的帖子。谢谢!
我有一个设备,它对信号进行增量编码并将 2 个 4 位增量打包到一个byte. 我试图通过将 的高 4 位和低 4 位byte分成两个单独的带符号的ints.
例如:
byte packedByte = (byte) 0x0FF; // should represent the value -1, -1 in twos complement
int upper = packedByte >> 4; // upper byte is easy, prints -1
/*
* A few goes at getting the lower 4 bits:
*/
int lower = packedByte & 0x0F; // sign is not extended, prints 15
lower = (packedByte << 4) >> 4; // doesn't work for positive values
lower = (byte)((byte)(packedByte << 4) >>> 4); // works, but this feels slow
有任何想法吗?
温温酱
相关分类