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

The `ushr` (unsigned shift right) operator in Kotlin performs a logical right bitwise shift. It shifts the binary representation of the left operand to the right by the number of bit positions specified by the right operand, filling the newly vacated most significant bits (MSBs) with zeros, regardless of the operand's original sign.

## Syntax

```kotlin theme={"dark"}
val result = value ushr bitCount

// Alternatively, using standard method invocation:
val result = value.ushr(bitCount)
```

Because `ushr` is declared with the `infix` modifier in Kotlin, it allows omitting the dot and parentheses for readability, though standard method invocation remains perfectly valid.

## Technical Mechanics

* **Zero Extension:** Unlike the `shr` (shift right) operator, which performs an arithmetic shift and preserves the sign bit (sign extension), `ushr` strictly performs a zero extension. This means a negative number shifted using `ushr` will become a positive number, as the MSB (the sign bit) is overwritten with a `0`.
* **Supported Types and the Sign-Extension Trap:** The `ushr` operator is defined exclusively for `Int` (32-bit) and `Long` (64-bit) types. It is not defined on smaller types like `Byte` or `Short`. Attempting to use `ushr` directly on these types results in a compiler error (`Unresolved reference: ushr`).

  While the operand must be explicitly converted first using `.toInt()` or `.toLong()`, doing so on a negative number introduces a semantic bug due to sign extension. Calling `.toInt()` on a negative `Byte` pads the new 24 upper bits with `1`s. To achieve a true 8-bit or 16-bit logical shift, you must apply a bitwise mask (e.g., `and 0xFF`) to clear the sign-extended bits before shifting:

```kotlin theme={"dark"}
val myByte: Byte = -8 // 8-bit binary: 11111000

// val error = myByte ushr 2 // Compiler error

// SEMANTIC BUG: .toInt() sign-extends to 32 bits (11111111 11111111 11111111 11111000)
// Shifting right by 2 yields a massive positive integer instead of an 8-bit shift.
val flawed = myByte.toInt() ushr 2 // Result: 1073741822 (00111111 11111111 11111111 11111110)

// CORRECT: Masking with 0xFF clears the sign-extended bits before the shift.
// 1. myByte.toInt() and 0xFF -> 00000000 00000000 00000000 11111000
// 2. ushr 2                  -> 00000000 00000000 00000000 00111110
val correct = (myByte.toInt() and 0xFF) ushr 2 // Result: 62
```

* **Shift Distance Masking:** Kotlin masks the right operand (`bitCount`) to prevent shifting beyond the bit-width of the left operand.
  * For an `Int`, the shift distance is masked with `31` (binary `11111`), meaning only the lowest 5 bits of the right operand are used. `x ushr 32` is equivalent to `x ushr 0`.
  * For a `Long`, the shift distance is masked with `63` (binary `111111`), using the lowest 6 bits.

## Behavior Visualization

### Example 1: Positive Integer

When applied to a positive integer, `ushr` behaves identically to `shr` because the MSB is already `0`.

```kotlin theme={"dark"}
val a = 8          // Binary: 00000000 00000000 00000000 00001000
val b = a ushr 2   // Binary: 00000000 00000000 00000000 00000010
// Result: b = 2
```

### Example 2: Negative Integer

When applied to a negative integer, the difference between `ushr` and `shr` becomes apparent due to the zero-fill behavior.

```kotlin theme={"dark"}
val x = -2         // Binary: 11111111 11111111 11111111 11111110
val y = x ushr 1   // Binary: 01111111 11111111 11111111 11111111
// Result: y = 2147483647 (Int.MAX_VALUE)
```

By contrast, using `shr` on `-2` by `1` would result in `-1` (`11111111 11111111 11111111 11111111`) because it duplicates the `1` in the sign 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>
