Java 有 binary-or |
和 binary-and &
运算符:
int a = 5 | 10;
int b = 5 & 10;
它们似乎不适用于 Kotlin:
val a = 5 | 10;
val b = 5 & 10;
如何在 Kotlin 中使用 Java 的位运算符?
最佳答案
您已经为它们命名了函数。
直接来自 Kotlin docs
Bitwise operations are represented by functions that can be called in infix form. They can be applied only to
Int
andLong
.
例如:
val x = (1 shl 2) and 0x000FF000
这里是按位运算的完整列表:
shl(bits) – signed shift left (Java's <<)
shr(bits) – signed shift right (Java's >>)
ushr(bits) – unsigned shift right (Java's >>>)
and(bits) – bitwise and
or(bits) – bitwise or
xor(bits) – bitwise xor
inv() – bitwise inversion
关于java - 如何在 Kotlin 中使用 Java 的位运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48474520/