Skip to main content

Operator Overview

Leo provides a rich set of operators for working with values. All operations are checked for correctness at compile time and during execution.

Arithmetic Operators

Basic Arithmetic

Standard arithmetic operations with overflow checking:
Arithmetic operations on integers check for overflow. If an operation would overflow, the program will fail at runtime.

Wrapping Arithmetic

For intentional wraparound behavior, use wrapping methods:

Method Syntax

All arithmetic operations can also be called as methods:

Comparison Operators

Comparison operators return boolean values:

Method Syntax

Comparisons can use method syntax:

Logical Operators

Logical operators work with boolean values:

Short-Circuit Evaluation

Logical AND (&&) and OR (||) use short-circuit evaluation:

Bitwise Operators

Bitwise operations manipulate individual bits:

Shift Operators

Shift operations move bits left or right:
Shift amounts that exceed the bit width will cause the operation to fail. Use wrapping versions for intentional wraparound.

Unary Operators

Negation

Negate numeric values:
Negation is only available for signed integer types (i8, i16, i32, i64, i128).

Logical NOT

Invert boolean values:

Cryptographic Operations

Field Operations

Field elements support arithmetic:

Group Operations

Group elements support elliptic curve operations:

Scalar Operations

Scalars support modular arithmetic:

Special Operations

Absolute Value

Get the absolute value of signed integers:

Modulo Operation

Compute modulo (different from remainder):

Square Root

Compute square root for field and scalar types:

Operator Precedence

Operators are evaluated in the following order (highest to lowest):
  1. Postfix: .method(), [index], .field
  2. Unary: -, !, .not(), .neg()
  3. Cast: as
  4. Power: **
  5. Multiplicative: *, /, %
  6. Additive: +, -
  7. Shift: <<, >>
  8. Bitwise AND: &
  9. Bitwise XOR: ^
  10. Bitwise OR: |
  11. Comparison: ==, !=, <, >, <=, >=
  12. Logical AND: &&
  13. Logical OR: ||
  14. Ternary: condition ? true_expr : false_expr

Precedence Examples

Ternary Operator

The ternary operator provides conditional expressions:

Type-Specific Operators

Address Operations

Addresses support only equality comparison:

Boolean Operations

Booleans support logical and bitwise operations:

Best Practices

1. Use Checked Operations

2. Prefer Operator Syntax

3. Use Parentheses for Clarity

4. Validate Before Operations

Common Patterns

Range Checking

Bitwise Flags

Min/Max

Next Steps

Statements

Use operators in statements

Functions

Build expressions in functions

Data Types

Understand type compatibility