Skip to main content

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.

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

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 0s.
    • If the left operand is negative (sign bit is 1 in two’s complement representation), the vacated bits are filled with 1s.
  • 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 0s. 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 2n, 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 2n.

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.
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:
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, 0s are shifted in from the left. Signed Negative Integer Shift:
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, 1s are shifted in from the left, maintaining the two’s complement negative representation. Unsigned Integer Shift:
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, 0s are shifted in from the left, regardless of the leading bit.
Master Kotlin with Deep Grasping Methodology!Learn More