试一下
public static void main(String[] args) { System.out.println(Integer.toBinaryString(-16)); System.out.println(Integer.toBinaryString(-16>>2)); System.out.println(Integer.toBinaryString(16)); System.out.println(Integer.toBinaryString(16>>2)); System.out.println(Integer.toBinaryString(-16)); System.out.println(Integer.toBinaryString(-16>>>2)); System.out.println(Integer.toBinaryString(16)); System.out.println(Integer.toBinaryString(16>>>2)); System.out.println(-16>>2); System.out.println(-16>>>2); System.out.println(-15>>2); System.out.println(-15>>>2); System.out.println(15>>2); System.out.println(15>>>2); }
得到结果:
11111111111111111111111111110000
11111111111111111111111111111100
10000
100
11111111111111111111111111110000
111111111111111111111111111100
10000
100
-4
1073741820
-4
1073741820
3
3
结论:
>> :
负数右移用1补位
正数右移用0补位
>>>:
无论正负,都用0补位