Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Bitwise AND assignment operator (&=)

The &= operator stores an AND result in its left operand: bitwise for integral values, logical for boolean values.

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

Syntax

variable &= expression;

variable &= expression combines & with assignment. For integral types, a result bit is 1 only when both corresponding bits are 1. A mask can clear selected bits.

For a left operand of type T, the rule is E1 = (T) ((E1) & (E2)), except that E1 is evaluated only once. Implicit conversion permits storing the result back into byte, short, or char. Floating-point types are not allowed.

For boolean, it performs logical AND without short-circuiting: a false left value does not skip the right operand, unlike &&. Java has no &&= operator.

Examples

public class Main {
    public static void main(String[] args) {
        byte flags = 0b1101;
        flags &= 0b1010;
        System.out.println(flags);

        boolean allowed = false;
        int checks = 0;
        allowed &= (++checks > 0);
        System.out.println(allowed);
        System.out.println(checks);
    }
}

Result

8
false
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