Bitwise operations

Kotlin doesn't have operators for the bitwise manipulation of integers. Bitwise operations in Kotlin are done with functions. The following table shows all bitwise manipulation functions:

Function

Operation

shl(bits)

signed shift left (<< in Java)

shr(bits)

signed shift right (>> in Java)

ushr(bits)

unsigned shift right(>>> in Java)

and(bits)

bitwise and ( & in Java)

or(bits)

bitwise or ( | in Java)

xor(bits)

bitwise xor (^ in Java)

inv()

bitwise inversion

 

These are infix functions so we can call them without parentheses. This is how you'd shift by one bit to the right:

val n = 8 shr 1
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.107.85