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

# Python Right Shift

The `>>` operator is the bitwise right shift operator. It shifts the binary representation of an integer to the right by a specified number of bit positions. Because Python integers have arbitrary precision and are conceptually represented using an infinite-width two's complement format, the `>>` operator performs an *arithmetic* right shift, meaning it preserves the sign bit via sign extension.

## Syntax

```python theme={"dark"}
result = x >> y
```

* **`x` (Left Operand):** The integer whose bits will be shifted.
* **`y` (Right Operand):** The number of bit positions to shift `x` to the right. This must be a non-negative integer.

## Mathematical Equivalence

The operation `x >> y` is strictly equivalent to the floor division of `x` by $2^y$.

```python theme={"dark"}
x >> y == x // (2 ** y)
```

## Mechanical Behavior

### 1. Positive Integers

When shifting a positive integer, the rightmost `y` bits are discarded, and `y` number of `0` bits are conceptually shifted in from the left.

```python theme={"dark"}
x = 22      # Binary: ...0001 0110
y = 2
    
result = x >> y  # Binary: ...0000 0101 (Decimal: 5)
```

### 2. Negative Integers

When shifting a negative integer, Python maintains the two's complement sign. The rightmost `y` bits are discarded, and `y` number of `1` bits are shifted in from the left to preserve the negative value. Because the operation equates to floor division, shifting a negative number rounds towards negative infinity.

```python theme={"dark"}
x = -22     # Binary: ...1110 1010
y = 2
    
result = x >> y  # Binary: ...1111 1010 (Decimal: -6)

# Mathematical equivalent: -22 // (2 ** 2) == -22 // 4 == -6
```

### 3. Invalid Operands

The right operand (`y`) must be greater than or equal to zero. Attempting to shift by a negative number results in a `ValueError`.

```python theme={"dark"}
x = 10
result = x >> -1  # Raises ValueError: negative shift count
```

If either operand is not an integer, Python will attempt to invoke the `__rshift__` (on the left operand) or `__rrshift__` (on the right operand) dunder methods. If the types do not implement these methods for the given operands, Python raises a `TypeError`.

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