Skip to content

Operators

Zeus provides a complete set of operators covering arithmetic, bitwise, comparison, logical, ternary, and assignment operations.

Arithmetic Operators

OperatorDescriptionExample
+Addition / string concatenation5 + 38, "a" + "b""ab"
-Subtraction5 - 32
*Multiplication5 * 315
/Division15 / 35
%Modulo (remainder)7 % 31
**Exponentiation (right-associative)2.0 ** 10.01024.0
let a: i32 = 10;
let b: i32 = 3;
let sum: i32 = a + b; // 13
let diff: i32 = a - b; // 7
let product: i32 = a * b; // 30
let quotient: i32 = a / b; // 3 (integer division)
let rem: i32 = a % b; // 1

Integer vs Float Division

Division between integers truncates toward zero:

let intDiv: i32 = 7 / 2; // 3
let floatDiv: f64 = 7.0 / 2.0; // 3.5

Exponentiation

** operates on f64. Both operands are cast to f64 automatically:

let squared: f64 = 2.0 ** 2.0; // 4.0
let cubed: f64 = 2.0 ** 3.0; // 8.0
let chained: f64 = 2.0 ** 3.0 ** 2.0; // 2.0 ** (3.0 ** 2.0) = 512.0 (right-associative)

Unary Minus

let x: i32 = 42;
let neg: i32 = -x; // -42

Bitwise Operators

Bitwise operators work on integer types (i8i64, u8u64) and treat the value as a sequence of bits.

OperatorDescriptionExample
&Bitwise AND0b1100 & 0b10100b1000 (8)
|Bitwise OR0b1100 | 0b10100b1110 (14)
^Bitwise XOR0b1100 ^ 0b10100b0110 (6)
~Bitwise NOT (unary)~0-1 (all bits set)
<<Left shift1 << 38
>>Right shift (arithmetic)16 >> 24
let a: i32 = 0b1100; // 12
let b: i32 = 0b1010; // 10
let andResult: i32 = a & b; // 8 (0b1000)
let orResult: i32 = a | b; // 14 (0b1110)
let xorResult: i32 = a ^ b; // 6 (0b0110)
let notA: i32 = ~a; // -13 (all bits flipped)
let shifted: i32 = 1 << 4; // 16
let halved: i32 = 64 >> 2; // 16

Common Bitwise Patterns

// Check if a bit is set
function isBitSet(value: i32, bit: i32): boolean {
return (value & (1 << bit)) != 0;
}
// Set a bit
function setBit(value: i32, bit: i32): i32 {
return value | (1 << bit);
}
// Clear a bit
function clearBit(value: i32, bit: i32): i32 {
return value & ~(1 << bit);
}
// Toggle a bit
function toggleBit(value: i32, bit: i32): i32 {
return value ^ (1 << bit);
}

Ternary Operator

The ternary operator ?: selects between two values based on a condition — a concise alternative to an if/else expression:

condition ? valueIfTrue : valueIfFalse
function abs(n: i32): i32 {
return n < 0 ? -n : n;
}
let x: i32 = 10;
let y: i32 = 20;
let max: i32 = x > y ? x : y; // 20
let min: i32 = x < y ? x : y; // 10

Ternary expressions can be nested, though deeply nested ternaries reduce readability:

function classify(n: i32): i32 {
return n < 0 ? -1 : n == 0 ? 0 : 1;
}

Comparison Operators

OperatorDescriptionExample
==Equal to5 == 5true
!=Not equal to5 != 3true
<Less than3 < 5true
<=Less than or equal5 <= 5true
>Greater than5 > 3true
>=Greater than or equal5 >= 5true
function compare(a: i32, b: i32): i32 {
if (a == b) { return 0; }
if (a < b) { return -1; }
return 1;
}

Logical Operators

