Reputation: 967
I am new to scala and was trying a few basic operations to get a hang of the language.
I am trying to use the logical operators. For example :
val a2 = 0x01&0xFF
println(!a2)
I want to negate the value of a2 and then print it out. But it gives me an error saying
value unary_! is not a member of Int
I am not sure on how do I use the NOt operator. Could somebody help me?
Upvotes: 2
Views: 255
Reputation: 23532
Use the bitwise not operator ~
.
val a2 = 0x01&0xFF
println(~a2)
Check here for reference.
Of course, this is assuming you want to negate the value bitwise, otherwise use -
.
Upvotes: 9
Reputation: 1535
If you expect to obtain -2, use the bitwise operator ~
, it will invert all the bits of your integer. If you expect to obtain -1, i.e. the opposite of your integer, use the -
operator.
Valid operators are listed here
Upvotes: 1