Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Bitwise AND assignment (&=)

The &= operator stores a bitwise AND result in its left operand, retaining only bits set in both operands.

Version: C23; repository compiler flag, production runtime not attested

Syntax

left &= right

left &= right evaluates the left operand only once. If the left operand is non-atomic, it otherwise acts like left = left & (right). Both operands must have integer types; the left must be a modifiable lvalue. Integer promotions and usual arithmetic conversions apply, and the result is converted to the left operand's type.

If the left operand has an atomic type, &= performs a single sequentially consistent atomic read-modify-write. By contrast, left = left & (right) performs a separate atomic load and atomic store, so it can lose an update that occurs between them.

A result bit is set only when both corresponding bits are set. This is bitwise, not logical &&. Here, 13u (binary 1101) and 6u (0110) produce 4u (0100).

Examples

#include <stdio.h>

int main(void)
{
    unsigned int flags = 13u;
    flags &= 6u;
    printf("%u\n", flags);
    return 0;
}

Result

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