Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Unsigned right shift assignment (>>>=)

The >>>= operator shifts bits right, fills vacated positions with zeros, and stores the result in the same variable. Unlike >>=, it does not sign-extend; promotion and conversion back matter for narrow types.

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

Syntax

variable >>>= distance;

variable >>>= distance combines the >>> shift with assignment. It fills vacated positions with zeros; >>= sign-extends. Plain >>> does not modify the variable.

For a variable of type T, it corresponds to variable = (T) (variable >>> distance), but evaluates the left side only once. Operands must have integral types after promotion: byte, short, and char promote to int, and the result is converted back.

The distance is distance & 31 for int or distance & 63 for long. Thus, shifting an int by 32 changes nothing. “Unsigned” describes zero-filling, not the result type: conversion back to byte can produce a negative value again.

Examples

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

        int signed = -8;
        signed >>= 1;
        System.out.println(signed);

        byte small = -8;
        small >>>= 1;
        System.out.println(small);
    }
}

Result

2147483644
-4
-4

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