Show 例子 4
优质
小牛编辑
129浏览
2023-12-01
尝试以下示例程序以了解Scala编程语言中可用的所有Bitwise运算符。
例子 (Example)
object Demo {
def main(args: Array[String]) {
var a = 60; /* 60 = 0011 1100 */
var b = 13; /* 13 = 0000 1101 */
var c = 0;
c = a & b; /* 12 = 0000 1100 */
println("a & b = " + c );
c = a | b; /* 61 = 0011 1101 */
println("a | b = " + c );
c = a ^ b; /* 49 = 0011 0001 */
println("a ^ b = " + c );
c = ~a; /* -61 = 1100 0011 */
println("~a = " + c );
c = a << 2; /* 240 = 1111 0000 */
println("a << 2 = " + c );
c = a >> 2; /* 215 = 1111 */
println("a >> 2 = " + c );
c = a >>> 2; /* 215 = 0000 1111 */
println("a >>> 2 = " + c );
}
}
将上述程序保存在Demo.scala 。 以下命令用于编译和执行此程序。
Command
\>scalac Demo.scala
\>scala Demo
输出 (Output)
a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2 = 15
a >>> 2 = 15