当前位置: 首页 > 文档资料 > Scala 中文文档 >

Show 例子 5

优质
小牛编辑
133浏览
2023-12-01

尝试以下示例程序以了解Scala编程语言中可用的所有Bitwise运算符。

例子 (Example)

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 。 以下命令用于编译和执行此程序。

Command

\>scalac Demo.scala
\>scala Demo

输出 (Output)

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