How to use the ! operator in scala?

夙愿已清 提交于 2019-12-11 01:41:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!