就是Java位移循环? [英] Is java bit shifting circular?

查看:154
本文介绍了就是Java位移循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用Java这种行为:

I have this behavior using Java:

int b=16;
System.out.println(b<<30);
System.out.println(b<<31);
System.out.println(b<<32);
System.out.println(b<<33);

输出:
0
0
16
32

output: 0 0 16 32

就是Java移位循环?如果不是,为什么我得到0时,B&LT;小于30和16时,B&LT;&LT; 32

Is java bit shift circular? IF not, why I get 0 when b<<30 and 16 when b<<32?

推荐答案

位转移是不是圆的;对于位移 INT S,Java只使用了5最低显著位,这样,(B&LT;&小于0)等同于(b&LT;&LT; 32)(等同于(b&LT;&LT; 64)等)。你可以简单的把位移量和取余数除以32时。

Bit shifting is not circular; for bit-shifting ints, Java only uses the 5 least-significant bits, so that (b << 0) is equivalent to (b << 32) (is equivalent to (b << 64), etc.). You can simply take the bit-shifting amount and take the remainder when dividing by 32.

同样的事情也发生了位移 s,其中Ja​​va只使用了6最低显著位,这样,(沿&LT;&LT 0)等同于(沿&LT;&LT; 64)

Something similar occurs for bit-shifting longs, where Java only uses the 6 least-significant bits, so that (aLong << 0) is equivalent to (aLong << 64).

的JLS <第15.19 / A>关于本讲座:

Section 15.19 of the JLS talks about this:

如果左侧操作数的提升的类型为int,只有五个最低阶位右边的操作数作为移动距离。这是因为如果右手操作体进行按位逻辑与运算放大器&; (§15.22.1)与屏蔽值0x1F的(0b11111)。因此实际所用的偏移距离总是在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.

如果左侧操作数的提升的类型很长,然后点击只有六个最低阶位右手操作数的被用作移动距离。这是因为如果右手操作体进行按位逻辑与运算放大器&; (§15.22.1)与屏蔽值的0x3F(0b111111)。因此实际所用的偏移距离总是在范围为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 (0b111111). The shift distance actually used is therefore always in the range 0 to 63, inclusive.

(重点煤矿)

(你不能位移浮动 s或双击 S,并试图位移字节将受到该值的一元数值提升的一个 INT 反正。)

(You can't bit-shift floats or doubles, and attempting to bit-shift a short or a byte would be subject the value to unary numeric promotion to an int anyway.)

您获得 0 16 LT;&LT; 30 ,因为 16

00000000 00000000 00000000 00010000

被移出的 INT 末被丢弃。

// Discarded - Result-----------------------------
  (00000100)   00000000 00000000 00000000 00000000 

这篇关于就是Java位移循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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