OperatorDescriptionShort-circuits
!Logical NOT
&&Logical ANDYes — skips right side if left is false
||Logical ORYes — skips right side if left is true
function isInRange(x: i32, min: i32, max: i32): boolean {
return x >= min && x <= max;
}
function isValidInput(value: i32): boolean {
return value > 0 || value == -1; // -1 is a sentinel "any" value
}

String Operators

OperatorDescription
+Concatenation
== / !=Content equality
< / <= / > / >=Lexicographic comparison
let greeting: string = "Hello" + ", " + "World!";
let a: string = "apple";
let b: string = "banana";
if (a == "apple") { /* true */ }
if (a < b) { /* true — lexicographic order */ }

Increment and Decrement

OperatorFormBehaviour
++xPrefixIncrement first, return new value
x++PostfixReturn old value, then increment
--xPrefixDecrement first, return new value
x--PostfixReturn old value, then decrement
let a: i32 = 5;
let b: i32 = ++a; // a → 6, b = 6
let c: i32 = 5;
let d: i32 = c++; // d = 5, c → 6

Assignment Operators

Basic Assignment

let x: i32 = 10;
x = 20;
x = x + 5;

Compound Assignment

Compound operators combine an operation with assignment. Zeus supports compound forms for all arithmetic and bitwise operators:

OperatorEquivalentCategory
+=x = x + yArithmetic
-=x = x - yArithmetic
*=x = x * yArithmetic
/=x = x / yArithmetic
%=x = x % yArithmetic
**=x = x ** yArithmetic
&=x = x & yBitwise
|=x = x | yBitwise
^=x = x ^ yBitwise
<<=x = x << yBitwise
>>=x = x >> yBitwise
let x: i32 = 0b11110000; // 240
x &= 0b11001100; // keep only matching bits → 0b11000000 (192)
x |= 0b00001111; // set lower nibble → 0b11001111 (207)
x ^= 0b00110011; // toggle bits → 0b11111100 (252)
x >>= 2; // shift right by 2 → 63
x <<= 1; // shift left by 1 → 126

Operator Precedence

Higher precedence operators bind more tightly. Operators on the same row have equal precedence and associate left-to-right unless marked otherwise.

PrecedenceOperatorsNotes
20 (highest). []Member access, indexing
19() newFunction call, object construction
18x++ x--Postfix increment/decrement
17-x ! ~ ++x --xUnary operators
16** asExponentiation (right-associative); as type cast
15* / %Multiplicative
14+ -Additive
13<< >>Bit shifts
12< <= > >=Relational
11== !=Equality
10&Bitwise AND
9^Bitwise XOR
8|Bitwise OR
7&&Logical AND
6||Logical OR
3?:Ternary (right-associative)
1 (lowest)= += -= *= /= %= **= &= |= ^= <<= >>=Assignment (right-associative)
// Precedence in action
let a: i32 = 2 + 3 * 4; // 14 (* before +)
let b: i32 = (2 + 3) * 4; // 20 (parentheses override)
let c: i32 = 1 | 2 & 3; // 3 (& before |: 1 | (2 & 3) = 1 | 2 = 3)
let d: boolean = 1 < 2 && 3 > 0; // true (< and > before &&)
let e: i32 = 4 > 3 ? 10 : 20; // 10 (comparison before ternary)

Practical Examples

Packing flags into a bitmask

const FLAG_READ: i32 = 1 << 0; // 1
const FLAG_WRITE: i32 = 1 << 1; // 2
const FLAG_EXECUTE: i32 = 1 << 2; // 4
function makePermissions(read: boolean, write: boolean, execute: boolean): i32 {
let flags: i32 = 0;
if (read) { flags |= FLAG_READ; }
if (write) { flags |= FLAG_WRITE; }
if (execute) { flags |= FLAG_EXECUTE; }
return flags;
}

Clamping a value

function clamp(value: i32, lo: i32, hi: i32): i32 {
return value < lo ? lo : value > hi ? hi : value;
}

Checking even / odd with bitwise AND

function isEven(n: i32): boolean {
return (n & 1) == 0;
}