Below
table summarizes the bit wise operators supported by Kotlin.
Operator
|
Description
|
Java Equivalant
|
shl
|
Perform
signed shift left
|
<<
|
shr
|
Perform
signed shift right
|
>>
|
ushr
|
Perform
unsigned shift right
|
>>>
|
and
|
Bitwise
and
|
&
|
or
|
Bitwise
or
|
|
|
xor
|
Bitwise
X-OR
|
^
|
inv()
|
Bitwise
inversion
|
~
|
Other
than inv(), all other operators are used in infix form.
Ex:
var1
shl var2
var1
or var2
1. shl: Perform
signed shift left
Left
shift operator shifts all the bits to one bit left, i.e, it doubles the value.
Syntax:
variable shl n
left shift the variable by 'n' number of
times.
Variable
shl n is equals to variable = variable * 2 power n
var var1 : Int = 2
var1 shl 1 = 2 * 2 power 1 = 4
var1 shl 2 = 2 * 2 power 2 = 8
var1 shl 9 = 2 * 2 power 9 = 1024
LeftShift.kt
fun main(args: Array<String>) {
var var1: Int = 2
var res1 = var1 shl 1
var res2 = var1 shl 2
var res3 = var1 shl 9
println("res1 : ${res1}")
println("res2 : ${res2}")
println("res3 : ${res3}")
}
Output
res1 : 4
res2 : 8
res3 : 1024
2. shr: Perform
signed shift right
Right
shift operator shifts all the bits to one bit right, i.e, it half (divide by 2)
the value
Syntax:
variable shr n
right shift the variable by 'n' number of
times.
Variable shr n is equals to variable =
variable / 2 power n
var a
: Int = 128
a shr 1 = 128 / (2 power 1) = 64
a shr 2 = 128 / (2 power 2) = 32
RightShift.kt
fun main(args: Array<String>) {
var a: Int = 128
var res1 = a shr 1
var res2 = a shr 2
var res3 = a shr 3
println("res1 : ${res1}")
println("res2 : ${res2}")
println("res3 : ${res3}")
}
Output
res1 : 64
res2 : 32
res3 : 16
3. ushr: Unsigned
right shift
Shifts
all the bits to one bit right, and make the left most bit to zero
Syntax
Variable
ushr n
UnsignedRightShift.kt
fun main(args: Array<String>) {
var a: Int = -128
var res1 = a ushr 1
println("res1 : ${res1}")
}
Output
Explanation
-128
in 2's complement form is
11111111
11111111 11111111 10000000
>>>
shifts the one bit right and replace leftmost bit by zero
so
(-128 ushr 1) is like below sequence
01111111
11111111 11111111 11000000
4. Bit Wise and, or,
xor
Bit1
|
Bit2
|
and
|
or
|
xor
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
1
|
1
|
0
|
0
|
1
|
1
|
1
|
1
|
1
|
1
|
0
|
HelloWorld.kt
fun main(args: Array<String>) {
var a: Int = 8
var b: Int = 9
println("a and b is ${a and b} ")
println("a or b is ${a or b} ")
println("a xor b is ${a xor b}")
}
Output
a and b is 8
a or b is 9
a xor b is 1
Explanation
a =
0000 1000
b =
0000 1001
a&b = 0000 1000
a =
0000 1000
b =
0000 1001
a|b = 0000 10001
a
= 0000 1000
b
= 0000 1001
a^b = 0000 0001
Note:
Bit
wise operators are applicable only to integer values like byte, short etc.,
these are not applicable to float, double, Boolean.
5. inv(): Bitwise
inversion
Inversion
operator change the bit value to zero, if it is one and change the bit value to
one if it is zero.
Example
var
a : Int = 8
a
in binary format is 0 0 0 0 0 1 1 1
a.inv()
in binary format is 1 1 1 1 1 0 0 0
Inverse.kt
fun main(args: Array<String>) {
var a: Int = 8
println("Value of a is : ${a}")
println("inversion of a is : ${a.inv()}")
}
Output
Value of a is : 8
inversion of a is : -9