Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The >>= (bitwise right shift assignment) operator is a compound assignment operator that shifts the binary representation of its evaluated left operand to the right by the number of bit positions specified by its right operand, and subsequently assigns the resulting value back to the left operand.

Syntax

left_operand >>= right_operand;
This operation is semantically equivalent to:
left_operand = left_operand >> right_operand;
Note: The critical distinction is that in the compound assignment (>>=), the left_operand is evaluated exactly once. This is strictly enforced to prevent multiple evaluations of side effects (e.g., arr[i++] >>= 2;).

Technical Mechanics

Operand Constraints and Result Type: Both operands must be of integer types. Before the shift operation occurs, both operands undergo standard integer promotions. However, unlike the standalone >> operator (where the result type is the promoted type of the left operand), the type of the resulting >>= assignment expression is the unqualified type of the left operand (its original type prior to integer promotion). For example, if c is a uint8_t, the expression c >>= 2 yields a uint8_t, whereas c >> 2 yields an int. Integer Promotion and Bit Filling Behavior: The bits vacated at the most significant bit (MSB) positions are filled based on the type and value of the left operand after integer promotion:
  1. Promoted Unsigned Types: If the promoted left operand has an unsigned type, a logical right shift is performed. The vacated MSBs are strictly filled with 0s.
  2. Promoted Signed Types (Non-negative): If the promoted left operand has a signed type and a positive value (or zero), the vacated MSBs are filled with 0s. Crucially, narrow unsigned types (such as uint8_t or unsigned short) are typically promoted to signed int. Because their promoted values are guaranteed to be positive, the arithmetic shift fills the vacated bits with 0s, effectively mirroring the behavior of a logical shift.
  3. Promoted Signed Types (Negative): If the promoted left operand has a signed type and a negative value, the behavior is implementation-defined. Virtually all modern C compilers implement this as an arithmetic right shift (sign extension), where the vacated MSBs are filled with 1s to preserve the negative sign.

Undefined Behavior (UB)

The >>= operator will invoke undefined behavior under the following conditions:
  • The right_operand is strictly negative.
  • The right_operand is greater than or equal to the width (in bits) of the promoted left_operand. For example, shifting a 16-bit short right by 16 positions is valid if it promotes to a 32-bit int, but shifting a 32-bit int right by 32 or more positions is UB.

Execution Example

#include <stdio.h>
#include <stdint.h>

int main() {
    // 1. Narrow unsigned operation (Promoted to signed int, positive value)
    uint8_t u_val = 200;      // Binary: 11001000
    u_val >>= 2;              // Promoted to int: 200 >> 2 = 50.
                              // Cast back to uint8_t. Result: 00110010
                              // Expression type remains uint8_t.

    // 2. Signed operation (Implementation-defined, typically Sign Extension)
    int8_t s_val = -56;       // Binary: 11001000 (Two's complement)
    s_val >>= 2;              // Promoted to int: -56 >> 2 = -14.
                              // Cast back to int8_t. Result: 11110010
                              // Vacated MSBs filled with 1.

    // 3. Evaluation of side effects
    int arr[] = {16, 32, 64};
    int i = 1;
    arr[i++] >>= 2;           // arr[1] becomes 8. 'i' is evaluated and incremented exactly once.
    
    return 0;
}
Master C with Deep Grasping Methodology!Learn More