java 鄙视位移题,在Java中使用字节进行位移的奇怪行为

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!

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.

Why is bit-shifting behaving that way with byte in Java?

解决方案

This happens exactly because byte is promoted to int prior performing bitwise operations. int -128 is presented as:

11111111 11111111 11111111 10000000

Thus, shifting right to 7 or 8 bits still leaves 7-th bit 1, so result is narrowed to negative byte value.

Compare:

System.out.println((byte) (b >>> 7)); // -1

System.out.println((byte) ((b & 0xFF) >>> 7)); // 1

By b & 0xFF, all highest bits are cleared prior shift, so result is produced as expected.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值