为什么1>> 32 == 1? [英] why is 1>>32 == 1?

查看:113
本文介绍了为什么1>> 32 == 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果这也许是一个JVM错误?

I'm wondering if perhaps this is a JVM bug?

Java版本1.6.0_0
OpenJDK的运行时环境(IcedTea6 1.4.1)(6b14-1.4.1-0ubuntu13)
OpenJDK的64位服务器VM(建14.0 B08,混合模式)

java version "1.6.0_0" OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu13) OpenJDK 64-Bit Server VM (build 14.0-b08, mixed mode)

class Tmp {
    public static void main(String[] args) {
        System.out.println("1>>1 = "+(1>>1));
        System.out.println("1>>2 = "+(1>>2));
        System.out.println("1>>31 = "+(1>>31));
        System.out.println("1>>32 = "+(1>>32));
        System.out.println("1>>33 = "+(1>>33));
    }
}

产生这样的,当我运行它:

produces this when I run it:

1>>1 = 0
1>>2 = 0
1>>31 = 0
1>>32 = 1 <---------- should be 0 i think
1>>33 = 0

我也得到相同的结果对于任何32多个。

I also get the same results for any multiple of 32.

我需要写我自己的右移来检查吗?

do I need to write my own right-shift to check for this?

推荐答案

<一个href=\"http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.1\">http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.1

15.19移位操作符

15.19 Shift Operators

如果左侧操作数的提升类型的 INT 只有五个最低阶的右手操作数位被用作移动距离。这是因为如果右手操作体进行按位逻辑AND运算符&安培; (§15.22.1)与屏蔽值0x1F的。因此实际所用的偏移距离总是在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. The shift distance actually used is therefore always in the range 0 to 31, inclusive.

如果左侧操作数升级后的类型为 只有右侧操作数的六个最低阶位被用作移位距离即可。这是因为如果右手操作体进行按位逻辑与运算放大器&; (§15.22.1)与在屏蔽值的0x3F 。因此实际所用的偏移距离总是在范围为0〜63(含)。

If the promoted type of the left-hand operand is long, then only the six 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 0x3f. The shift distance actually used is therefore always in the range 0 to 63, inclusive.

(重点煤矿)

这篇关于为什么1&GT;&GT; 32 == 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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