How to use bitwise java operators in Kotlin?

Java has binary code or |both binary and &operators:

int a = 5 | 10;
int b = 5 & 10;

They don't seem to work in Kotlin.

val a = 5 | 10;
val b = 5 & 10;

How to use bitwise java operators in Kotlin?

+10
source share
2 answers

You named functions for them.

Directly from Kotlin docs

Like bitwise operations, there are no special characters for them, but only named functions that can be called in the form of infix.

eg:

val x = (1 shl 2) and 0x000FF000

Here is the complete list of bitwise operations (available only for Int and Long):

shl(bits) – signed shift left (Java <<)
shr(bits) – signed shift right (Java >>)
ushr(bits) – unsigned shift right (Java >>>)
and(bits) – bitwise and
or(bits) – bitwise or
xor(bits) – bitwise xor
inv() – bitwise inversion
+12
source

You can do it in Kotlin

val a = 5 or 10;
val b = 5 and 10;

,

shl(bits) – signed shift left (Java <<)
shr(bits) – signed shift right (Java >>)
ushr(bits) – unsigned shift right (Java >>>)
and(bits) – bitwise and
or(bits) – bitwise or
xor(bits) – bitwise xor
inv() – bitwise inversion
+1

All Articles