Fortinet white logo
Fortinet white logo

Script Reference Guide

Operators

Operators

Operators are symbols used to perform specific mathematical or logical manipulations.

The Lua language includes the following categories of built-in operators:

For more details, see the Lua Reference Manual.

Arithmetic Operators

The Arithmetic operators listed below all operate on real numbers.
In the Example column, assume variable A is 10 and variable B is 20.

Operator

Description

Example

+ Adds two operands. A + B results in 30.
- Subtracts second operand from the first. A - B results in -10.
* Multiplies both operands. A * B results in 200.
/ Divides the numerator by the denominator. B / A results in 2.

//

Floor division is a division that rounds the quotient towards minus infinity, that is, the floor of the division of its operands. This rounds the result down to the nearest whole number or integer, which is less than or equal to the normal division result.

B // A results in 0.

% Modulo is the remainder of an integer division that rounds the quotient towards minus infinity (floor division). B % A results in 0.
^ Exponentiation. A^2 results in 100.

-

Unary minus acts as negation.

-A results in -10.

Bitwise Operators

Bitwise operators convert its operands to integers, operate on all bits of those integers, and result in an integer.
In the Example column, assume we have two integer variables with binary representations:
a = 1100; // (12 in decimal)
b = 1010; // (10 in decimal)

Operator

Description

Example

& Bitwise AND compares each bit of the first operand with the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.

c = a & b; // (8 in decimal)

c = 1000;

| Bitwise OR compares each bit of the first operand to the corresponding bit of the second operand. If either of the bits is 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.

c = a | b; // (14 in decimal)

c = 1110;

~ Bitwise exclusive OR (XOR) compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.

c = a ~ b; // (6 in decimal)

c = 0110;

>> Bitwise Right Shift shifts the bits of the given number to the right by the specified positions. When a number is right shifted, 0s are added to the left side of the number, and the rightmost bits are discarded.

Using a variable "n" and a shift count "s", the left shift operation can be represented as:

c = n >> s;

a = 1100; (12 in decimal)

If we right shift "a" by 2 positions, the resulting value is:

c = a >> 2; // (3 in decimal)

c = 11;

<< Bitwise Left Shift shifts the bits of the given number to the left by the specified positions. When a number is left shifted, 0s are added to the right side of the number. The leftmost bits are discarded.

Using a variable "n" and a shift count "s", the left shift operation can be represented as:

c = n << s;

a = 1010; (10 in decimal)

If we left shift "a" by 2 positions, the resulting value is:

c = a << 2; // (40 in decimal)

c = 101000;

~ Unary bitwise NOT is used to perform bitwise negation on a single operand. It flips all the bits of the operand, changing each - bit to 1 and each 1 bit to 0.

Using the variable "a":

a = 1010; (10 in decimal)

c = ~a

c = 11111111111111111111111111110101; (-11 in decimal)

Applying the unary bitwise NOT operator to "10" flips all the bits, resulting in the Lua integer "-11".

Relational Operators

Lua provides the Relational operators listed below. This type of operator always results in true or false.
In the Example column, assume variable A is 10 and variable B is 20.

Operator

Description

Example

== Checks if the value of two operands are equal or not. If yes, then the condition is true. (A == B) is false.
~= Checks if the value of two operands are equal or not. If the values are not equal, then the condition is true. (A ~= B) is true.
> Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition is true. (A > B) is not false.
< Checks if the value of the left operand is less than the value of the right operand. If yes, then the condition is true. (A < B) is true.
>= Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition is true. (A >= B) is false.
<= Checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition is true. (A <= B) is true.

Logical Operators

Lua provides the Logical operators listed below. This type of operator considers false and nil both as false, and anything else as true.

In the Example column, assume variable A is 10 and variable B is 20.

Operator

Description

Example

and Performs a logical "and" comparison between two values. If both the operands are non-zero then the condition is true. The operator and returns its first argument if it is false; otherwise, it returns its second argument. (A and B) is false.
or Performs a logical "or" comparison between two values. If any of the two operands is non-zero, then the condition is true. The operator or returns its first argument if it is not false; otherwise, it returns its second argument (A or B) is true.
not Performs a logical "not" on a value. Used to reverse the logical state of its operand. If a condition is true, then the Logical not operator will make it false. !(A and B) is true.

Misc Operators

Miscellaneous operators supported by Lua include concatenation and length.

Operator

Description

Example

.. The string concatenation operator in Lua is denoted by two dots ('..'). If both operands are strings or numbers, then they are converted to a string. It's the same as __concat. a..b where a is "Hello " and b is "World", will return "Hello World".

