Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Left-shift operator <<

The << operator shifts bits left. Learn integer promotions, shift-count limits, and the differences between signed and unsigned operands.

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

Syntax

value << count

value << count shifts bits left, filling vacated positions with zeros. It does not modify value.

Both operands must have integer type and undergo integer promotions. The result has the promoted left operand's type. count must be nonnegative and smaller than that type's width; otherwise, behavior is undefined.

For an unsigned result type, multiply the left operand's value by 2 raised to the power count, then reduce the product modulo one more than the maximum value of the result type. For a signed result type, the left operand must be nonnegative and the product representable in the result type; otherwise, behavior is undefined.

In the example, 3u << 2 produces 12u; value remains 3u.

Examples

#include <stdio.h>

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

Result

3 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