Operators
Zeus provides a complete set of operators covering arithmetic, bitwise, comparison, logical, ternary, and assignment operations.
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Addition / string concatenation | 5 + 3 → 8, "a" + "b" → "ab" |
- | Subtraction | 5 - 3 → 2 |
* | Multiplication | 5 * 3 → 15 |
/ | Division | 15 / 3 → 5 |
% | Modulo (remainder) | 7 % 3 → 1 |
** | Exponentiation (right-associative) | 2.0 ** 10.0 → 1024.0 |
let a: i32 = 10;let b: i32 = 3;
let sum: i32 = a + b; // 13let diff: i32 = a - b; // 7let product: i32 = a * b; // 30let quotient: i32 = a / b; // 3 (integer division)let rem: i32 = a % b; // 1Integer vs Float Division
Division between integers truncates toward zero:
let intDiv: i32 = 7 / 2; // 3let floatDiv: f64 = 7.0 / 2.0; // 3.5Exponentiation
** operates on f64. Both operands are cast to f64 automatically:
let squared: f64 = 2.0 ** 2.0; // 4.0let cubed: f64 = 2.0 ** 3.0; // 8.0let 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; // -42Bitwise Operators
Bitwise operators work on integer types (i8–i64, u8–u64) and treat the value as a sequence of bits.
| Operator | Description | Example |
|---|---|---|
& | Bitwise AND | 0b1100 & 0b1010 → 0b1000 (8) |
| | Bitwise OR | 0b1100 | 0b1010 → 0b1110 (14) |
^ | Bitwise XOR | 0b1100 ^ 0b1010 → 0b0110 (6) |
~ | Bitwise NOT (unary) | ~0 → -1 (all bits set) |
<< | Left shift | 1 << 3 → 8 |
>> | Right shift (arithmetic) | 16 >> 2 → 4 |
let a: i32 = 0b1100; // 12let 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; // 16let halved: i32 = 64 >> 2; // 16Common Bitwise Patterns
// Check if a bit is setfunction isBitSet(value: i32, bit: i32): boolean { return (value & (1 << bit)) != 0;}
// Set a bitfunction setBit(value: i32, bit: i32): i32 { return value | (1 << bit);}
// Clear a bitfunction clearBit(value: i32, bit: i32): i32 { return value & ~(1 << bit);}
// Toggle a bitfunction 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 : valueIfFalsefunction abs(n: i32): i32 { return n < 0 ? -n : n;}
let x: i32 = 10;let y: i32 = 20;
let max: i32 = x > y ? x : y; // 20let min: i32 = x < y ? x : y; // 10Ternary 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
| Operator | Description | Example |
|---|---|---|
== | Equal to | 5 == 5 → true |
!= | Not equal to | 5 != 3 → true |
< | Less than | 3 < 5 → true |
<= | Less than or equal | 5 <= 5 → true |
> | Greater than | 5 > 3 → true |
>= | Greater than or equal | 5 >= 5 → true |
function compare(a: i32, b: i32): i32 { if (a == b) { return 0; } if (a < b) { return -1; } return 1;}Logical Operators
| Operator | Description | Short-circuits |
|---|---|---|
! | Logical NOT | — |
&& | Logical AND | Yes — skips right side if left is false |
|| | Logical OR | Yes — 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
| Operator | Description |
|---|---|
+ | 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
| Operator | Form | Behaviour |
|---|---|---|
++x | Prefix | Increment first, return new value |
x++ | Postfix | Return old value, then increment |
--x | Prefix | Decrement first, return new value |
x-- | Postfix | Return 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 → 6Assignment 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:
| Operator | Equivalent | Category |
|---|---|---|
+= | x = x + y | Arithmetic |
-= | x = x - y | Arithmetic |
*= | x = x * y | Arithmetic |
/= | x = x / y | Arithmetic |
%= | x = x % y | Arithmetic |
**= | x = x ** y | Arithmetic |
&= | x = x & y | Bitwise |
|= | x = x | y | Bitwise |
^= | x = x ^ y | Bitwise |
<<= | x = x << y | Bitwise |
>>= | x = x >> y | Bitwise |
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 → 63x <<= 1; // shift left by 1 → 126Operator Precedence
Higher precedence operators bind more tightly. Operators on the same row have equal precedence and associate left-to-right unless marked otherwise.
| Precedence | Operators | Notes |
|---|---|---|
| 20 (highest) | . [] | Member access, indexing |
| 19 | () new | Function call, object construction |
| 18 | x++ x-- | Postfix increment/decrement |
| 17 | -x ! ~ ++x --x | Unary operators |
| 16 | ** as | Exponentiation (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 actionlet 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; // 1const FLAG_WRITE: i32 = 1 << 1; // 2const 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;}