字节和按位运算符

Bin*_*224 2 byte compiler-errors bitwise-operators kotlin

我测试了代码:

val a : Int = 0x01
val b : Int = 0x03
println(a and b)
Run Code Online (Sandbox Code Playgroud)

得到了:

1
Run Code Online (Sandbox Code Playgroud)

但如果我宣布为Bytes:

val a : Byte = 0x01
val b : Byte = 0x03
println(a and b)
Run Code Online (Sandbox Code Playgroud)

得到错误:

error: unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
@SinceKotlin @InlineOnly public inline infix fun BigInteger.and(other: BigInteger): BigInteger defined in kotlin
println(a and b)
          ^
Run Code Online (Sandbox Code Playgroud)

我找到了参考:https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.experimental/and.html说:

平台和版本要求:Kotlin 1.1

我检查了我的版本:

C:\>kotlinc.bat -version
info: kotlinc-jvm 1.2.30 (JRE 1.8.0_181-b13)
Run Code Online (Sandbox Code Playgroud)

这是什么意思?

DVa*_*rga 5

您缺少import语句:

import kotlin.experimental.and
Run Code Online (Sandbox Code Playgroud)

如果没有导入,编译器会尝试使用and找到的BigIntegers.kt.