TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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
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),ushrstrictly performs a zero extension. This means a negative number shifted usingushrwill become a positive number, as the MSB (the sign bit) is overwritten with a0. -
Supported Types and the Sign-Extension Trap: The
ushroperator is defined exclusively forInt(32-bit) andLong(64-bit) types. It is not defined on smaller types likeByteorShort. Attempting to useushrdirectly 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 negativeBytepads the new 24 upper bits with1s. 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:
- 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 with31(binary11111), meaning only the lowest 5 bits of the right operand are used.x ushr 32is equivalent tox ushr 0. - For a
Long, the shift distance is masked with63(binary111111), using the lowest 6 bits.
- For an
Behavior Visualization
Example 1: Positive Integer
When applied to a positive integer,ushr behaves identically to shr because the MSB is already 0.
Example 2: Negative Integer
When applied to a negative integer, the difference betweenushr and shr becomes apparent due to the zero-fill behavior.
shr on -2 by 1 would result in -1 (11111111 11111111 11111111 11111111) because it duplicates the 1 in the sign bit.
Master Kotlin with Deep Grasping Methodology!Learn More





