> ## 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.

# C Right Shift

The `>>` operator in C is the bitwise right shift operator. It shifts the binary representation of its promoted left operand to the right by the number of bit positions specified by its right operand. Bits shifted off the least significant end are discarded.

```c theme={"dark"}
result = left_operand >> right_operand;
```

## Integer Promotion and Return Type

Before the shift operation occurs, both operands undergo standard integer promotions:

* Types smaller than `int` (such as `char` and `short`) are promoted to `int` (or `unsigned int` if the value cannot fit in an `int`).
* The return type of the `>>` operation is exactly the type of the **promoted left operand**. The type or value of the right operand does not affect the return type.

## Mechanics and Bit Filling

When bits are shifted to the right, vacated bit positions are created at the most significant bit (MSB) end. How C fills these vacated bits depends strictly on the **promoted type** of the `left_operand`:

* **Unsigned Promoted Types (Logical Shift):** If the *promoted* left operand has an unsigned type, the operator performs a logical right shift. The vacated MSBs are unconditionally filled with zeros (`0`).
* **Signed Promoted Types (Arithmetic vs. Logical Shift):** If the *promoted* left operand has a signed type, the behavior depends on its value:
  * **Non-negative values:** The vacated MSBs are filled with zeros (`0`).
  * **Negative values:** The behavior is **implementation-defined**. Most modern C compilers (such as GCC and Clang on x86/ARM architectures) perform an arithmetic right shift, filling the vacated bits with the sign bit (`1`) to preserve the two's complement sign. However, the C standard permits compilers to perform a logical shift (filling with `0`) instead.

## Syntax and Behavior Visualization

```c theme={"dark"}
#include <stdint.h>

// 1. Unsigned Right Shift (Logical Shift)
// uint32_t promotes to itself (or unsigned int), remaining unsigned.
uint32_t val_unsigned = 3221225472U; 
// Binary: 11000000 00000000 00000000 00000000

uint32_t res_unsigned = val_unsigned >> 2; 
// Binary: 00110000 00000000 00000000 00000000 (Vacated bits filled with 0)


// 2. Signed Right Shift (Implementation-Defined)
// int32_t promotes to itself (or int), remaining signed.
int32_t val_signed = -1073741824;    
// Binary: 11000000 00000000 00000000 00000000 (Two's complement)

int32_t res_signed = val_signed >> 2;  
// Binary: 11110000 00000000 00000000 00000000 (Typical: filled with sign bit 1)
```

## Undefined Behavior (UB) Constraints

The C standard dictates that the `>>` operator invokes Undefined Behavior if any of the following conditions are met regarding the `right_operand`:

1. **Negative Shift Amount:** The right operand is less than zero.

```c theme={"dark"}
int x = 10 >> -1; // Undefined Behavior
```

2. **Oversized Shift Amount:** The right operand is greater than or equal to the width (in bits) of the *promoted* left operand.

```c theme={"dark"}
uint32_t y = 15;
uint32_t z = y >> 32; // Undefined Behavior (32 is >= width of uint32_t)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
