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 shl (shift left) operator is an infix function in Kotlin that performs a bitwise left shift operation. It shifts the binary representation of the left-hand operand to the left by the number of bit positions specified by the right-hand operand. During this operation, the vacated bit positions on the right (least significant bits) are filled with zeros, and the bits shifted beyond the left boundary (most significant bits) of the data type are discarded. Mathematically, x shl n is equivalent to multiplying x by 2n2^n, assuming no arithmetic overflow occurs.

Syntax and Signatures

In Kotlin, bitwise operators are not built-in language symbols (like << in Java or C++) but are implemented as named infix functions. The shl function is defined for signed integers, unsigned integers, and java.math.BigInteger (via standard library extensions):
infix fun Int.shl(bitCount: Int): Int
infix fun Long.shl(bitCount: Int): Long
infix fun UInt.shl(bitCount: Int): UInt
infix fun ULong.shl(bitCount: Int): ULong
infix fun BigInteger.shl(n: Int): BigInteger

Bitwise Visualization

val original = 5       // Binary (32-bit): 00000000 00000000 00000000 00000101
val shifted = 5 shl 2  // Binary (32-bit): 00000000 00000000 00000000 00010100
                       // Decimal result: 20

Technical Characteristics

1. Operand Masking To prevent shifting by a value greater than or equal to the bit-width of the type, Kotlin masks the right-hand operand (bitCount) at the hardware level for standard integer types:
  • For Int and UInt (32-bit): The shift distance is masked with 31 (binary 11111). Therefore, x shl 32 is equivalent to x shl 0, and x shl 33 is equivalent to x shl 1.
  • For Long and ULong (64-bit): The shift distance is masked with 63 (binary 111111). Therefore, y shl 64 is equivalent to y shl 0.
val x = 1
val result1 = x shl 33 // Evaluates to 2 (1 shl 1)
2. Type Restrictions and Explicit Conversion Kotlin does not provide a shl function for smaller integer types like Byte, Short, UByte, or UShort, and it does not perform implicit widening conversions to resolve member or extension functions. Attempting to use shl directly on these types will result in a compiler error (Unresolved reference: shl). Explicit conversion to Int or UInt is strictly required before applying the operator.
val b: Byte = 3
// val error = b shl 4            // Compiler error: Unresolved reference: shl
val result: Int = b.toInt() shl 4 // Explicit conversion is strictly required
3. Precedence As a named infix function, shl shares the same precedence level as other bitwise operations (shr, ushr, and, or, xor). This precedence is lower than standard arithmetic operators (+, -, *, /) but higher than comparison operators (==, >, <).
val result = 1 shl 2 + 3 // Evaluates as 1 shl (2 + 3) -> 1 shl 5 -> 32
Master Kotlin with Deep Grasping Methodology!Learn More