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

The `>>` operator in Java is the signed right shift bitwise operator. It shifts the bit pattern of the left operand to the right by the number of positions specified by the right operand, while performing sign extension to preserve the sign of the original number.

```java theme={"dark"}
result = value >> distance;
```

## Mechanics of the Shift

When the `>>` operator is applied, the bits of the `value` are moved to the right.

* The bits shifted off the right side (the least significant bits, or LSB) are permanently discarded.
* The empty spaces created on the left side (the most significant bits, or MSB) are filled with the value of the original sign bit.

This behavior is known as **sign extension**:

* If the original number is positive (sign bit is `0`), the leftmost bits are padded with `0`s.
* If the original number is negative (sign bit is `1`), the leftmost bits are padded with `1`s.

## Syntax Visualization

Because Java uses 32-bit two's complement representation for the `int` data type, the bitwise operations look like this in memory:

```java theme={"dark"}
// Positive Integer Shift
int a = 8;      // 00000000 00000000 00000000 00001000
int b = a >> 2; // 00000000 00000000 00000000 00000010 (Result: 2)

// Negative Integer Shift
int c = -8;     // 11111111 11111111 11111111 11111000
int d = c >> 2; // 11111111 11111111 11111111 11111110 (Result: -2)
```

## Type Promotion and Evaluation

The `>>` operator only operates on integral types (`byte`, `short`, `char`, `int`, `long`). Before the shift occurs, Java applies **unary numeric promotion**:

* Operands of type `byte`, `short`, or `char` are implicitly promoted to a 32-bit `int`.
* The result of the shift operation on these smaller types is always an `int`.

```java theme={"dark"}
byte b = -16;
// 'b' is promoted to int: 11111111 11111111 11111111 11110000
// The result is an int, not a byte.
int result = b >> 2; 
```

## Shift Distance Masking

Java restricts the shift distance to prevent shifting by more bits than the data type holds. At runtime, the JVM (via bytecodes such as `ishr` or `lshr`) or the underlying CPU hardware applies a bitwise AND mask to the right operand (`distance`) based on the type of the promoted left operand:

* **For `int` (32-bit):** The shift distance is masked with `0x1F` (31). This evaluates to `distance & 31`.
* **For `long` (64-bit):** The shift distance is masked with `0x3F` (63). This evaluates to `distance & 63`.

This bitwise masking is strictly distinct from Java's remainder operator (`%`). If the remainder operator were used, a negative shift distance would yield a negative result (e.g., `-1 % 32 == -1`). By using a bitwise AND, Java ensures the shift distance correctly wraps to a positive integer (e.g., `-1 & 31 == 31`).

```java theme={"dark"}
int value = 1024;

// 34 is masked to (34 & 31), which equals 2.
// The operation executed at runtime is value >> 2.
int result = value >> 34; 

// -1 is masked to (-1 & 31), which equals 31.
// The operation executed at runtime is value >> 31.
int negativeShift = value >> -1; 
```

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