在Java中使用字节进行位移的奇怪行为 [英] Weird behaviour of bit-shifting with byte in Java
问题描述
由于我在字节
上使用位移,我注意到在使用无符号右移时我得到了奇怪的结果(>>> ;
)。使用 int
,右移(签名:>>
和无符号:> >>
)按预期行事:
As I was using bit-shifting on byte
, I notice I was getting weird results when using unsigned right shift (>>>
). With int
, both right shift (signed:>>
and unsigned:>>>
) behave as expected:
int min1 = Integer.MIN_VALUE>>31; //min1 = -1
int min2 = Integer.MIN_VALUE>>>31; //min2 = 1
但是当我用字节做同样的事情
,无符号右移发生奇怪的事情:
But when I do the same with byte
, strange things happen with unsigned right shift:
byte b1 = Byte.MIN_VALUE; //b1 = -128
b1 >>= 7; //b1 = -1
byte b2 = Byte.MIN_VALUE; //b2 = -128
b2 >>>= 7; //b2 = -1; NOT 1!
b2 >>>= 8; //b2 = -1; NOT 0!
我认为可能是编译器正在转换字节在内部code>到
int
,但似乎不足以解释这种行为。
I figured that it could be that the compiler is converting the byte
to int
internally, but does not seem quite sufficient to explain that behaviour.
为什么在Java中字节的位移表现如此?
推荐答案
这恰好是因为 byte
是提升到 int
之前执行按位运算。 int -128
表示为:
This happens exactly because byte
is promoted to int
prior performing bitwise operations. int -128
is presented as:
11111111 11111111 11111111 10000000
因此,向右移动到7或8位仍然会留下第7位1,因此结果是缩小为负数 byte
value。
Thus, shifting right to 7 or 8 bits still leaves 7-th bit 1, so result is narrowed to negative byte
value.
比较:
System.out.println((byte) (b >>> 7)); // -1
System.out.println((byte) ((b & 0xFF) >>> 7)); // 1
按 b& 0xFF
,所有最高位在移位前被清除,因此结果按预期产生。
By b & 0xFF
, all highest bits are cleared prior shift, so result is produced as expected.
这篇关于在Java中使用字节进行位移的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!