我正在定义一个简单的函数来执行一些位操作:
def getBit(num:Int, i:Int):Boolean = (num & (1 << i) != 0)
但我收到了这个错误:
<console>:7: error: overloaded method value & with alternatives:
(x: Long)Long <and>
(x: Int)Int <and>
(x: Char)Int <and>
(x: Short)Int <and>
(x: Byte)Int
cannot be applied to (Boolean)
def getBit(num:Int, i:Int):Boolean = (num & (1 << i) != 0)
为什么我不能使用
&
运算符?如何解决这个错误?
&
接收作为参数(1 << i) != 0
的结果,它是一个布尔值。在这种情况下,&
函数需要一个整数。 - LuGo