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

shr

The shr function shifts the bit pattern to the right by the specified number of bits.

Take this example into consideration:

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

Given here is the output:

5
2
1

The following is the explanation:

5 = 101 (Binary format)

101 Shift right by 0 bits = 101

101 Shift right by 1 bits = 010 (2 in Decimal)

101 Shift right by 2 bits = 001 (1 in Decimal)