Skip to content

Operators

Almost every contract operates on data: transforming some values into others. The scope may vary, but operators lie at the core of such modifications.

This page lists all the operators in Tact in decreasing order of their precedence, with examples of usage.

Table of operators

The following table lists operators in order of decreasing precedence, from highest to lowest.

Brief descriptionOperators
Parentheses()
Unary postfix!!
Unary prefix+   -   !   ~
Multiplicative*   /   %
Additive+   -
Shift>>   <<
Relational>   >=   <   <=
Equality==   !=
Bitwise AND&
Bitwise XOR^
Bitwise OR|
Logical AND&&
Logical OR||
Ternary?:
Assignment= and all augmented assignment operators

Precedence

All operators on this page are given in order of decreasing precedence, from highest to lowest. Precedence is used to determine which operator should be considered in a particular situation. Whenever ambiguity arises, Tact prefers operators with higher precedence over those with lower precedence.

For example, the minus sign (-) may be considered either a subtraction operator or a negation operator, which reverses the sign of the expression from plus to minus or vice versa. As the latter has a higher precedence over the former, in cases of ambiguity between the two, Tact will first consider - as a negation operator. Only if that interpretation does not make sense for the given expression will Tact consider it as a subtraction operator.

Consider the following code:

5 + -5; // here, the minus sign would be viewed as a negation operator
5 -5; // while here it would be viewed as a subtraction operator, despite formatting

Even though this example may be simple, neglecting precedence rules can often lead to confusing situations with operators. The correct order of operations can be ensured by wrapping every operation in parentheses, since parentheses have the highest precedence of all expressions and operators.

Parentheses, ()

Parentheses (also called round brackets, ()) are more punctuation symbols than actual operators, but their precedence is higher than the precedence of any other operator. Use parentheses to override the order of operations:

5 * 5 - 2; // 23
5 * (5 - 2); // 15

Unary

Unary here means that they are applied only to one operand of the given expression. All unary operators, except for the non-null assertion, have the same precedence.

Unary operators can be one of two types:

  • Prefix — placed before the expression.
  • Postfix (or suffix) — placed after the expression.

Non-null assert, !!

The unary double-exclamation mark (non-null assertion) operator !! is a postfix operator that enforces non-null values and allows direct access to the value of the optional variable if it’s not null. Otherwise, it raises a compilation error if the compiler can track it, and if not, throws an exception with exit code 128: Null reference exception. It can be applied to any optional variable regardless of its non-null type.

Plus, +

Although the unary plus sign operator + is specified in the grammar of the Tact compiler, it only exists as a binary operator.

Negate, -

The unary minus sign (negation) operator - is a prefix operator, which reverses the sign of the expression. It can only be applied to values of type Int:

let five: Int = 5;
five + -five; // here, the minus sign is a negation operator, not a subtraction operator
-(-1); // double application gives back the original value, which is 1
--1; // 1

Inverse, !

The unary exclamation mark (inversion) operator ! is a prefix operator, which inverts the boolean value of the expression—changing true to false, and vice versa. It can only be applied to values of type Bool:

let iLikeTact: Bool = true;
!iLikeTact; // false
!false; // true
!(!false); // false
!!false; // false

Bitwise NOT, ~

The unary tilde (bitwise NOT) operator ~ is a prefix operator, which inverts or flips each bit in the binary representation of the expression — changing each 11 to 00, and vice versa. It can only be applied to values of type Int:

let answer: Int = 42;
~answer; // -43
~(~answer); // 42
~(~0); // 0
~~0; // 0

Binary

Binary operators are split into several subsections, in order of decreasing precedence. Operators within each subsection have the same precedence as the subsection itself.

Multiplication

Multiply, divide, or obtain a remainder.

Multiply, *

The binary asterisk (multiplication) operator * is used for multiplication of two values. It can cause integer overflows.

It can only be applied to values of type Int:

let two: Int = 2;
two * two; // 4
0 * 1_000_000_000; // 0
-1 * 5; // -5
pow(2, 255) * pow(2, 255); // build error: integer overflow!

Divide, /

The binary slash (division) operator / is used for integer division of two values, which truncates toward zero if the result is positive and away from zero if the result is negative. This is also called rounding down or rounding toward -∞.

An attempt to divide by zero results in an error with exit code 4: Integer overflow.

