> ## 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++ serves a dual architectural purpose: it is natively the **bitwise right shift operator** for integral types, and it is conventionally overloaded within the C++ Standard Library as the **stream extraction operator** for input streams.

## Bitwise Right Shift Operator

At the hardware level, `>>` shifts the binary representation of its left operand to the right by the number of bit positions specified by its right operand.

```cpp theme={"dark"}
// Syntax
integral_result = left_operand >> right_operand;
```

**Mechanics and Behavior:**

* **Type Promotion:** Both operands undergo integral promotion before the shift operation. The type of the result is the type of the promoted left operand.
* **Unsigned Integers (Logical Shift):** If the left operand is an unsigned type, the operator performs a logical right shift. The vacated bits on the most significant (left) side are filled with zeros.
* **Signed Integers (Arithmetic Shift):** If the left operand is a signed type, the operator performs an arithmetic right shift. The vacated bits are filled with the value of the original sign bit (sign extension). *Note: Prior to C++20, right-shifting a negative signed integer was implementation-defined. As of C++20, C++ mandates two's complement representation, guaranteeing an arithmetic shift.*
* **Undefined Behavior (UB):** The operation invokes UB if the right operand is negative, or if it is greater than or equal to the bit-width of the promoted left operand.

## Stream Extraction Operator

The Standard Library overloads `>>` via `std::istream::operator>>` and non-member functions to facilitate formatted input parsing.

```cpp theme={"dark"}
// Syntax
std::istream& operator>>(std::istream& is, T& variable);
```

**Mechanics and Behavior:**

* **Whitespace Handling:** By default, the operator consumes and discards leading whitespace characters (spaces, tabs, newlines) before attempting to parse the target type. This behavior is governed by the `std::skipws` format flag.
* **State Mutation:** The operator modifies the right operand by writing the parsed value into it. If parsing fails (e.g., encountering non-numeric characters when a numeric type is expected), the right operand is left in a valid but unspecified state (or zeroed out in modern C++), and the stream's `failbit` is set.
* **Return Value:** The operator returns a reference to the left operand (`std::istream&`). This allows for left-to-right operator chaining.

```cpp theme={"dark"}
// Chaining visualization
// Evaluates as: (std::cin >> var1) >> var2;
std::cin >> var1 >> var2; 
```

## Operator Characteristics

* **Associativity:** Left-to-right.
* **Precedence:** Level 7. It evaluates after arithmetic operators (`+`, `-`) but before relational operators (`<`, `>`). This requires careful parenthesization when combining bitwise shifts with arithmetic.
* **Overloading:** When overloading `>>` for user-defined types to support stream extraction, it must be implemented as a non-member function (often a `friend` function) because the left operand is a standard library stream, not the user-defined class.

```cpp theme={"dark"}
// Standard signature for custom type overloading
class CustomType {
    friend std::istream& operator>>(std::istream& is, CustomType& obj) {
        // Extraction logic modifying obj
        return is;
    }
};
```

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