>>和>>>都是移位操作符,移位操作符只能处理整数类型。
先来谈谈int类型吧。int类型占4个字节(一共32位),第一位表示符号位,其它位为数值为。那么它最大能表示2^31(2147483648) ,-2^31(即-2147483648)。

为什么最小值不是-2^31-1
而是-2^31(即-2147483648)

负数在计算机中以补码表示(符号位不变,原码取反再加1),因此10000000 00000000 00000000 00000000表示最大的负数,为-2^31。

在计算机中
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Integer.toBinaryString(-2147483648)); //10000000000000000000000000000000
System.out.println(Integer.toBinaryString(-1)); // 11111111111111111111111111111111

移位操作符有以下几种(注意:移位时符号位也会跟着移动):

左移位操作符<<,低位补0
有符号右移位>>,使用符号扩展:若符号为正,则在高位插入0;若符号为负,则在高位插入1
无符号右移位>>>,使用零扩展:无论正负,都在高位插入0
如果对char、byte、short类型移位,则在移位前,它们会被转成int类型,且返回值也是int类型;如果对long类型移位,则返回值也是long。

int a = (-1)>>2;
int b = (-1)>>>2;

System.out.println(a); // -1
System.out.println(Integer.toBinaryString(a)); // 11111111111111111111111111111111
System.out.println(b); // 1073741823
System.out.println(Integer.toBinaryString(b)); // 111111111111111111111111111111

来源:Java中>>和>>>的区别_xy631739211的博客-CSDN博客_java>>