# Booleans (https://docs-fpm2731fy-ton-core-docs.vercel.app/llms/tolk/types/booleans/content.md)



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.

```tolk
fun isGreater10(v: int): bool {
    return v > 10
}
```

Several operators, such as `==` or `&&` produce values of type `bool`. Many standard library functions also return `bool` values:

```tolk
var valid = isSignatureValid(...);    // bool
var end = someSlice.isEmpty();        // bool
```

## Conditions and logical operators require `bool` [#conditions-and-logical-operators-require-bool]

* The condition of `if`, `while`, `do while`, ternary, and `assert` must be a `bool`. Integers are not implicitly converted: write `if (x != 0)` rather than `if (x)`.
* The unary `!` operator requires a `bool`: write `if (x == 0)` rather than `if (!x)`.
* Logical operators `&&` and `||` short-circuit and accept both `bool` and `int` operands.

Arithmetic operators are restricted to integers.

```tolk
valid && end;  // ok
valid & end;   // ok, bitwise & | ^ also work if both are bools
if (!end)      // ok, end is bool

valid + end;   // error
8 & valid;     // error, int & bool not allowed
```

## Logical and bitwise operators [#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.

|      Expression      |                    Behavior                    |
| :------------------: | :--------------------------------------------: |
|   `condition & f()`  |             `f()` is called always             |
|  `condition && f()`  |  `f()` is called only if `condition` is `true` |
|  `condition \| f()`  |             `f()` is called always             |
| `condition \|\| f()` | `f()` is called only if `condition` is `false` |

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 [#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.

```tolk
// -1 or 0 in decimal; serialized as a single bit 1 or 0
var i = boolValue as int1;
```

## Serialization [#serialization]

A boolean is [serialized](/llms/languages/tolk/types/overall-serialization/content.md) as a single bit: 1 for `true` and 0 for `false`.