It can only be applied to values of type Int:

let two: Int = 2;
two / 2; // 1
two / 1; // 2
-1 / 5; // -1
-1 / -5; // 0
1 / -5; // -1
1 / 5; // 0
6 / 5; // 1, rounding down
-6 / 5; // -2, rounding down (toward -∞)

Modulo, %

The binary percent sign (modulo) operator % is used for obtaining the modulo of integer division, which must not be confused with obtaining the remainder. For two values with the same sign, modulo and remainder operations are equivalent, but when the operands have different signs, the modulo result always has the same sign as the divisor (the value on the right), while the remainder has the same sign as the dividend (the value on the left). This difference can cause the results to differ by one unit of the divisor.

It can only be applied to values of type Int:

let two: Int = 2;
two % 2; // 0
two % 1; // 0
1 % 5; // 1
-1 % 5; // 4
1 % -5; // -4
-1 % -5; // -1

The simplest way to avoid confusion between obtaining the modulo and obtaining the remainder is to use only unsigned integers. Alternatively, consider using the abs() function to ensure non-negative values:

abs(-1) % abs(-5); // 1

Addition

Add or subtract.

Add, +

The binary plus (addition) operator + is used for adding numbers together. Going beyond the maximum value of an Int will result in an error with exit code 4: Integer overflow.

It can only be applied to values of type Int:

let two: Int = 2;
two + 2; // 4
-1 + 1; // 0
pow(2, 254) + pow(2, 254); // 2 * 2^{254}
pow(2, 255) + pow(2, 255); // build error: integer overflow!
pow(2, 255) - 1 + pow(2, 255); // 2^{256} - 1, maximal value of any integer in Tact!

Subtract, -

The binary minus (subtraction) operator - is used for subtracting numbers from each other. Going beyond the minimum value of an Int will result in an error with exit code 4: Integer overflow.

It can only be applied to values of type Int:

let two: Int = 2;
two - 2; // 0
-1 - 1; // -2
pow(2, 254) - pow(2, 254); // 0
pow(2, 255) - pow(2, 255); // 0
pow(2, 256) - pow(2, 256); // build error: integer overflow!

Bitwise shifts

Shift bits to the left or to the right.

Shift right, >>

The binary double greater than (bitwise shift right) operator >> returns an integer whose binary representation is the left operand value shifted by the right operand number of bits to the right. Excess bits shifted off to the right are discarded, and copies of the leftmost bit are shifted in from the left. This operation is also called “sign-propagating right shift” or “arithmetic right shift” because the sign of the resulting number is the same as the sign of the left operand. This is a more efficient way to divide the left operand by 2n2^n, where nn is equal to the right operand.

Can only be applied to values of type Int:

let two: Int = 2;
two >> 1; // 1
4 >> 1; // 2
5 >> 1; // 2, due to flooring of integer values
pow(2, 254) >> 254; // 1

Shift left, <<

The binary double less-than (bitwise shift left) operator << returns an integer whose binary representation is the left operand value shifted to the left by the number of bits specified by the right operand. Excess bits shifted off from the left are discarded, and zero bits are shifted in from the right. This is a more efficient way to multiply the left operand by 2n2^n, where nn is equal to the right operand. Exceeding the maximum value of an Int will result in an error with exit code 4: Integer overflow.

Can only be applied to values of type Int:

let two: Int = 2;
two << 1; // 4
1 << 5; // 1 * 2^5, which is 32
2 << 5; // 2 * 2^5, which is 64
pow(2, 254) == (1 << 254); // true
pow(2, 254) == 1 << 254; // true, no parentheses needed due to higher precedence of << over ==
pow(2, 255) == 1 << 255; // true, but we're very close to overflow here!

Relation

Find greater, smaller, or equal values.

Greater than, >

The binary greater than operator > returns true if the left operand is greater than the right operand and false otherwise. Can only be applied to values of type Int:

let two: Int = 2;
two > 2; // false
-1 > -3; // true

Greater than or equal to, >=

The binary greater than or equal to operator >= returns true if the left operand is greater than or equal to the right operand and false otherwise. Can only be applied to values of type Int:

let two: Int = 2;
two >= 2; // true
-1 >= -3; // true

Less than, <

The binary less than operator < returns true if the left operand is less than the right operand, and false otherwise. It can only be applied to values of type Int:

