测试代码:
System.out.println("16 << 1 : " + (16 << 1));
System.out.println("16 >> 3 : " + (16 >> 3));
System.out.println("16 >> 10 : " + (16 >> 10));
System.out.println("1 >> 1 : " + (1 >> 1));
System.out.println("16 >>> 2 : " + (16 >>> 2));
System.out.println("-16 >> 2 : " + (-16 >> 2));
System.out.println("-16 << 2 : " + (-16 << 2));
System.out.println("-16 >>> 2 : " + (-16 >>> 2));
打印结果:
16 << 1 : 32
16 >> 3 : 2
16 >> 10 : 0
1 >> 1 : 0
16 >>> 2 : 4
-16 >> 2 : -4
-16 << 2 : -64
-16 >>> 2 : 1073741820
简单理解:
PS:位移操作涉及二进制、原码、补码、反码相关,可参考:
ConstXiong 备案号:苏ICP备16009629号-3