Operators

Arithmetic

Op Meaning
+ Add / concat
- Subtract
* Multiply
/ Divide
% Modulo

Use Math:Power(base, exp, out) for exponentiation — ^ is bitwise XOR.

+ on a string and any value concatenates both as strings.

Var:Number a = 10;
Var:Number b = 3;
Var:Number sum  = a + b;   // 13
Var:Number diff = a - b;   // 7
Var:Number prod = a * b;   // 30
Var:Number quot = a / b;   // 3.333...
Var:Number rem  = a % b;   // 1

Var:String label = "Score: " + a;   // "Score: 10"

Comparison

Op Meaning
== Equal
!= Not Equal
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal to
Var:Number x = 5;
Konsol:Print(x == 5);   // true
Konsol:Print(x != 3);   // true
Konsol:Print(x > 4);    // true
Konsol:Print(x <= 5);   // true

Logical

Op Meaning
&& logical AND
|| logical OR
! logical NOT
Var:Boolean active = true;
Var:Boolean admin  = false;
if (active == true && admin == false) {
    Konsol:Print("active non-admin");
}
if (!admin) {
    Konsol:Print("not an admin");
}
Var:Boolean allowed = (active == true || admin == true);

Bitwise

Op Meaning
& bitwise AND
^ bitwise XOR
\| bitwise OR
~ bitwise NOT (complement)
<< left shift
>> right shift

Operands are truncated to 64-bit integer before the operation; the result is stored as Number.

Var:Number flags   = 1 | 4;      // 5
Var:Number masked  = flags & 3;  // 1
Var:Number flipped = 12 ^ 10;    // 6
Var:Number shifted = 1 << 8;     // 256
Var:Boolean isOdd  = (n & 1) == 1;

Assignment

Op Meaning
= assign
+= add then assign
-= subtract then assign
*= multiply then assign
/= divide then assign
%= modulus then assign
++ increment (add 1)
-- decrement (subtract 1)
Var:Number n = 10;
n += 5;    // 15
n -= 3;    // 12
n *= 2;    // 24
n /= 4;    // 6
n %= 4;    // 2
n++;       // 3
n--;       // 2

Ternary

condition ? valueIfTrue : valueIfFalse
Var:Number x = 10;
Var:String label = x > 0 ? "positive" : "non-positive";
Konsol:Print(label);   // positive

Ternary has the lowest precedence after ||. Nesting is allowed.

Precedence (high to low)

Unary (`-`, `!`, `~`) → `* / %` → `+ -` → `<< >>` → comparison → `&` → `^` → `|` → `&&` → `||` → ternary `?:`


Examples

Operators

// 02_operators.ks — Arithmetic, comparison, logical, compound assignment

Konsol:Print("=== Operators ===");

// Arithmetic
Var:Number a = 10;
Var:Number b = 3;
Konsol:Print("10 + 3  = ${a + b}");
Konsol:Print("10 - 3  = ${a - b}");
Konsol:Print("10 * 3  = ${a * b}");
Konsol:Print("10 / 3  = ${a / b}");
Konsol:Print("10 % 3  = ${a % b}");
Konsol:Print("2 ^ 8   = ${2 ^ 8}");

// String concatenation with +
Var:String s = "foo" + "bar";
Konsol:Print("concat  = ${s}");
Konsol:Print("mix     = ${"score: " + 42}");

// Unary
Konsol:Print("neg     = ${-a}");
Konsol:Print("not t   = ${!true}");
Konsol:Print("not f   = ${!false}");

// Comparison
Konsol:Print("10 == 10 = ${10 == 10}");
Konsol:Print("10 != 3  = ${10 != 3}");
Konsol:Print("10 > 3   = ${10 > 3}");
Konsol:Print("10 >= 10 = ${10 >= 10}");
Konsol:Print("3  < 10  = ${3 < 10}");
Konsol:Print("3  <= 3  = ${3 <= 3}");

// Logical
Konsol:Print("T && T  = ${true && true}");
Konsol:Print("T && F  = ${true && false}");
Konsol:Print("F || T  = ${false || true}");
Konsol:Print("F || F  = ${false || false}");

// Compound assignment
Var:Number n = 10;
n += 5;   Konsol:Print("+=5  -> ${n}");
n -= 3;   Konsol:Print("-=3  -> ${n}");
n *= 2;   Konsol:Print("*=2  -> ${n}");
n /= 4;   Konsol:Print("/=4  -> ${n}");
n %= 3;   Konsol:Print("%=3  -> ${n}");

// Increment / decrement
Var:Number i = 0;
i++;  Konsol:Print("i++  -> ${i}");
i++;  Konsol:Print("i++  -> ${i}");
i--;  Konsol:Print("i--  -> ${i}");