let two: Int = 2;
two < 2; // false
-1 < -3; // false

Less than or equal to, <=

The binary less than or equal to operator <= returns true if the left operand is less than or equal to the right operand, and false otherwise. It can only be applied to values of type Int:

let two: Int = 2;
two <= 2; // true
-1 <= -3; // false

Equality and inequality, == !=

The binary equality (equal) operator == checks whether its two operands are equal, returning a result of type Bool.

The binary inequality (not equal) operator != checks whether its two operands are not equal, returning a result of type Bool.

Both operators require operands to be of the same type, and neither operator performs implicit type conversions, except for the Cell and Slice types, which are implicitly compared by their hashes.

Both operators can be applied to the following list of types and values:

// Int:
2 == 3; // false
2 != 3; // true
// Bool:
true == true; // true
false != true; // true
// Address:
myAddress() == myAddress(); // true
myAddress() != myAddress(); // false
// Cell:
emptyCell() == emptyCell(); // true
emptyCell() != emptyCell(); // false
// Slice:
"A".asSlice() == "A".asSlice(); // true
"A".asSlice() != "A".asSlice(); // false
// String:
"A" == "A"; // true
"A" != "A"; // false
// map<K, V>:
let map1: map<Int, Int> = emptyMap();
let map2: map<Int, Int> = emptyMap();
map1 == map2; // true
map1 != map2; // false
// Optionals and null values themselves
let nullable: Int? = null;
nullable == null; // true
null == null; // true
nullable != null; // false
null != null; // false
let anotherNullable: Int? = 5;
nullable == anotherNullable; // false
nullable != anotherNullable; // true

Bitwise AND, &

The binary ampersand (bitwise AND) operator & applies a bitwise AND, which performs the logical AND operation on each pair of corresponding bits of the operands. This is useful when we want to clear selected bits of a number, where each bit represents an individual flag or a boolean state. This makes it possible to “store” up to 257 boolean values per integer, as all integers in Tact are 257-bit signed.

It can only be applied to values of type Int:

let two: Int = 2;
two & 1; // 0
4 & 1; // 0
3 & 1; // 1
1 & 1; // 1
255 & 0b00001111; // 15
0b11111111 & 0b00001111; // 15

Bitwise XOR, ^

The binary caret (bitwise XOR) operator ^ applies a bitwise XOR, performing the logical exclusive OR operation on each pair of corresponding bits of the operands. The result in each position is 11 if exactly one of the bits is 11, or 00 if both bits are 00 or both bits are 11. Thus, it compares two bits, yielding 11 if the bits are different and 00 if they are the same.

It is useful for inverting selected bits of an operand (also called toggling or flipping), as any bit can be toggled by “XORing” it with 11.

It can only be applied to values of type Int:

let two: Int = 2;
two ^ 3; // 1
4 ^ 1; // 5
3 ^ 1; // 2
1 ^ 1; // 0
255 ^ 0b00001111; // 240
0b11111111 ^ 0b00001111; // 240

Bitwise OR, |

The binary bar (bitwise OR) operator | applies a bitwise OR, which performs the logical OR operation on each pair of corresponding bits of the operands. This is useful when we want to apply a specific bitmask.

For example, bitwise OR is commonly used in Tact to combine base modes with optional flags by masking specific bits to 11 in order to construct a target message mode.

Can only be applied to values of type Int:

let two: Int = 2;
two | 1; // 3
4 | 1; // 5
3 | 1; // 3
1 | 1; // 1
255 | 0b00001111; // 255
0b11111111 | 0b00001111; // 255

Logical AND, &&

The binary logical AND (logical conjunction) operator && returns true if both operands are true and false otherwise. It’s short-circuited, meaning that it immediately evaluates the entire expression as false if the left operand is false, without evaluating the right one.

Can only be applied to values of type Bool:

let iLikeTact: Bool = true;
iLikeTact && true; // true, evaluated both operands
iLikeTact && false; // false, evaluated both operands
false && iLikeTact; // false, didn't evaluate iLikeTact

Logical OR, ||

The binary logical OR (logical disjunction) operator || returns false only if both operands are false, and true otherwise. It is short-circuited, meaning that it immediately evaluates the whole expression as true if the left operand is true, without evaluating the right one.

This operator can only be applied to values of type Bool:

