Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Operators Reference

This note is a quick reference for all operators in Compact.

Docs: Compact Reference


Arithmetic Operators

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Moduloa % b

Field Arithmetic

Field operations are modulo the prime field order:

const x: Field = 10;
const y: Field = 3;
const z = x / y;  // 10 * inverse(3) mod p
const r = x % y;  // Remainder

Uint Arithmetic

Uint operations wrap at the type boundary:

const x: Uint<8> = 255;
const y = x + 1;  // 0 (wrapped)

Comparison Operators

OperatorMeaningTypes
==EqualAll
!=Not equalAll
<Less thanUint<n>, Uint<0..n>
<=Less or equalUint<n>, Uint<0..n>
>Greater thanUint<n>, Uint<0..n>
>=Greater or equalUint<n>, Uint<0..n>

Note: Field only supports == and !=. Use Uint types for comparisons.


Boolean Operators

OperatorMeaningExample
&&Logical ANDa && b
``
!Logical NOT!a

Short-circuit evaluation:

const valid = x != 0 && y / x > threshold;

Bitwise Operators

OperatorMeaningExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XORa ^ b
~Bitwise NOT~a

Assignment Operators

OperatorExampleMeaning
=x = 5Simple assignment
+=x += 1x = x + 1
-=x -= 1x = x - 1
*=x *= 2x = x * 2

Type Cast

OperatorMeaning
as TCast to type T
const x: Uint<64> = 42;
const y = x as Field;                    // Widen
const z = x as Uint<0..100>;             // Narrow

Other Operators

OperatorMeaningExample
? :Ternarya > b ? a : b
->Function type(x: T) -> U
[...]Array literal[1, 2, 3]
...Spread...vec

Precedence

Highest to lowest:

  1. () [] .
  2. ! ~ as
  3. * / %
  4. + -
  5. << >>
  6. < <= > >=
  7. == !=
  8. &
  9. ^
  10. |
  11. &&
  12. ||
  13. ? :
  14. = += etc

Quick Reference

// Arithmetic
a + b - c * d / e % f

// Comparison
x == y != z > w >= 0 <= 255

// Boolean
flag && !disabled || hidden

// Ternary
max = a > b ? a : b