Skip to main content
Tolk has a bool type with two values: true or false. TVM does not have booleans as a separate type. Instead, true is represented as the bit 1 and false as the bit 0. In the context of TVM’s signed 257-bit integers, this means that true corresponds to a decimal -1, while false corresponds to 0.
Several operators, such as == or && produce values of type bool. Many standard library functions also return bool values:

Logical operators accept both int and bool

  • Operator !x supports both bool and int.
  • The condition of if and similar statements accepts both bool and int, where it treats the latter as true if the value is not equal to 0, and as false otherwise.
  • Logical operators && and || accept both bool and int. For example, a && b evaluates to true when both operands are true or both operands are non-zero integers.
Arithmetic operators are restricted to integers.

Logical and bitwise operators

Tolk has both bitwise & ^ | and logical && || operators that can be used for booleans and integers. The main difference is that logical operators short-circuit: the right operand is evaluated only if required. The compiler performs better instruction optimizations on booleans, saving more gas per comparison. Bitwise operators may sometimes be used instead of logical operators to avoid generating conditional branches at runtime. For example, (a > 0) && (a < 10), when replaced with bitwise &, consumes less gas.

Casting to an integer

Use the as operator to cast the bool to int. No runtime transformations take place: at the TVM level, the bool type is represented as an integer with a decimal value of either -1 or 0.

Serialization

A boolean is serialized as a single bit: 1 for true and 0 for false.