Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala bitwise operation

I am defining a simple function to do some bitwise operation :

def getBit(num:Int, i:Int):Boolean = (num & (1 << i) != 0)

But I am getting this error :

    <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)

Why I cannot use the & operator ? How can I solve this error?

like image 746
Dimitri Avatar asked Dec 20 '22 21:12

Dimitri


1 Answers

The following code should work: def getBit(num:Int, i:Int):Boolean = ((num & (1 << i)) != 0)

like image 142
LuGo Avatar answered Jan 03 '23 06:01

LuGo