首页 > 开发 > JAVA > 正文

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

2017-09-07 09:23:59  来源:网友分享

大家好,最近在看《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,非常不错!

解决方案

一看到你的问题,我也不理解这句话。所以我写了一个简单的程序来看位移操作用的是什么 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.println:(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”更明确地表示是右手边的操作数。