CKTIS / Java
Unsigned right shift operator (>>>)
The >>> operator shifts bits right and fills vacated positions on the left with zeros. Learn how it differs from >>, how operands are promoted, and how shift distances are masked.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
value >>> distancevalue >>> distance shifts bits right, discarding bits shifted out and inserting zeros on the left. In contrast, >> replicates the sign bit. “Unsigned” describes the operation, not a new result type.
Both operands undergo unary numeric promotion separately and must then have primitive integral types. byte, short, and char become int. The result has the promoted left operand's type: int or long; a long right operand does not change that.
The distance is masked: with & 31 for int, or & 63 for long. Thus, value >>> 32 does not shift an int at all. An effective zero distance also preserves a negative value, so >>> does not guarantee a nonnegative result.
The example compares >>> with >> for -8 and demonstrates masking and the long variant.
Examples
public class Main {
public static void main(String[] args) {
int value = -8;
System.out.println(value >>> 1);
System.out.println(value >> 1);
System.out.println(value >>> 32);
System.out.println(-8L >>> 1);
}
}Result
2147483644 -4 -8 9223372036854775804
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.