Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Right-shift assignment >>=

The >>= operator shifts bits right with sign extension and stores the result back in the same variable.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

variable >>= distance;

variable >>= distance combines >> with assignment. Unlike >>>=, it copies the sign bit instead of inserting zeros.

For the variable's type T, it corresponds to variable = (T) (variable >> distance), but evaluates the left side only once. The implicit conversion also permits storing the result in byte, short, or char.

Operands must have integral types after numeric promotion. The left operand is promoted to int or long; the distance uses only the lowest 5 or 6 bits, respectively. Thus shifting an int by 32 leaves it unchanged.

For negative numbers, the result rounds down: -9 >> 1 gives -5, whereas -9 / 2 gives -4.

Examples

public class Main {
    public static void main(String[] args) {
        int value = -9;
        value >>= 1;
        System.out.println(value);

        byte small = 64;
        small >>= 2;
        System.out.println(small);

        int masked = 12;
        masked >>= 32;
        System.out.println(masked);
    }
}

Result

-5
16
12

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources