java右划线转移,java位操作>>>转移

Why if

int x = -1 // binary: 11111111111111111111111111111111

x = x >>> 31;

we have 00000000000000000000000000000001

but if

int x = -1

x = x >>> 32;

we have 11111111111111111111111111111111 (again -1)

but not 00000000000000000000000000000000 ?

解决方案

If the promoted type of the left-hand operand is int, only the five

lowest-order bits of the right-hand operand are used as the shift

distance. It is as if the right-hand operand were subjected to a

bitwise logical AND operator & (§15.22.1) with the mask value 0x1f

(0b11111). The shift distance actually used is therefore always in the

range 0 to 31, inclusive.

Emphasis mine. So:

x >>> n

is equivalent to:

x >>> n & 0x1f // or x >>> n % 32

So, x >>> 32 is equivalent to x >>> 32 & 0x1f <==> x >>> 0 == x.

So the Rule of Thumb is, whenever you shift a number by a multiple of 32(int is 32 bits), you get back the same value.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值