Operators
Arithmetic
| Operator | Meaning | Works on |
|---|---|---|
+ | add | int, float, byte |
- | subtract | int, float, byte |
* | multiply | int, float, byte |
/ | divide | int, float, byte |
% | remainder | int, byte |
-x | negate | int, float, byte |
Both sides must have the same type (see Types don’t mix).
print(7 + 3)
print(7 - 10)
print(6 * 7)
print(7 / 2)
print(7 % 3)
print(7.0 / 2.0)
print(-(2 + 3))Output:
10
-3
42
3
1
3.5
-5Whole-number division
For int and byte:
/drops the fraction, rounding toward zero:7 / 2is3,-7 / 2is-3.%gives the remainder, with the sign of the left side:7 % 3is1,-7 % 3is-1.- Dividing by zero (with
/or%) stops the program with a runtime error:
zero: int = 0
print(10 / zero)Output:
example.luma:2:10: runtime error: division by zeroFor floats, division by zero gives inf or nan instead (see float).
Overflow
int results wrap around at 64 bits, and byte results wrap around within 0 to 255. Floats follow IEEE 754 and become inf when they’re too large.
Comparison
| Operator | Meaning | Works on |
|---|---|---|
== | equal | all types except lists, maps and sets |
!= | not equal | all types except lists, maps and sets |
< | less than | int, float, byte, str |
<= | less than or equal | int, float, byte, str |
> | greater than | int, float, byte, str |
>= | greater than or equal | int, float, byte, str |
A comparison gives a bool.
print(3 < 5)
print(3 == 5)
print("abc" == "abc")
print("abc" != "abd")
print("Kask" < "Tamm")
print(true == false)Output:
true
false
true
true
true
false- Strings are equal when they contain exactly the same characters.
- Strings are ordered by their characters’ numbers (code points), one after another, as
sort()puts them:"Apple" < "apple","z" < "ä", and"ab" < "abc"(a string comes before the longer ones it starts). - Comparisons can’t be chained:
1 < x < 10is an error. Write1 < x && x < 10.
Logic
| Operator | Meaning |
|---|---|
a && b | true if both are true |
a || b | true if at least one is true |
!a | true if a is false |
The operands must be bool. There is no “truthiness”: if count is an error; write if count > 0.
age: int = 30
print(age >= 18 && age <= 65)
print(age < 18 || age > 65)
print(!(age == 30))Output:
true
false
falseShort-circuit evaluation
&& and || only evaluate the right side when it can change the result:
a && bdoesn’t evaluatebwhenais false.a || bdoesn’t evaluatebwhenais true.
This makes guards like the following safe:
zero: int = 0
if zero != 0 && 10 / zero > 1 {
print("never")
}
print("no division by zero happened")Output:
no division by zero happenedBitwise
Bitwise operators work on the bits of an int or a byte. Network protocols, file formats and flags need them.
| Operator | Meaning | Works on |
|---|---|---|
a & b | and: the bits that are 1 in both | int, byte |
a | b | or: the bits that are 1 in either | int, byte |
a ^ b | xor: the bits that are 1 in one of them, not both | int, byte |
a << n | shift left by n bits | int, byte |
a >> n | shift right by n bits | int, byte |
print(12 & 10)
print(12 | 10)
print(12 ^ 10)
print(1 << 10)
print(0xFF00 >> 8)Output:
8
14
6
1024
255For &, | and ^, both sides have the same type, as with arithmetic. They don’t work on bool: for those, use && and ||.
Shifts
In a << n and a >> n, the left side decides the type, and n is an int (or a byte):
<<moves the bits up and fills with 0. On abyte, bits moved past the 8th are gone:x << 1withx= 200 is 144.>>moves the bits down and keeps the sign of anint:-16 >> 2is-4.- Shifting by 64 or more gives
0, and-1for>>of a negativeint. On abyte, shifting by 8 or more gives0. - Shifting by a negative number raises error 1:
cannot shift by -1.
There is no “not” operator for bits. x ^ -1 flips all the bits of an int, and b ^ 255 flips all the bits of a byte.
Flags
A common use is a set of yes/no flags packed into one number:
lock READ: int = 1 << 0
lock WRITE: int = 1 << 1
lock ADMIN: int = 1 << 2
rights: int = READ | WRITE
print(rights & WRITE != 0)
print(rights & ADMIN != 0)
rights = rights | ADMIN // turn ADMIN on
rights = rights & (WRITE ^ -1) // turn WRITE off
print(rights)Output:
true
false
5And reading a number from bytes, lowest byte first:
data: [byte] = [0x2C, 0x01, 0x00]
size: int = convert(data[0]) | convert(data[1]) << 8 | convert(data[2]) << 16
print(size)Output:
300A buffer does this for you with b.read(3); bitwise operators are for what is packed inside the numbers.
Precedence
From highest (done first) to lowest:
| Level | Operators |
|---|---|
| 1 | -x !x |
| 2 | * / % & << >> |
| 3 | + - | ^ |
| 4 | ?? (a default for an optional value: nick ?? "anonymous") |
| 5 | == != < <= > >= is (which obj a trait value holds, what a json value is) |
| 6 | && |
| 7 | || |
Operators on the same level are evaluated from left to right: 10 - 4 - 3 is (10 - 4) - 3. (?? goes the other way: a ?? b ?? c is a ?? (b ?? c), the first that isn’t nil). Use parentheses to change the order.
The bitwise operators bind tighter than comparisons, so flags & WRITE != 0 means (flags & WRITE) != 0. A shift is like a multiplication, so 1 + 2 << 3 is 1 + (2 << 3), which is 17.
print(1 + 2 * 3)
print((1 + 2) * 3)
print(10 - 4 - 3)
print(true || false && false)Output:
7
9
3
true