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: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: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):- Postfix:
.method(),[index],.field - Unary:
-,!,.not(),.neg() - Cast:
as - Power:
** - Multiplicative:
*,/,% - Additive:
+,- - Shift:
<<,>> - Bitwise AND:
& - Bitwise XOR:
^ - Bitwise OR:
| - Comparison:
==,!=,<,>,<=,>= - Logical AND:
&& - Logical OR:
|| - 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