# 7.3. Operators

MScript, like most languages, provides several operators for working with numbers and for comparing values, among other things:

- 7.3.1. Arithmetic
- 7.3.2. Equality
- 7.3.3. Comparisons
- 7.3.4. Logical
- 7.3.5. Binary
- 7.3.6. Unary operators

## 7.3.1. Arithmetic

The arithmetic operators are used for standard numerical operations like addition, subtraction, multiplication and division. If one of the operands is `none`

, then it will be treated as a `0`

. For example, `5 + none`

returns `5`

, and `5 * none`

returns `0`

.

These operators can also be used with arrays: the operators are simply applied to items from both arrays. If one array is shorter than the other, then the final operations will use `none`

as a fallback value. For example, `[1, 2, 3] - [5, 4]`

returns `[-4, -2, 3]`

.

Numbers and array can also be combined: in that case, the number is 'reused' while the operation is applied to all items in the array. For example, `4 * [1, 2, 3]`

returns `[4, 8, 12]`

.

`a + b`

The `+`

operator adds up two numbers. However, if one of the operands is a string then it will perform string concatenation instead.

For example, `4 + 5`

returns `9`

, but `4 + '5'`

returns `'45'`

.

`a - b`

The `-`

operator subtracts one number from another number.

For example: `4 - 5`

returns `-1`

, and `[4, 5] - 1`

returns `[3, 4]`

.

`a * b`

The `*`

operator multiplies two numbers.

For example, `4 * 5`

returns `20`

, and `4 * [1, 2, 3]`

returns `[4, 8, 12]`

.

`a / b`

The `/`

operator divides a number by another number.

For example, `4 / 5`

returns `0.8`

, and `[2, 5, 10] / 2`

returns `[1, 2.5, 5]`

.

`a % b`

The `%`

operator takes the remainder of a division.

For example, `11 % 4`

returns `3`

, and `[4, 5, 6, 7] % 3`

returns `[1, 2, 0, 1]`

.

## 7.3.2. Equality

The equality operators can be used with values of any type. They check whether two values are of the same type and whether they represent the same value. Values of different types are never equal.

`a == b`

The `==`

operator checks if two values are equal.

For example, `'5' == 5`

returns `none`

(false), but `[3, 4] == [3, 4]`

returns `1`

(true).

`a != b`

The `!=`

operator checks whether two values are not equal.

For example, `'5' != 5`

returns `1`

(true), but `[3, 4] != [3, 4]`

returns `none`

(false).

## 7.3.3. Comparisons

The comparison operators determine whether one number is greater than, equal to or less than another number. As with the arithmetic operators, `none`

is treated as `0`

. However, unlike the arithmetic operators, the comparison operators cannot be used with arrays.

`a > b`

The `>`

operator checks if a number is greater than another number.

For example, `5 > 4`

returns `1`

(true).

`a >= b`

The `>=`

operator checks if a number is greater than or equal to another number.

For example, `5 >= 5`

returns `1`

(true).

`a < b`

The `<`

operator checks if a number is less than another number.

For example, `4 < 5`

returns `1`

(true).

`a <= b`

The `<=`

operator checks if a number is less than or equal to another number.

For example, `4 <= 4`

returns `1`

(true).

## 7.3.4. Logical

The logical operators compare two boolean (true/false) expressions. These operators have 'short-circuiting' behavior: they will only evaluate the right-hand expression if necessary. This can sometimes be useful for controlling side-effects, such as with a `setglobal`

or a `trace`

function call.

** a && b** (or

**)**

`a and b`

The `&&`

operator (which can also be written as `and`

) returns the second expression if both expressions are true, or `none`

otherwise. It short-circuits: if the first expression is not true, then the second expression will not be evaluated because the result cannot be true anyway.

For example, `'name'.contains('a') && 4`

returns `4`

.

** a || b** (or

**)**

`a or b`

The `||`

operator (which can also be written as `or`

) returns the first expression if it is true, or otherwise the second expression if that is true, or otherwise `none`

. It short-circuits: if the first expression is true, then the second expression will not be evaluated because the result is already true anyway.

For example, `'name'.contains('a') || 4`

returns `1`

(the result of the `'name.contains('a')`

expression).

## 7.3.5. Binary

Binary operators convert their operands to 32-bit numbers and operate on their bits. Binary operators can only be used with numbers, or with `none`

, which is treated as `0`

.

`a << b`

The `<<`

operator converts the first operand to a 32-bit number, shifts its bits to the left and returns the resulting number. The second operand determines how many bits the first number will be shifted. The right side will be filled with `0`

-bits. The left-most bits of the original number are lost.

For example, `3 << 2`

returns `12`

(bitwise view: shifting `0011`

two to the left becomes `1100`

).

`a >> b`

The `>>`

operator converts the first operand to a 32-bit number, shifts its bits to the right and returns the resulting number. The second operand determines how many bits the first number will be shifted. If the first number is negative, the left side will be filled with `1`

-bits, else it will be filled with `0`

-bits. The right-most bits of the original number are lost.

For example, `13 >> 2`

returns `3`

(bitwise view: shifting `1101`

two to the right becomes `0011`

).

`a & b`

The `&`

operator converts both operands to 32-bit numbers, performs a bitwise 'and' operation and returns the resulting number. A resulting bit will only be `1`

if the input bits are both `1`

.

This is often used for bitmasks, to check whether specific bit(s) are set.

For example, `10 & 3`

returns `2`

(bitwise view: `1010 & 0011`

is `0010`

).

`a ^ b`

The `^`

operator converts both operands to 32-bit numbers, performs a bitwise 'exclusive or' operation and returns the resulting number. A resulting bit will only be `1`

if exactly one of the input bits is `1`

.

This can be used for bitmasks to invert specific bits.

For example, `7 ^ 12`

returns `11`

(bitwise view: `0111 ^ 1100`

is `1011`

).

`a | b`

The `|`

operator converts both operands to 32-bit numbers, performs a bitwise 'or' operation and returns the resulting number. A resulting bit will be one if one or both of the input bits are `1`

.

This is often used to combine bitmasks.

For example, `12 | 7`

returns `15`

(bitwise view: `1100 | 0111`

is `1111`

).

`~a`

The `~`

operator converts its operand to a 32-bit number, inverts all of its bits and returns the resulting number.

For example, `~15`

returns `-16`

(bitwise view: `~00000000000000000000000000001111`

is `11111111111111111111111111110000`

).

## 7.3.6. Unary operators

There are also a few 'unary' operators, which take only a single operand. These are used to negate a numeric value or a boolean expression.

`-a`

The `-`

operator negates a number. It also works with arrays that contain numbers: it will return an array with all numbers negated.

For example, `-max(4, 5)`

returns `-5`

, and `-[4, 5]`

returns `[-4, -5]`

.

** !a** (or

**)**

`not a`

The `!`

operator (which can also be written as `not`

) returns `1`

(true) if the expression is false, or `none`

(false) otherwise.

For example, `!'name'.contains('a')`

returns `none`

.