Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Left-shift assignment (<<=)

The <<= operator shifts an integer value left and stores the result in its left operand.

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

Syntax

lhs <<= count

The <<= operator requires a modifiable lvalue on the left and integer operands. With a non-atomic left operand, lhs <<= count behaves like lhs = lhs << (count), except that lhs is evaluated only once.

With an atomic left operand, lhs <<= count is a single sequentially consistent atomic read-modify-write operation (memory_order_seq_cst). In contrast, lhs = lhs << (count) performs a separate atomic load and atomic store, so it can lose an update made by another thread between the two operations.

Integer promotions precede shifting; the result is converted to lhs's type. Vacated bits become zeros. A negative count, or a count at least as large as the promoted left operand's width, causes undefined behavior.

For a promoted unsigned type, the result is reduced modulo the number of values that type can represent. For a promoted signed type, the value must be nonnegative and the product by 2^count must be representable in that type; otherwise behavior is undefined. An unsigned variable can still promote to int.

Examples

#include <stdio.h>

int main(void)
{
    unsigned int value = 3u;
    value <<= 2;
    printf("%u\n", value);
    return 0;
}

Result

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