> ## 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 Left Shift

The `<<` operator is the bitwise left shift operator in Python. It shifts the binary representation of an integer to the left by a specified number of bit positions, appending zeros to the least significant bits (the right side). Mathematically, evaluating `x << y` is strictly equivalent to multiplying `x` by 2 raised to the power of `y` ($x \times 2^y$).

## Syntax

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

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

## Technical Mechanics

1. **Binary Translation:** Python evaluates the binary equivalent of the base integer `x`.
2. **Bit Manipulation:** It moves every bit `y` steps to the left.
3. **Zero Padding:** It inserts `y` number of `0` bits at the rightmost end of the binary sequence.
4. **Arbitrary Precision and Limits:** Unlike languages with fixed-width integers (e.g., C or Java), Python integers have arbitrary precision and do not wrap around. However, CPython enforces a maximum integer size. Shifting by an excessively large amount (e.g., `1 << 10**19`) will raise an `OverflowError` (`Python int too large to convert to C ssize_t`), and shifting to a size that exceeds available system RAM will raise a `MemoryError`.
5. **Negative Shift Counts:** Attempting to shift by a negative number (`y < 0`) is an illegal operation and will raise a `ValueError`.
6. **Operand Types:** Both operands must be integers. Passing floats or strings will raise a `TypeError`.

## Operator Precedence

The `<<` operator has lower precedence than arithmetic operators such as addition (`+`) and subtraction (`-`), but higher precedence than bitwise AND (`&`), OR (`|`), and XOR (`^`).

Because of this, an expression like `x << y + 1` evaluates as `x << (y + 1)`, not `(x << y) + 1`. Parentheses must be used to explicitly define the desired evaluation order if the shift operation should occur before arithmetic.

## Execution Examples

**Basic Left Shift**

```python theme={"dark"}
x = 5      # Binary: 0b101
y = 3
    
result = x << y  # Binary: 0b101000 (Decimal: 40)
```

**Visualizing the Bit Shift**
Using Python's built-in `bin()` function demonstrates the exact bit manipulation:

```python theme={"dark"}
x = 11
print(bin(x))        # Output: 0b1011
    
shifted = x << 2
print(bin(shifted))  # Output: 0b101100 (Two zeros appended to the right)
```

**Operator Precedence**

```python theme={"dark"}
x = 2
y = 3


# Evaluates as 2 << (3 + 1) -> 2 << 4
print(x << y + 1)    # Output: 32


# Evaluates as (2 << 3) + 1 -> 16 + 1
print((x << y) + 1)  # Output: 17
```

**Mathematical Equivalence**

```python theme={"dark"}
x = 7
y = 4
    

# Bitwise left shift
shift_result = x << y
    

# Mathematical equivalent: x * (2 ** y)
math_result = x * (2 ** y)
    
print(shift_result == math_result)  # Output: True (112 == 112)
```

**Error Handling for Negative Shifts**

```python theme={"dark"}
try:
    10 << -1
except ValueError as e:
    print(e)  # Output: negative shift count
```

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