Skip to main content
Tolk has a bool type which can have two values: true or false.
Operators == >= && || ... return 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 (!= 0)
  • logical && || accept both bool and int; for example, a && b evaluates to true when both operands are true, and for integers when both operands are non-zero
Arithmetic operators are restricted to integers.

Logical vs bitwise operators

Tolk has both bitwise & ^ | and logical && || operators. Both can be used for booleans and integers. The main difference is that logical are short-circuit: the right operand is evaluated only if required to. The compiler does some optimizations for booleans. Example: !x for int results in asm 0 EQINT, but !x for bool results in asm NOT. Bitwise operators may sometimes be used instead of logical operators to avoid generating conditional branches at runtime. For example, (a > 0) && (a < 10), being replaced with bitwise, consumes less gas. Future versions of the compiler may perform such transformations automatically, although this is non-trivial.

Casting to int via as operator

There are no runtime transformations: bool is guaranteed to be -1/0 at the TVM level.

Q: Why are booleans -1, not 1?

In TVM, there are only integers. When all bits in a signed integer are set to 1, it equals -1 in a decimal representation. This makes bitwise operations more intuitive — for example, NOT 0 naturally becomes -1.
  • true = -1 — all bits set to 1
  • false = 0 — all bits set to 0
It is consistent across TVM instructions. For example, operator a > b is asm GREATER, which returns -1 or 0.

Stack layout and serialization

A boolean is backed by TVM INT (-1 or 0). Serialized as a single bit. For details, follow TVM representation and Serialization.