Scala - 赋值运算符
尝试以下示例程序以了解 Scala 编程语言中可用的所有位运算符。
示例
object Demo { def main(args: Array[String]) { var a = 10; var b = 20; var c = 0; c = a + b; println("c = a + b = " + c ); c += a ; println("c += a = " + c ); c -= a ; println("c -= a = " + c ); c *= a ; println("c *= a = " + c ); a = 10; c = 15; c /= a ; println("c /= a = " + c ); a = 10; c = 15; c %= a ; println("c %= a = " + c ); c <<= 2 ; println("c <<= 2 = " + c ); c >>= 2 ; println("c >>= 2 = " + c ); c >>= 2 ; println("c >>= 2 = " + c ); c &= a ; println("c &= a = " + c ); c ^= a ; println("c ^= a = " + c ); c |= a ; println("c |= a = " + c ); } }
将上述程序保存在 Demo.scala 中。 以下命令用于编译和执行该程序。
命令
\>scalac Demo.scala \>scala Demo
输出
c = a + b = 30 c += a = 40 c -= a = 30 c *= a = 300 c /= a = 1 c %= a = 5 c <<= 2 = 20 c >>= 2 = 5 c >>= 2 = 1 c &= a = 0 c ^= a = 10 c |= a = 10