why is 1>>32 == 1?

前端 未结 2 698
灰色年华
灰色年华 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)

提交回复
热议问题