Zig Operators
Operators and expressions are the basic building blocks used to perform various operations in programming languages.
In Zig, operators can be divided into several categories, including arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators.
The following is a detailed description and examples of each type of operator.
Arithmetic operators
| operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulo (Modulus) Operator |
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
const
a
:
i32
=
5
;
const
b
:
i32
=
3
;
const
add
:
i32
=
a
b
;
const
subtract
:
i32
=
a
b
;
const
multiply
:
i32
=
a
b
;
const
divide
:
i32
=
a
/
b
;
const
remainder
:
i32
=
a
%
b
;
std.
debug
.
(
"a + b = {}\n"
, .
{
add
}
)
;
std.
debug
.
(
"a - b = {}\n"
, .
{
subtract
}
)
;
std.
debug
.
(
"a * b = {}\n"
, .
{
multiply
}
)
;
std.
debug
.
(
"a / b = {}\n"
, .
{
divide
}
)
;
std.
debug
.
(
"a % b = {}\n"
, .
{
remainder
}
)
;
}
The compilation output result is:
a + b = 8
a - b = 2
a * b = 15
a / b = 1
a % b = 2
Relational operators
| operator | Description |
|---|---|
| == | Equal |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
const
a
:
i32
=
5
;
const
b
:
i32
=
3
;
const
equal
:
bool
=
a
==
b
;
const
not_equal
:
bool
=
a
!
=
b
;
const
greater
:
bool
=
a
b
;
const
less
:
bool
=
a
<
b
;
const
greater_equal
:
bool
=
a
=
b
;
const
less_equal
:
bool
=
a
<=
b
;
std.
debug
.
(
"a == b: {}\n"
, .
{
equal
}
)
;
std.
debug
.
(
"a != b: {}\n"
, .
{
not_equal
}
)
;
std.
debug
.
(
"a > b: {}\n"
, .
{
greater
}
)
;
std.
debug
.
(
"a < b: {}\n"
, .
{
less
}
)
;
std.
debug
.
(
"a >= b: {}\n"
, .
{
greater_equal
}
)
;
std.
debug
.
(
"a <= b: {}\n"
, .
{
less_equal
}
)
;
}
The compilation output result is:
a == b: false
a != b: true
a > b: true
a < b: false
a >= b: true
a <= b: false
Logical operators
| operator | Description |
|---|---|
| and | Logical AND |
| or | Logical OR |
| ! | Logical NOT |
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
const
a
:
bool
=
true
;
const
b
:
bool
=
false
;
const
and_result
:
bool
=
a and b
;
const
or_result
:
bool
=
a or b
;
const
not_result
:
bool
=
!
a
;
std.
debug
.
(
"a and b: {}\n"
, .
{
and_result
}
)
;
// false
std.
debug
.
(
"a or b: {}\n"
, .
{
or_result
}
)
;
// true
std.
debug
.
(
"!a: {}\n"
, .
{
not_result
}
)
;
// false
}
The compilation output result is:
a and b: false
a or b: true
!a: false
Bitwise operators
| operator | Description |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left shift |
| >> | Right shift |
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
const
a
:
i32
=
5
;
// 0101
const
b
:
i32
=
3
;
// 0011
const
bit_and
:
i32
=
a
&
b
;
// 0001
const
bit_or
:
i32
=
a
|
b
;
// 0111
const
bit_xor
:
i32
=
a
^
b
;
// 0110
const
bit_not
:
i32
=
~a
;
// 11111111111111111111111111111010
const
left_shift
:
i32
=
a
<<
1
;
// 1010
const
right_shift
:
i32
=
a
1
;
// 0010
std.
debug
.
(
"a & b: {}\n"
, .
{
bit_and
}
)
;
std.
debug
.
(
"a | b: {}\n"
, .
{
bit_or
}
)
;
std.
debug
.
(
"a ^ b: {}\n"
, .
{
bit_xor
}
)
;
std.
debug
.
(
"~a: {}\n"
, .
{
bit_not
}
)
;
std.
debug
.
(
"a << 1: {}\n"
, .
{
left_shift
}
)
;
std.
debug
.
(
"a >> 1: {}\n"
, .
{
right_shift
}
)
;
}
The compilation output result is:
a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2
Assignment operators
| operator | Description |
|---|---|
| = | Assignment |
| += | Addition Assignment |
| -= | Subtraction Assignment |
| *= | Multiplication Assignment |
| /= | Division Assignment |
| %= | Modulo Assignment |
| &= | Bitwise AND Assignment |
| |= | Bitwise OR Assignment |
| ^= | Bitwise XOR Assignment |
| <<= | Left Shift Assignment |
| >>= | Right Shift Assignment |
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
var a
:
i32
=
5
;
const
b
:
i32
=
3
;
a
=
b
;
// Equivalent to a = a + b;
std.
debug
.
(
"a += b: {}\n"
, .
{
a
}
)
;
a
=
b
;
// Equivalent to a = a - b;
std.
debug
.
(
"a -= b: {}\n"
, .
{
a
}
)
;
a
=
b
;
// Equivalent to a = a * b;
std.
debug
.
(
"a *= b: {}\n"
, .
{
a
}
)
;
a
=
@divTrunc
(
a, b
)
;
// Equivalent to a = a / b;
std.
debug
.
(
"a /= b: {}\n"
, .
{
a
}
)
;
a
=
@mod
(
a, b
)
;
// Equivalent to a = a % b;
std.
debug
.
(
"a %= b: {}\n"
, .
{
a
}
)
;
a
&
=
b
;
// Equivalent to a = a & b;
std.
debug
.
(
"a &= b: {}\n"
, .
{
a
}
)
;
a
|
=
b
;
// Equivalent to a = a | b;
std.
debug
.
(
"a |= b: {}\n"
, .
{
a
}
)
;
a
^
=
b
;
// Equivalent to a = a ^ b;
std.
debug
.
(
"a ^= b: {}\n"
, .
{
a
}
)
;
a
<<=
1
;
// Equivalent to a = a << 1;
std.
debug
.
(
"a <<= 1: {}\n"
, .
{
a
}
)
;
a
=
1
;
// Equivalent to a = a >> 1;
std.
debug
.
(
"a >>= 1: {}\n"
, .
{
a
}
)
;
}
The compilation output result is:
a += b: 8
a -= b: 5
a *= b: 15
a /= b: 5
a %= b: 2
a &= b: 2
a |= b: 3
a ^= b: 0
a <<= 1: 0
a >>= 1: 0
Other operators
| operator | Description |
|---|---|
| ++ | Increment |
| -- | Decrement |
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
var a
:
i32
=
5
;
a
=
1
;
// Zig has no ++ operator; you can use += 1 instead
std.
debug
.
(
"a += 1: {}\n"
, .
{
a
}
)
;
a
=
1
;
// Zig has no -- operator; you can use -= 1 instead
std.
debug
.
(
"a -= 1: {}\n"
, .
{
a
}
)
;
}
The compilation output result is:
a += 1: 6
a -= 1: 5
Operator Precedence
The following is the Zig operator precedence list, arranged from highest to lowest:
| Precedence | operator | Description |
|---|---|---|
| 1 | [] |
Subscript operation, array or pointer access |
| 1 | . |
Member access |
| 2 | fn_call() |
Function call |
| 2 | @builtin() |
Built-in function call |
| 3 | ! |
Error Propagation |
| 3 | ? |
Optional value unwrapping |
| 4 | * & |
Pointer dereference and address operation |
| 5 | + - |
Unary plus and minus |
| 6 | ~ |
Bitwise NOT |
| 7 | * / % |
Multiplication, division, modulo |
| 8 | + - |
Addition, subtraction |
| 9 | << >> |
Bitwise left shift, right shift |
| 10 | & |
Bitwise AND |
| 11 | ^ |
Bitwise XOR |
| 12 | ` | ` |
| 13 | == != |
Equality, inequality |
| 13 | < <= > >= |
Less than, less than or equal to, greater than, greater than or equal to |
| 14 | and |
Logical AND |
| 15 | or |
Logical OR |
| 16 | orelse |
or returns another value |
| 17 | catch |
Catch error |
| 18 | = |
Assignment |
| 19 | -> |
Closure function body or return value type indicator |
Explanation:
- Associativity: Usually Zig operators are left-associative, but you can refer to the documentation based on the specific functionality of the operator.
- Notes: Zig's operator design is simple and clear, avoiding the complex implicit behaviors found in many languages, such as no implicit type conversion.
The following example shows how operator precedence affects the evaluation order of expressions:
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
const
a
:
i32
=
5
;
const
b
:
i32
=
3
;
const
c
:
i32
=
2
;
// Multiplication takes precedence over addition
const
result1
:
i32
=
a
b
c
;
// 5 + (3 * 2) = 11
std.
debug
.
(
"a + b * c = {}\n"
, .
{
result1
}
)
;
// Use parentheses to change precedence
const
result2
:
i32
=
(
a
b
)
c
;
// (5 + 3) * 2 = 16
std.
debug
.
(
"(a + b) * c = {}\n"
, .
{
result2
}
)
;
// Comparison operators take precedence over logical operators
const
result3
:
bool
=
a
b and b
c
;
// (5 > 3) and (3 > 2) = true
std.
debug
.
(
"a > b and b > c = {}\n"
, .
{
result3
}
)
;
// Logical NOT takes precedence over logical AND
const
result4
:
bool
=
!
(
a
b
)
and b
c
;
// !(5 > 3) and (3 > 2) = false
std.
debug
.
(
"!(a > b) and b > c = {}\n"
, .
{
result4
}
)
;
}
The compilation output result is:
a + b * c = 11
(a + b) * c = 16
a > b and b > c = true
!(a > b) and b > c = false