why is 1>>32 == 1?

前端 未结 2 682
灰色年华
灰色年华 2020-11-27 18:40

I\'m wondering if perhaps this is a JVM bug?

java version \"1.6.0_0\" OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu13) OpenJDK 64-Bit Server VM (b

相关标签:
2条回答
  • 2020-11-27 19:25

    http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22.1

    15.19 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. The shift distance actually used is therefore always in the range 0 to 31, inclusive.

    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.

    (emphasis mine)

    0 讨论(0)
  • 2020-11-27 19:32

    It's not a bug. In n >> m, it only looks at the last five bits of m - so any number greater than 31 will be reduced to that number mod 32. So, (256 >> 37) == 8 is true.

    Edit: This is true if you're working with ints. If it's longs, then it looks at the last six bits of m, or mods by 64.

    0 讨论(0)
提交回复
热议问题