Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

Right-shift assignment (>>=)

The >>= operator shifts an integer value right and stores the result in the left operand.

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

Syntax

lhs >>= count

Both operands must have integer types, and lhs must be a modifiable lvalue. If lhs has a non-atomic type, lhs >>= count is equivalent to lhs = lhs >> (count) except that the expression lhs is evaluated only once.

If lhs has an atomic type, lhs >>= count still evaluates the expression lhs only once and performs a single atomic read-modify-write operation with memory_order_seq_cst (sequentially consistent) ordering. In contrast, lhs = lhs >> (count) performs a separate atomic load and atomic store.

After integer promotions, the shift is performed; the result is converted to the type of lhs and stored. For nonnegative values, this divides by 2 raised to count, discarding the fractional part. For negative signed values, the result is implementation-defined, including in C23.

A negative count, or one at least as large as the width of the promoted left operand, causes undefined behavior. The example changes 40u to 5.

Examples

#include <stdio.h>

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

Result

5

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