Operators

Operators

Operators are symbols used to perform specific mathematical or logical manipulations.

The Lua language includes the following categories of built-in operators:

For more details, see the Lua Reference Manual.

Arithmetic Operators

The Arithmetic operators listed below all operate on real numbers.
In the Example column, assume variable A is 10 and variable B is 20.

Operator

Description

Example

+ Adds two operands. A + B results in 30.
- Subtracts second operand from the first. A - B results in -10.
* Multiplies both operands. A * B results in 200.
/ Divides the numerator by the denominator. B / A results in 2.

//

Floor division is a division that rounds the quotient towards minus infinity, that is, the floor of the division of its operands. This rounds the result down to the nearest whole number or integer, which is less than or equal to the normal division result.

B // A results in 0.

% Modulo is the remainder of an integer division that rounds the quotient towards minus infinity (floor division). B % A results in 0.
^ Exponentiation. A^2 results in 100.

-

Unary minus acts as negation.

-A results in -10.

Bitwise Operators

Bitwise operators convert its operands to integers, operate on all bits of those integers, and result in an integer.
In the Example column, assume we have two integer variables with binary representations:
a = 1100; // (12 in decimal)
b = 1010; // (10 in decimal)

Operator

Description

Example

& Bitwise AND compares each bit of the first operand with the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.

c = a & b; // (8 in decimal)

c = 1000;

| Bitwise OR compares each bit of the first operand to the corresponding bit of the second operand. If either of the bits is 1, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.

c = a | b; // (14 in decimal)

c = 1110;

~ Bitwise exclusive OR (XOR) compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1. Otherwise, the result bit is set to 0.

c = a ~ b; // (6 in decimal)

c = 0110;

>> Bitwise Right Shift shifts the bits of the given number to the right by the specified positions. When a number is right shifted, 0s are added to the left side of the number, and the rightmost bits are discarded.

Using a variable "n" and a shift count "s", the left shift operation can be represented as:

c = n >> s;

a = 1100; (12 in decimal)

If we right shift "a" by 2 positions, the resulting value is:

c = a >> 2; // (3 in decimal)

c = 11;

<< Bitwise Left Shift shifts the bits of the given number to the left by the specified positions. When a number is left shifted, 0s are added to the right side of the number. The leftmost bits are discarded.

Using a variable "n" and a shift count "s", the left shift operation can be represented as:

c = n << s;

a = 1010; (10 in decimal)

If we left shift "a" by 2 positions, the resulting value is:

c = a << 2; // (40 in decimal)

c = 101000;

~ Unary bitwise NOT is used to perform bitwise negation on a single operand. It flips all the bits of the operand, changing each - bit to 1 and each 1 bit to 0.

Using the variable "a":

a = 1010; (10 in decimal)

c = ~a

c = 11111111111111111111111111110101; (-11 in decimal)

Applying the unary bitwise NOT operator to "10" flips all the bits, resulting in the Lua integer "-11".

Relational Operators

Lua provides the Relational operators listed below. This type of operator always results in true or false.
In the Example column, assume variable A is 10 and variable B is 20.

Operator

Description

Example

== Checks if the value of two operands are equal or not. If yes, then the condition is true. (A == B) is false.
~= Checks if the value of two operands are equal or not. If the values are not equal, then the condition is true. (A ~= B) is true.
> Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition is true. (A > B) is not false.
< Checks if the value of the left operand is less than the value of the right operand. If yes, then the condition is true. (A < B) is true.
>= Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition is true. (A >= B) is false.
<= Checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition is true. (A <= B) is true.

Logical Operators

Lua provides the Logical operators listed below. This type of operator considers false and nil both as false, and anything else as true.

In the Example column, assume variable A is 10 and variable B is 20.

Operator

Description

Example

and Performs a logical "and" comparison between two values. If both the operands are non-zero then the condition is true. The operator and returns its first argument if it is false; otherwise, it returns its second argument. (A and B) is false.
or Performs a logical "or" comparison between two values. If any of the two operands is non-zero, then the condition is true. The operator or returns its first argument if it is not false; otherwise, it returns its second argument (A or B) is true.
not Performs a logical "not" on a value. Used to reverse the logical state of its operand. If a condition is true, then the Logical not operator will make it false. !(A and B) is true.

Misc Operators

Miscellaneous operators supported by Lua include concatenation and length.

Operator

Description

Example

.. The string concatenation operator in Lua is denoted by two dots ('..'). If both operands are strings or numbers, then they are converted to a string. It's the same as __concat. a..b where a is "Hello " and b is "World", will return "Hello World".