# Scala運算符

• 算術運算符
• 關係運算符
• 邏輯運算符
• 按位運算符
• 賦值運算符
• 其它運算符

## 1.算術運算符

`+`

`A + B = 30`

`-`

`A − B = -10`

`*`

`A * B = 200`

`/`

`%`

`B % A = 0`

`++`

`A++ = 11`

`--`

`A-- = 9`

``````object Demo {
def main(args: Array[String]) {
var a = 10;
var b = 20;
var c = 25;
var d = 25;

println("a + b = " + (a + b) );
println("a - b = " + (a - b) );
println("a * b = " + (a * b) );
println("b / a = " + (b / a) );
println("b % a = " + (b % a) );
println("c % a = " + (c % a) );
}
}``````

``````a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5``````

## 2.關係運算符

`==`

`(A == B)`結果爲`false`

`!=`

`(A != B)` 結果爲`true`

`>`

`(A > B)` 結果爲`false`

`<`

`(A < B)`結果爲`true`

`>=`

`(A >= B)` 結果爲`false`

`<=`

`(A <= B)`結果爲`true`

``````object Demo {
def main(args: Array[String]) {
var a = 10;
var b = 20;

println("a == b = " + (a == b) );
println("a != b = " + (a != b) );
println("a > b = " + (a > b) );
println("a < b = " + (a < b) );
println("b >= a = " + (b >= a) );
println("b <= a = " + (b <= a) );
}
}``````

``````a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false``````

## 3.邏輯運算符

`&&`

`(A && B)`結果爲`false`

(A B)結果爲`true`

`!`

``````object Demo {
def main(args: Array[String]) {
var a = true;
var b = false;

println("a && b = " + (a&&b) );

println("a || b = " + (a||b) );

println("!(a && b) = " + !(a && b) );
}
}``````

``````a && b = false
a || b = true
!(a && b) = true``````

## 4.按位運算符

p

q

p & q

p/q

p ^ q

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

1

1

``````A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011``````

`&`

`(A&B)= 12`，即`0000 1100`

(A B) = 61, 即 `0011 1101`

`^`

`(A ^ B) = 49`, 即, `0011 0001`

`~`

`(~A)= -61`，即 `1100 0011`的補碼形式。

`<<`

`A << 2 = 240` 即, `1111 0000`

`>>`

`A >> 2 = 15` 即,`0000 1111`

``````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 );
}
}``````

``````a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2 = 15
a >>> 2 = 15``````

## 5.賦值運算符

`=`

`C = A + B`，將`A + B`的值分配給`C`

`+=`

`C + = A`等價於`C = C + A`

`-=`

`C -= A`等價於 `C = C - A`

`*=`

`C * = A`等價於`C = C * A`

`/=`

`C /= A`等價於`C = C / A`

`%=`

`C %= A`等價於`C = C % A`

`<<=`

`C <<= 2`等價於`C = C << 2`

`>>=`

`C >> = 2`等價於`C = C >> 2`

`&=`

`C &= 2`等價於`C = C & 2`

`^=`

`C ^= 2`等價於`C = C ^ 2`

``````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 );
}
}``````

``````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``````

## 6.運算符優先級

``int value = 10 + 20 * 10;``

`value`變量計算結果爲：`210`，因爲`*`(乘法運算符)的優先級比`+`(加法運算符)高，所以在`+`(加法運算符)之前進行求值。

C語言運算符的優先級和關聯性如下：

`() [] -> . ++ - -`

`+ - ! ~ ++ - - (type)* & sizeof`

`* / %`

`+ -`

`<< >>`

`< <= > >=`

`== !=`

`&`

`^`

`/`

`&&`

`//`

`?:`

`= += -= *= /= %=>>= <<= &= ^= /=`

`,`