Java中的移位

来源:互联网 发布:淘宝卖什么销量最好 编辑:程序博客网 时间:2024/05/16 05:35
Java中的移位,对 int 3 进行移位:
3本身:0000 0000 0000 0000 0000 0000 0000 0011(没疑问)
3<<30: 1100 0000 0000 0000 0000 0000 0000 0000(没疑问)
3<<31: 1000 0000 0000 0000 0000 0000 0000 0000(没疑问)

3<<32: 0000 0000 0000 0000 0000 0000 0000 0011(奇怪啊,最后四位应该是 0000 才对啊)

最后面的问题搞不定,谁能解释下啊

=========================================================================

找到答案了:

Java™ Language Specification, Third Edition -- 15.19. Shift Operators

If the promoted type of the left-hand operand is int, only thefive 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 theright-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.



原创粉丝点击