Goldengirl
Goldengirl

Reputation: 967

How to use the ! operator in scala?

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

Answers (2)

Luka Jacobowitz
Luka Jacobowitz

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

Agemen
Agemen

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

Related Questions