gpt4 book ai didi

java - 为什么3>>32等于3?

转载 作者:太空宇宙 更新时间:2023-11-04 02:54:40 25 4
gpt4 key购买 nike

why 3 right shift 32 is equal to 3 and not 0.

我在 NodeJs 和 Java 中得到了这些结果

  • 3 >> 31 = 0
  • 3 >> 32 = 3
  • 3 >> 33 = 1
  • 3 >> 34 = 0
  • 3 >> 35 = 0

最佳答案

这是 Java 语言规范的一部分。右侧操作数被换行,使其始终位于 0 - bits 范围内,其中 bits 是左侧操作数的位数。由于您要移位 32 位整数,因此右侧操作数会包含在 0 到 31 之间。32 变为 0,33 变为 1 等。

请参阅Java language specification for shift operators :

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.

关于java - 为什么3>>32等于3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56356415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com