Arithmetic Operators
§ 1 Arithmetic Operators
§ 1.1 Addition
+
: Adds two numbers.
val a: int = 10
val b: int = 20
val c: int = a + b // c = 30
§ 1.2 Subtraction
-
: Subtracts two numbers.
val a: int = 20
val b: int = 10
val c: int = a - b // c = 10
§ 1.3 Multiplication
*
: Multiplies two numbers.
val a: int = 10
val b: int = 20
val c: int = a * b // c = 200
§ 1.4 Division
/
: Divides two numbers.
val a: int = 20
val b: int = 10
val c: int = a / b // c = 2
§ 1.5 Modulo
%
: Returns the remainder of the division of two numbers.
val a: int = 20
val b: int = 10
val c: int = a % b // c = 0
§ 1.6 Power
**
: Raises the first number to the power of the second number.
val a: int = 2
val b: int = 3
val c: int = a ** b // c = 8
§ 1.7 Parentheses
(
)
: Parentheses are used to group expressions and have the highest precedence.
val a: int = 10
val b: int = 20
val c: int = (a + b) * 2 // c = 60
§ 1.8 Shift Operators
<<
, >>
, >>>
: Shifts the bits of the first number by the second number.
val a: int = 2
val b: int = 1
val c: int = a << b // c = 4
§ 1.9 Increment and Decrement
++
, --
: Increments and decrements a number by one.
var a: int = 10
a++ // a = 11, >> 10
var b: int = 10
++b // b = 11, >> 11
var a: int = 10
a-- // a = 9, >> 10
var b: int = 10
--b // b = 9, >> 9
§ 1.10 Bitwise Operators
&
, |
, ^
, ~
: Perform bitwise AND, OR, XOR, and NOT operations on two numbers.
val a: int = 0b101
val b: int = 0b110
val c: int = a & b // c = 0b100
val d: int = a | b // d = 0b111
val e: int = a ^ b // e = 0b011
val f: int = ~a // f = 0b11111111111111111111111111111010