Kotlin Programming Cookbook
上QQ阅读APP看书,第一时间看更新

shl

The shl function shifts the bit pattern to the left by the specified number of bits.

Consider this example:

fun main(args: Array<String>) {
println( 5 shl 0)
println( 5 shl 1)
println( 5 shl 2)
}

This is the output:

5
10
20

Here's the explanation:

5 = 101 (Binary format)

101 Shift left by 0 bits = 101

101 Shift left by 1 bits = 1010 (10 in Decimal)

101 Shift left by 2 bits = 10100 (20 in Decimal)