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

# Kotlin Shift Right

The `shr` operator in Kotlin performs a bitwise right shift. For signed integer types (`Int`, `Long`), it acts as an arithmetic right shift, preserving the sign bit (sign extension). For unsigned integer types (`UInt`, `ULong`), it acts as a logical right shift, shifting in zeros from the left (zero-extension).

## Syntax

```kotlin theme={"dark"}
val result = operand shr bitCount
```

Kotlin implements bitwise operations as infix functions rather than traditional symbolic operators (such as `>>` found in Java or C++).

## Mechanics

* **Signed Types (`Int`, `Long`):** The operator performs sign extension. When the bits are shifted to the right, the empty bit positions created on the left (the most significant bits) are filled with the value of the original sign bit.
  * If the left operand is positive (sign bit is `0`), the vacated bits are filled with `0`s.
  * If the left operand is negative (sign bit is `1` in two's complement representation), the vacated bits are filled with `1`s.
* **Unsigned Types (`UInt`, `ULong`):** The operator performs zero-extension. Because unsigned types do not have a sign bit, the vacated most significant bits are always filled with `0`s. Consequently, Kotlin does not provide a `ushr` (unsigned shift right) operator for `UInt` and `ULong`, as `shr` inherently performs a logical shift for these types.
* **Mathematical Equivalence:** For signed integers, shifting `x` right by `n` positions is mathematically equivalent to exact mathematical division (real division) by 2<sup>n</sup>, with the result rounded towards negative infinity (floor division). This differs from standard Kotlin integer division, which truncates towards zero (e.g., `-15 / 4` evaluates to `-3`, whereas `-15 shr 2` evaluates to `-4`). For unsigned integers, `shr` is mathematically equivalent to standard integer division by 2<sup>n</sup>.

## Type Support

The `shr` function is defined for `Int`, `Long`, `UInt`, and `ULong` types, and is also provided as an extension function for `java.math.BigInteger`.

Kotlin does not support implicit widening conversions for bitwise operations. Attempting to invoke `shr` directly on a `Byte` or `Short` (e.g., `myByte shr 2`) will result in a compiler error (`Unresolved reference: shr`). The left operand must be explicitly converted using `.toInt()`, `.toLong()`, or their unsigned equivalents before applying the shift.

```kotlin theme={"dark"}
val b: Byte = -16
// val error = b shr 2 // Compiler error: Unresolved reference: shr
val valid = b.toInt() shr 2
```

## Shift Count Masking

To prevent over-shifting, Kotlin applies a bitwise AND mask to the right operand (`bitCount`) before performing the shift. This guarantees the shift distance is always a valid, positive integer within the bit-width of the target type.

* For **`Int`** and **`UInt`** operands, the shift count is masked with `31` (`bitCount and 31`). This restricts the shift distance to a range of 0 to 31. For example, `x shr 33` evaluates to `x shr 1`. Because it is a bitwise mask rather than a modulo operation, negative shift counts wrap correctly: `x shr -1` evaluates to `x shr 31`.
* For **`Long`** and **`ULong`** operands, the shift count is masked with `63` (`bitCount and 63`), restricting the shift distance to a range of 0 to 63.

## Binary Visualization

**Signed Positive Integer Shift:**

```kotlin theme={"dark"}
val a = 16      // Binary: 00000000 00000000 00000000 00010000
val b = a shr 2 // Binary: 00000000 00000000 00000000 00000100 (Decimal: 4)
```

*Because the original signed value is positive, `0`s are shifted in from the left.*

**Signed Negative Integer Shift:**

```kotlin theme={"dark"}
val x = -16     // Binary: 11111111 11111111 11111111 11110000
val y = x shr 2 // Binary: 11111111 11111111 11111111 11111100 (Decimal: -4)
```

*Because the original signed value is negative, `1`s are shifted in from the left, maintaining the two's complement negative representation.*

**Unsigned Integer Shift:**

```kotlin theme={"dark"}
val u = 4294967280u // Binary: 11111111 11111111 11111111 11110000
val v = u shr 2     // Binary: 00111111 11111111 11111111 11111100 (Decimal: 1073741820)
```

*Because the type is unsigned, `0`s are shifted in from the left, regardless of the leading bit.*

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