JAVA displacement pit

The difference between >> and >>> in Java

>>: Move right with sign. Positive numbers are shifted to the right by high-order zeros, and negative numbers are shifted to the right by high-order ones. such as:

4 >> 1, the result is 2; -4 >> 1, the result is -2. -2 >> 1, the result is -1.

>>>: Move right without sign. Regardless of whether it is positive or negative, the upper bits are filled with 0s.

For positive numbers, there is no difference between >> and >>>.

For negative numbers, -2 >>> 1, the result is 2147483647 (Integer.MAX_VALUE), -1 >>> 1, the result is 2147483647 (Integer.MAX_VALUE).

So, to determine whether two numbers have the same sign, you can do this:

return ((a >> 31) ^ (b >> 31)) == 0;

 

Transfer from the Internet, invade and delete

Guess you like

Origin www.cnblogs.com/ZJPaang/p/12726645.html