首页 > Java中的整型移位操作,为什么是“只有数值右端的低5位才有用”?

Java中的整型移位操作,为什么是“只有数值右端的低5位才有用”?

大家好,最近在看《Java编程思想》,在第三章“操作符”中有这么一段:

如果对char、byte或者short类型的数值进行移位处理,那么在移位进行之前,它们会被转成int类型,并且得到的结果也是一个int类型的值。只有数值右端的低5位才有用。这样可防止我们移位超过init型值所具有的位数。(译注:因为2的5次方为32,而int型值只有32位。

之后google查到了这篇文章:http://blog.csdn.net/showershow/article/details/6959122,不过还是没懂。

虽然译者做了注解,不过我还是不明白,为什么是“只有数值右端的低5位才有用”?有谁能够解释一下吗?谢谢!

PS. 第一次来这里,发现编辑器竟然支持Markdown,非常不错!


因为int只有32位,<< 32就溢出了呀。所以限制为最右边那5位,以防溢出( 2 ** 5 = 32 )。


一看到你的问题,我也不理解这句话。所以我写了一个简单的程序来看位移操作用的是什么 bytecode :

public class Test {
    public static void main(String[] args) {
        byte b = 32;
        int i = b << 4;
        System.out.println(i);
    }
}

用 javap 查看编译出来的字节码:

  public static void main(java.lang.String[]);
    Code:
       0: bipush        32
       2: istore_1
       3: iload_1
       4: iconst_4
       5: ishl
       6: istore_2
       7: getstatic     #2                  // Field java/lang/System.out:Ljava/
io/PrintStream;
      10: iload_2
      11: invokevirtual #3                  // Method java/io/PrintStream.printl
n:(I)V
      14: return
}

我发现用的是 ishl ,它的解释是这样的:http://cs.au.dk/~mis/dOvs/jvmspec/ref-_ishl.html

Shifts value2 left by the amount indicated in the five low bits of value1

所以我终于明白英文原文里的“right-hand side”指的并不是某个数值的“右端”,“right-hand side”是一个术语,应该翻译成“右操作数”。

Only the five low-order bits of the right-hand side will be used.

这句话可以做这样的理解:位移操作符只用到了它的右操作数的低5位。

我看到这句话的时候就理解成只使用了左操作数的低5位,可能你也是这样理解的。

P.S. 在 wiki 上翻到:http://en.wikipedia.org/wiki/Bitwise_operation#Shifts_in_Java

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

这里使用“right-hand operand”更明确地表示是右手边的操作数。


题主可能理解错了。

a = b << c  

规范中说的 只有数值右端的低5位才有用 说的是 c

我们先看 b,在计算过程中,b 被转换成 int,因为 int 类型是 32 位,也就是 b 的值最多可以移动 31 位。

如果把 b 移动 33 位,只有最后的 5 位有效,于是:

(为了方便测试,一下代码为 js 代码)

50 << 33
// output:100

等价于

50 << (33%32)
// output:100

也就是

50 << 1
// output:100

为了严谨,题主自行在 java 中测试。

【热门文章】
【热门文章】