XScript Manual · Chapter 4

Operators

Precedence

PrecedenceOperatorsDescription
1 (high)( ) [ ] . :: x++ x--Call · index · member · module access · postfix inc/dec
2- + ! ~ ++x --xSign · logical not · bitwise not · prefix inc/dec
3* / % **Multiply · divide · remainder · power
4+ -Add · subtract (+ also concatenates strings)
5<< >>Bit shift
6< <= > >=Relational comparison
7== !=Equality
8&Bitwise AND
9^Bitwise XOR (not power)
10|Bitwise OR
11&&Logical AND (short-circuit)
12||Logical OR (short-circuit)
13 (low)= += -=Assignment · compound assignment (no *= /=)

Binary operators of the same precedence are evaluated from left to right. Assignment is evaluated from right to left, so chains like a = b = 0; work.

Arithmetic and bit operations

xscript
int    a   = 5 / 2;            // 2 ( integer ÷ integer = integer division )
double d   = 5.0 / 2;          // 2.5 ( one double operand = floating division )
int    m   = 7 % 3;            // 1
double p   = 2 ** 10;          // 1024.0 ( ** power — result is always double )
int    x   = 5 ^ 3;            // 6 ( ^ is bitwise XOR — not power! )
int    s   = 1 << 4;           // 16
int    inv = ~0b1010 & 0xF;    // 0b0101 = 5

Caution Power is **. ^ is bitwise XOR, so 5 ^ 3 is 6, not 125. Also, unary - is applied before **, so -2 ** 2 is 4.0.

Increment and compound assignment

xscript
int i = 0;
i++;          // postfix ( prefix ++i works too )
i += 10;
i -= 3;
i = i * 2;    // there is no *= or /=, so write it out
  • The only compound assignments are += and -=. *= and /= are syntax errors.
  • Use ++ and -- on simple variables.
  • += and -= cannot be used on a bool variable (compile error).

String concatenation and comparison

xscript
string tag  = "NO-" + 12;            // "NO-12" ( numbers are converted and appended )
bool   same = ("abc" == "abc");      // string comparison ( relational = lexicographic )

For string functions see STR — string functions; for working with strings see Strings and arrays.

Short-circuit evaluation 4.0+

In A && B(), when A is false B() is not called. Likewise A || B() skips B() when A is true. 3.x evaluated both sides.

xscript
// When devIdx is out of range, CheckDevice() is never called
if( devIdx >= 0 && CheckDevice(devIdx) == true )
{
   Log("Device OK");
}

Division-by-zero protection

Dividing or taking the remainder by 0 yields 0 instead of throwing. This is a protective behaviour so the machine sequence does not stop — verify in your code that the value is what you intended.

64-bit integer arithmetic 4.0+

Integer arithmetic, comparison and bit operations are computed exactly in 64-bit space. Even values above 9 quadrillion are calculated, compared and bit-operated without drifting.

Assignment inside a condition

An assignment in a condition, as in if( a = 1 ) · while( a = 1 ), is treated as a typo for == and is a compile error. Assigning to a non-assignable target, as in if( a == 1 && b = 2 ), is an error too — see Compile diagnostics.

Subscribe