在Java中,使用bitshifts的时候,为什么1 LT;< 32 = 1<!< 31 LT;< 1? [英] In Java, when using bitshifts, why does 1 << 32 != 1 << 31 << 1?

查看:267
本文介绍了在Java中,使用bitshifts的时候,为什么1 LT;< 32 = 1<!< 31 LT;< 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int类型的= 1<< 32;
INT B = 1&所述;&下; 31 LT;< 1;

为什么一个== 1? b为0如我所料。

Why does a == 1? b is 0 as I expected.

推荐答案

所有的变化都做了整数mod32和多头mod64。

All shifts are done mod 32 for ints and mod 64 for longs.

从规范的部分15.19:

From section 15.19 of the spec:

如果在推动型左侧的
  操作数是 INT ,只有五
  右侧的最低阶位
  操作数被用作移
  距离。这是因为如果右手
  操作数经受逐位
  逻辑与运算放大器&; (§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.

至于为什么的语言是这样设计的 - 我不知道,但是C#有相同的设计决定。这里的注解ECMA C#规范说什么:

As for why the language was designed that way - I don't know, but C# has the same design decision. Here's what the annotated ECMA C# spec says:

C#刻意保持
  实现定义的行为一
  miinimum。它们仅在被接受
  迫使对性能的影响
  统一的行为是太过分了
  (如对某些浮点
  precision问题)。因此,大小
  每个整类型为precisely
  指定,并且该字符集是
  固定统一code。

C# deliberately keeps implementation-defined behaviors to a miinimum. They are accepted only when the performance impact of forcing uniform behavior would be excessive (such as for some floating-point precision issues). Hence, the size of each integral type is precisely specified, and the character set is fixed to Unicode.

有关移位操作,也统一
  被指定的行为。有可能
  使用单个额外取得
  指令(安培; 0x1F的或放大器; 0x3F的)那
  仅招致现代的小成本
  处理器,特别是因为它不
  不能引用的内存。对于不同
  浮点运算,该
  在转变行为的差异会
  戏剧性如果离开的心血来潮
  处理器;而不是一个小
  在precision差异,完全
  不同的积分结果将是
  产生的。

For shift operations, too, uniform behavior is specified. It can be achieved using a single extra instruction (& 0x1F or & 0x3F) that incurs only a tiny cost on modern processors, especially since it does not reference memory. Unlike for floating-point operations, the difference in shift behavior would be dramatic if left to the whim of the processors; rather than a small difference in precision, completely different integral results would be produced.

在做这个决定的committe
  研究参考资料一
  不同的处理器数量
  架构。有一点
  在换挡行为的一致性
  范围-32之外计数.. + 32
  32位操作数,并分别
  -64 .. + 64的64位操作数。

In making this decision the committe studied reference materials for a number of different processor architectures. There is little consistency in the behavior for shift counts outside the range -32..+32 for 32-bit operands, and respectively -64..+64 for 64-bit operands.

(有那么一些例子列表。)

(There's then a list of some examples.)

这似乎是一个完全合理的解释给我。一致性是绝对重要的,如果它是不可能实现的不同的的某些系统的一个性能方法一致的行为,我认为这是一个合理的解决方案。

This seems an entirely reasonable explanation to me. Consistency is definitely important, and if it would be impossible to implement different consistent behaviour in a performant way on some systems, I think this is a reasonable solution.

这篇关于在Java中,使用bitshifts的时候,为什么1 LT;< 32 = 1<!< 31 LT;< 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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