let iLikeSnails: Bool = false;
iLikeSnails || true; // true, evaluated both operands
iLikeSnails || false; // false, evaluated both operands
true || iLikeSnails; // true, didn't evaluate iLikeSnails

Ternary, ?:

The conditional (ternary) operator is the only Tact operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition evaluates to true, followed by a colon (:), and finally the expression to execute if the condition evaluates to false. This operator is frequently used as an alternative to an if...else statement.

The condition must resolve to type Bool:

// condition
// ↓
true ? "incredibly so" : "absolutely not"; // "incredibly so"
// --------------- ----------------
// ↑ ↑
// | alternative, when condition is false
// |
// consequence, when condition is true
2 + 2 == 4 ? true : false; // true

The ternary operator is the only operator with right associativity, besides assignment-related ones. This means that in ambiguous situations, Tact will prefer the longest matching sequence. In short, this makes bracket-less nesting of ternary operators possible, but only for alternative cases (the part that comes after the colon sign :):

// don't need additional parentheses for alternative cases
false ? 1 : (false ? 2 : 3); // 3
false ? 1 : false ? 2 : 3; // also 3
false ? 1 : true ? 2 : 3; // 2
// need additional parentheses for consequence cases (parts between ? and :)
false ? (false ? 1 : 2) : 3; // 3
false ? false ? 1 : 2 : 3; // SYNTAX ERROR!
true ? (false ? 1 : 2) : 3; // 2

Assignment, =

The assignment operator = is used to assign a value to a variable or to a property of a Message or a Struct. The assignment is a statement, and it does not return a value.

let someVar: Int = 5; // assignment operator = is used here...
someVar = 4; // ...and here
someVar = (someVar = 5); // SYNTAX ERROR!

Augmented assignment

Augmented (or compound) assignment operators such as += combine an operation with an assignment. An augmented assignment is a statement and does not return a value.

Augmented assignments are semantically equivalent to regular assignments but include an operation:

let value: Int = 5;
// this:
value += 5;
// is equivalent to this:
value = value + 5;

List of augmented assignment operators:

let value: Int = 5;
// +=
value + 5; // adds 5
value = value + 5; // adds 5 and assigns result back
value += 5; // also adds 5 and assigns result back
// -=
value - 5; // subtracts 5
value = value - 5; // subtracts 5 and assigns result back
value -= 5; // also subtracts 5 and assigns result back
// *=
value * 5; // multiplies by 5
value = value * 5; // multiplies by 5 and assigns result back
value *= 5; // also multiplies by 5 and assigns result back
// /=
value / 5; // divides by 5
value = value / 5; // divides by 5 and assigns result back
value /= 5; // also divides by 5 and assigns result back
// %=
value % 5; // gets modulo by 5
value = value % 5; // gets modulo by 5 and assigns result back
value %= 5; // also gets modulo by 5 and assigns result back
// &=
value & 5; // bitwise ANDs with 5
value = value & 5; // bitwise ANDs with 5 and assigns result back
value &= 5; // also bitwise ANDs with 5 and assigns result back
// ^=
value ^ 5; // bitwise XORs with 5
value = value ^ 5; // bitwise XORs with 5 and assigns result back
value ^= 5; // also bitwise XORs with 5 and assigns result back
// |=
value | 5; // bitwise ORs with 5
value = value | 5; // bitwise ORs with 5 and assigns result back
value |= 5; // also bitwise ORs with 5 and assigns result back
//
// The following augmented assignment operators are available since Tact 1.6
//
// <<=
value << 5; // bitwise shifts left by 5
value = value << 5; // bitwise shifts left by 5 and assigns result back
value <<= 5; // also bitwise shifts left by 5 and assigns result back
// >>=
value >> 5; // bitwise shifts right by 5
value = value >> 5; // bitwise shifts right by 5 and assigns result back
value >>= 5; // also bitwise shifts right by 5 and assigns result back
let bValue: Bool = true;
// &&=
bValue && false; // logically ANDs with false
bValue = bValue && false; // logically ANDs with false and assigns result back
bValue &&= false; // also logically ANDs with false and assigns result back
// ||=
bValue || true; // logically ORs with true
bValue = bValue || true; // logically ORs with true and assigns result back
bValue ||= true; // also logically ORs with true and assigns result back