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

# Java Left Shift

The `<<` (left shift) operator is a binary bitwise operator in Java that shifts the two's complement bit pattern of the left operand to the left by the number of positions specified by the right operand.

```java theme={"dark"}
result = leftOperand << rightOperand;
```

## Mechanics

When the `<<` operator is applied:

1. The bits of the `leftOperand` are shifted to the left by the distance of the `rightOperand`.
2. The vacated bit positions on the right are filled with zeros (`0`).
3. The bits shifted off the left boundary (the most significant bits, including the sign bit) are discarded.

Mathematically, shifting an integer left by $n$ positions is equivalent to multiplying that integer by $2^n$, provided that the operation does not result in an overflow that alters the sign bit.

## Type Promotion

Before the shift operation occurs, Java applies unary numeric promotion to the left operand:

* If the left operand is of type `byte`, `short`, or `char`, it is implicitly promoted to a 32-bit `int`. The result of the shift will also be an `int`.
* If the left operand is a `long`, no promotion occurs, and the result is a `long`.

## Shift Distance Masking

To prevent undefined behavior from shifting by a distance greater than the bit-width of the operand, Java applies a bitwise AND mask to the right operand:

* **For `int` operands:** The right operand is masked with `0x1F` (31). The actual shift distance is `rightOperand & 31`. For example, shifting an `int` by 32 positions is equivalent to shifting it by 0 positions.
* **For `long` operands:** The right operand is masked with `0x3F` (63). The actual shift distance is `rightOperand & 63`.

## Syntax Visualization

**Example 1: Standard Left Shift**
Shifting the integer `5` left by `2` positions.

```java theme={"dark"}
int a = 5;      // Binary: 00000000 00000000 00000000 00000101
int b = a << 2; // Binary: 00000000 00000000 00000000 00010100 (Decimal: 20)
```

**Example 2: Negative Number Shift**
Shifting a negative integer (represented in two's complement). The rightmost bits are still filled with zeros, which can eventually push the zero bits into the sign bit position, changing the sign if shifted far enough.

```java theme={"dark"}
int a = -5;     // Binary: 11111111 11111111 11111111 11111011
int b = a << 1; // Binary: 11111111 11111111 11111111 11110110 (Decimal: -10)
```

**Example 3: Shift Distance Masking**
Demonstrating how Java masks the right operand for a 32-bit `int`.

```java theme={"dark"}
int a = 10;
int b = a << 33; // 33 & 0x1F evaluates to 1. Equivalent to: a << 1
// b equals 20
```

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