English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Kotlin provides several functions (in infix form) to perform bitwise and shift operations. In this article, you will learn how to perform bit-level operations in Kotlin with examples.
bitwise operators and shift operators are used to perform bit-level operations only on two integer types (Int and Long).
To perform these operations, Kotlin provides7is a function using infix notation.
the OR function compares the corresponding bits of two values. If any of the bits is1then1. Otherwise, it is 0. For example,
12 = 00001100 (binary) 25 = 00011001 (binary) 12and25bitwise OR operation of 00001100 or 00011001 ________ 00011101 = 29 (decimal)
fun main(args: Array<String>) { val number1 = 12 val number2 = 25 val result: Int result = number1 or number2 // result = number1.or(number2) println(result) }
When running the program, the output is:
29
and the function compares the corresponding bits of two values. If both bits are1the result is1If either of the two bits is 0, the result is 0. For example,
12 = 00001100 (binary) 25 = 00011001 (binary) 12and25bitwise AND operation 00001100 and 00011001 ________ 00001000 = 8 (decimal)
fun main(args: Array<String>) { val number1 = 12 val number2 = 25 val result: Int result = number1 and number2 // result = number1.and(number2) println(result) }
When running the program, the output is:
8
The xor function compares the corresponding bits of two values. If the corresponding bits are different, it is1. If the corresponding bits are the same, it is 0. For example,
12 = 00001100 (binary) 25 = 00011001 (binary) 12and25bitwise XOR operation 00001100 xor 00011001 ________ 00010101 = 21 (decimal)
fun main(args: Array<String>) { val number1 = 12 val number2 = 25 val result: Int result = number1 xor number2 // result = number1.xor(number2) println(result) }
When running the program, the output is:
21
inv() function performs bitwise NOT. It flips each 0 to 1as well as each 1 to 0.
35 = 00100011 (binary) 35bitwise complement operation 00100011 ________ 11011100 = 220 (decimal)
fun main(args: Array<String>) { val number = 35 val result: Int result = number.inv() println(result) }
When running the program, the output is:
-36
We output -36 instead of 220Why do we output
?.2This is because the compiler displays the number's
complement. The negative sign of binary numbers.2The complement of any integer n, n's-The complement of (n + 1).
Decimal Binary 2's complement --------- --------- --------------------------------------- 0 00000000 -(11111111+1) = -00000000 = -0 (decimal) 1 00000001 -(11111110+1) = -11111111 = -256(decimal) 12 00001100 -(11110011+1) = -11110100 = -244(decimal) 220 11011100 -(00100011+1) = -00100100 = -36(decimal) Note: Overflow is ignored while computing 2's complement.
35The bitwise complement of220 (decimal).22The bitwise complement of 0 is2The two's complement of-36. Therefore, the output is-36Instead of220.
The shl function shifts the bit pattern to the left by a certain number of specified bits, and zero bits are moved to the low position.
212 (binary: 11010100) 212 shl 1 evaluates to 424 (binary: 110101000) 212 shl 0 evaluates to 212 (binary: 11010100) 212 shl 4 evaluates to 3392 (binary: 110101000000)
fun main(args: Array<String>) { val number = 212 println(number shl 1) println(number shl 0) println(number shl 4) }
When running the program, the output is:
424 212 3392
The shr function shifts the bit pattern to the right by the specified number of bits.
212 (binary: 11010100) 212 shr 1 is calculated as 106 (binary: 01101010) 212 shr 0 is calculated as 212 (binary: 11010100) 212 shr 8 calculated as 0 (binary: 00000000)
If the number is2If the complement bit is set, then the flag is shifted to the high position.
fun main(args: Array<String>) { val number = 212 println(number shr 1) println(number shr 0) println(number shr 8) }
When you run the program, the output will be:
106 212 0
The ushr function shifts zeros to the leftmost position.
fun main(args: Array<String>) { val number1 = 5 val number2 = -5 //Signed Right Shift println(number1 shr 1) //Unsigned Right Shift println(number1 ushr 1) //Signed Right Shift println(number2 shr 1) //Unsigned Right Shift println(number2 ushr 1) }
When running the program, the output is:
2 2 -3 2147483645
Note that for2The two's complement, the working principle of the unsigned right shift function and the signed right shift function are different.
2147483645is2The two's complement of3.