// Precedence
Konsol:Print("2+3*4   = ${2 + 3 * 4}");
Konsol:Print("(2+3)*4 = ${(2 + 3) * 4}");
Konsol:Print("2^3^2   = ${2 ^ 3 ^ 2}");

Ternary

// 30_ternary.ks — ternary operator  condition ? thenExpr : elseExpr

// ── Basic boolean ─────────────────────────────────────────────────────────────
Var:Boolean flag = true;
Konsol:Print(flag ? "yes" : "no");          // yes

flag = false;
Konsol:Print(flag ? "yes" : "no");          // no

// ── Comparison condition ───────────────────────────────────────────────────────
Var:Number x = 10;
Konsol:Print(x > 5 ? "big" : "small");     // big
Konsol:Print(x == 10 ? "ten" : "other");   // ten
Konsol:Print(x < 3 ? "low" : "high");      // high

// ── Arithmetic in branches ────────────────────────────────────────────────────
Var:Number a = 4;
Var:Number b = 7;
Var:Number bigger = a > b ? a : b;
Konsol:Print(bigger);                       // 7

// ── Assignment from ternary ───────────────────────────────────────────────────
Var:String label = x >= 10 ? "pass" : "fail";
Konsol:Print(label);                        // pass

// ── Ternary inside string interpolation ──────────────────────────────────────
Var:Number score = 85;
Konsol:Print("Grade: ${score >= 90 ? "A" : "B"}");    // Grade: B

// ── Nested ternary (right-associative) ───────────────────────────────────────
Var:Number n = 50;
Var:String tier = n < 25 ? "low" : n < 75 ? "mid" : "high";
Konsol:Print(tier);                         // mid

n = 10;
tier = n < 25 ? "low" : n < 75 ? "mid" : "high";
Konsol:Print(tier);                         // low

n = 90;
tier = n < 25 ? "low" : n < 75 ? "mid" : "high";
Konsol:Print(tier);                         // high

// ── Ternary in function call argument ────────────────────────────────────────
Var:Number val = 0;
Konsol:Print(val == 0 ? "zero" : "nonzero");   // zero

// ── Logical operators in condition ────────────────────────────────────────────
Var:Number age = 20;
Var:Boolean hasId = true;
Konsol:Print(age >= 18 && hasId ? "allowed" : "denied");  // allowed

hasId = false;
Konsol:Print(age >= 18 && hasId ? "allowed" : "denied");  // denied

Konsol:Print("done");

Bitwise

// 41_bitwise.ks — Bitwise operators: & | ^ ~ << >>

Var:Number a = 12;
Var:Number b = 10;
Var:Number result;

// AND
Konsol:Print("=== & (bitwise AND) ===");
result = a & b;
Konsol:Print(result);         // 8  (1100 & 1010 = 1000)

// OR
Konsol:Print("=== | (bitwise OR) ===");
result = a | b;
Konsol:Print(result);         // 14 (1100 | 1010 = 1110)

// XOR
Konsol:Print("=== ^ (bitwise XOR) ===");
result = a ^ b;
Konsol:Print(result);         // 6  (1100 ^ 1010 = 0110)

result = 0 ^ 0;
Konsol:Print(result);         // 0
result = 0xFF ^ 0x0F;
Konsol:Print(result);         // 240
result = 5 ^ 5;
Konsol:Print(result);         // 0  (x ^ x = 0)
result = 0 ^ 7;
Konsol:Print(result);         // 7  (0 ^ x = x)

// NOT
Konsol:Print("=== ~ (bitwise NOT) ===");
result = ~0;
Konsol:Print(result);         // -1
result = ~12 & 15;
Konsol:Print(result);         // 3

// Left shift
Konsol:Print("=== << (left shift) ===");
result = 1 << 3;
Konsol:Print(result);         // 8

result = 3 << 2;
Konsol:Print(result);         // 12

// Right shift
Konsol:Print("=== >> (right shift) ===");
result = 16 >> 2;
Konsol:Print(result);         // 4

result = 255 >> 4;
Konsol:Print(result);         // 15

// In conditions
Konsol:Print("=== bitwise in conditions ===");
Var:Boolean isOdd;
isOdd = (5 & 1) == 1;
Konsol:Print(isOdd);          // true

isOdd = (4 & 1) == 1;
Konsol:Print(isOdd);          // false

// Combined expression
result = (3 | 4) & 7;
Konsol:Print(result);         // 7

// Flags pattern
Var:Number flags = 0;
Var:Number FLAG_READ  = 1;
Var:Number FLAG_WRITE = 2;
Var:Number FLAG_EXEC  = 4;

flags = FLAG_READ | FLAG_WRITE;
Konsol:Print(flags);          // 3

Var:Boolean canRead;
canRead = (flags & FLAG_READ) != 0;
Konsol:Print(canRead);        // true

Var:Boolean canExec;
canExec = (flags & FLAG_EXEC) != 0;
Konsol:Print(canExec);        // false

Konsol:Print("done");