# 數值操作

``println(1 + 2 * 3)``

``println(10 / 3)``

``println(10.0 / 3)``

`val x = 0.1println((x + x + x) == 0.3)`

0.5 = 1/2
0.75 = 1/2 + 1/4
0.875 = 1/2 + 1/4 + 1/8
0.1 = 1/16 + 1/32 + 1/256 + 1/512 + 1/4096 + 1/8192 + ...

`val x = BigDecimal("0.1")println((x + x + x) == 0.3)`

``println("10 >  5  -> " + (10 > 5))println("10 >= 5  -> " + (10 >= 5))println("10 <  5  -> " + (10 < 5))println("10 <= 5  -> " + (10 <= 5))println("10 == 5  -> " + (10 == 5))println("10 != 5  -> " + (10 != 5))``

10 >  5  -> true
10 >= 5  -> true
10 <  5  -> false
10 <= 5  -> false
10 == 5  -> false
10 != 5  -> true

``println(List(1, 2, 3) == List(1, 2, 3))``

``println(List(1, 2, 3) eq List(1, 2, 3))``

``val number = 75println(number > 70 && number < 80)println(number > 80 || number < 75)println(!(number > 80))``

``println("AND運算：")println("0 AND 0\t\t" + (0 & 0))println("0 AND 1\t\t" + (0 & 1))println("1 AND 0\t\t" + (1 & 0))println("1 AND 1\t\t" + (1 & 1))println("\nOR運算：")println("0 OR 0\t\t" + (0 | 0))println("0 OR 1\t\t" + (0 | 1))println("1 OR 0\t\t" + (1 | 0))println("1 OR 1\t\t" + (1 | 1))println("\nXOR運算：")println("0 XOR 0\t\t" + (0 ^ 0))println("0 XOR 1\t\t" + (0 ^ 1))println("1 XOR 0\t\t" + (1 ^ 0))println("1 XOR 1\t\t" + (1 ^ 1))``

AND運算：
0 AND 0         0
0 AND 1         0
1 AND 0         0
1 AND 1         1

OR運算：
0 OR 0          0
0 OR 1          1
1 OR 0          1
1 OR 1          1

XOR運算：
0 XOR 0         0
0 XOR 1         1
1 XOR 0         1
1 XOR 1         0

``val number: Byte = 0println(~number)``

``val original = 'A'println("before encoding：" + original)   // 顯示Aprintln("after encoding：" + (original ^ 0x7).toChar) // 顯示F``

``val number = 1println("2的1次: " +  (number << 1))println("2的2次: " +  (number << 2))println("2的3次: " +  (number << 3))println("2的4次: " +  (number << 4))``

2的1次: 2
2的2次: 4
2的3次: 8
2的4次: 16

00000001 -> 1
00000010 -> 2
00000100 -> 4
00001000 -> 8

 運 算子 範 例 結 果 += a += b a = a + b -= a -= b a = a - b *= a *= b a = a * b /= a /= b a = a / b %= a %= b a = a % b &= a &= b a = a & b |= a |= b a = a | b ^= a ^= b a = a ^ b <<= a <<= b a = a << b >>= a >>= b a = a >> b >>>= a >>>= b a = a >>> b