How to use! operator in scala?

I am new to scala and have tried several basic operations to get the language hanging.

I am trying to use logical operators. For instance:

val a2 = 0x01&0xFF println(!a2) 

I want to negate the value of a2 and then print it. But it gives me a mistake saying

 value unary_! is not a member of Int 

I am not sure how to use the NOt operator. Can someone help me?

+2
source share
2 answers

Use the bitwise operator ~ .

 val a2 = 0x01&0xFF println(~a2) 

Check here for reference.

Of course, it is assumed that you want to negate the value bitwise, otherwise use - .

+9
source

If you expect to get -2, use the bitwise ~ operator, it will invert all bits of your integer. If you expect to get -1, i.e. The opposite of an integer, use the - operator.

Valid operators are listed here.

+1
source

Source: https://habr.com/ru/post/1247432/


All Articles