unsigned right Shift'>>>' Java中的运算符 [英] unsigned right Shift '>>>' Operator in Java

查看:147
本文介绍了unsigned right Shift'>>>' Java中的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为什么(-1>>> 32)= -1?

无符号右移运算符在最左边插入一个0。所以当我这样做时

The unsigned right shift operator inserts a 0 in the leftmost. So when I do

System.out.println(Integer.toBinaryString(-1>>>30))



输出



output

11

因此,它在最左边的位插入0。

Hence, it is inserting 0 in the left most bit.

System.out.println(Integer.toBinaryString(-1>>>32))



输出



output

11111111111111111111111111111111

不应该是0吗?

推荐答案

请参阅 http:// docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19

如果是推广类型的左侧操作数是int,只有右侧操作数的五个最低位用作移位距离。就好像右手操作数受到按位逻辑AND运算符& (§15.22.1),掩码值为0x1f(0b11111)。因此,实际使用的移位距离始终在0到31的范围内,包括0和31。

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.

-1>>> 32 相当于 -1>>> 0 -1>>> 33 相当于 -1>>> 1
,尤其令人困惑, -1>>> -1 相当于 -1>>> 31

that is -1 >>> 32 is equivalent to -1 >>> 0 and -1 >>> 33 is equivalent to -1 >>> 1 and, especially confusing, -1 >>> -1 is equivalent to -1 >>> 31

这篇关于unsigned right Shift'>>>' Java中的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