Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Bitwise OR assignment (|=)

The |= operator combines a variable’s value with the right operand using OR and stores the result. It supports integral and boolean values.

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

Syntax

target |= value;

The |= operator performs | and stores the result in the left-hand variable. For integers, it sets each bit present in either operand; a mask sets flags without clearing others.

For variable type T, target |= value corresponds to target = (T) (target | value), but evaluates the left side only once. Numeric promotion precedes the operation; implicit narrowing of the result can discard high bits. It does not support float or double.

For boolean, it performs logical OR without short-circuiting: the right operand is evaluated even when the left value is true, unlike ||. The example demonstrates setting a flag and executing ++checks. The operator does not provide an atomic update of a shared variable.

Examples

public class Main {
    public static void main(String[] args) {
        byte flags = 0b0001;
        flags |= 0b0100;
        System.out.println(flags);

        boolean found = true;
        int checks = 0;
        found |= (++checks > 0);
        System.out.println(found);
        System.out.println(checks);
    }
}

Result

5
true
1

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