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.
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
>> 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 with0s. - If the left operand is negative (sign bit is
1in two’s complement representation), the vacated bits are filled with1s.
- If the left operand is positive (sign bit is
- 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 with0s. Consequently, Kotlin does not provide aushr(unsigned shift right) operator forUIntandULong, asshrinherently performs a logical shift for these types. - Mathematical Equivalence: For signed integers, shifting
xright bynpositions 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 / 4evaluates to-3, whereas-15 shr 2evaluates to-4). For unsigned integers,shris mathematically equivalent to standard integer division by 2n.
Type Support
Theshr 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.
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
IntandUIntoperands, the shift count is masked with31(bitCount and 31). This restricts the shift distance to a range of 0 to 31. For example,x shr 33evaluates tox shr 1. Because it is a bitwise mask rather than a modulo operation, negative shift counts wrap correctly:x shr -1evaluates tox shr 31. - For
LongandULongoperands, the shift count is masked with63(bitCount and 63), restricting the shift distance to a range of 0 to 63.
Binary Visualization
Signed Positive Integer Shift:0s are shifted in from the left.
Signed Negative Integer Shift:
1s are shifted in from the left, maintaining the two’s complement negative representation.
Unsigned Integer Shift:
0s are shifted in from the left, regardless of the leading bit.
Master Kotlin with Deep Grasping Methodology!Learn More





