问题
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?
回答1:
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 -
.
回答2:
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
来源:https://stackoverflow.com/questions/36693811/how-to-use-the-operator-in-scala