Zig Operators
Operators combine values: add them, compare them, shift their bits. Most of Zig's operators look like C's, but Zig adds a family of wrapping and saturating operators that let you state exactly what should happen at the edges of an integer's range.
Arithmetic
| Operator | Meaning | Example (a = 17, b = 5) |
|---|---|---|
+ |
addition | 22 |
- |
subtraction | 12 |
* |
multiplication | 85 |
/ |
division (unsigned or float) | 3 |
% |
remainder (unsigned or float) | 2 |
-x |
negation | -17 |
const std = @import("std");
pub fn main() void {
const a: u32 = 17;
const b: u32 = 5;
std.debug.print("{d} {d} {d} {d} {d}\n", .{ a + b, a - b, a * b, a / b, a % b });
const x: f64 = 7.0;
std.debug.print("{d} {d}\n", .{ x / 2.0, -x });
}22 12 85 3 2
3.5 -7
There is no ++ or -- in Zig. Write i += 1.
Compound assignment
+=, -=, *=, /=, %=, and the bitwise versions &=, |=, ^=, <<=, >>= update a variable in place.
Wrapping and saturating operators
Plain +, - and * treat overflow as a bug and panic in safe build modes. When overflow is the intended behaviour, use the special forms:
| Normal | Wrapping (%) |
Saturating (|) |
|---|---|---|
a + b |
a +% b |
a +| b |
a - b |
a -% b |
a -| b |
a * b |
a *% b |
a *| b |
a << b |
— | a <<| b |
Wrapping means the value goes around like a car's odometer: for a u8, 255 +% 1 is 0. Saturating means the value sticks at the limit: 255 +| 1 stays 255, and 0 -| 1 stays 0.
const std = @import("std");
pub fn main() void {
const hp: u8 = 10;
std.debug.print("hp -| 25 = {d}\n", .{hp -| 25});
const volume: u8 = 240;
std.debug.print("volume +| 50 = {d}\n", .{volume +| 50});
var hash: u8 = 200;
hash *%= 3; // 600 mod 256
std.debug.print("hash = {d}\n", .{hash});
const counter: u8 = 0;
std.debug.print("0 -% 1 = {d}\n", .{counter -% 1});
}hp -| 25 = 0
volume +| 50 = 255
hash = 88
0 -% 1 = 255
Saturation is ideal for health bars, volume sliders and colour channels. Wrapping is the right tool for hash functions, checksums and ring buffers.
Overflow-reporting builtins
To detect overflow and react to it, use @addWithOverflow, @subWithOverflow or @mulWithOverflow. They return a tuple of the (wrapped) result and a u1 overflow bit:
const std = @import("std");
pub fn main() void {
const a: u8 = 200;
const result = @addWithOverflow(a, 100);
std.debug.print("value={d} overflowed={d}\n", .{ result[0], result[1] });
const ok = @addWithOverflow(a, 10);
std.debug.print("value={d} overflowed={d}\n", .{ ok[0], ok[1] });
}value=44 overflowed=1
value=210 overflowed=0
Comparison and logic
Comparison operators (==, !=, <, <=, >, >=) return bool. Logical operators are the keywords and, or and ! (see the Booleans lesson).
Bitwise operators
| Operator | Meaning | 0b1100 op 0b1010 |
|---|---|---|
& |
AND | 0b1000 |
| |
OR | 0b1110 |
^ |
XOR | 0b0110 |
~ |
NOT (flip all bits) | — |
<<, >> |
shift left / right | — |
const std = @import("std");
pub fn main() void {
const a: u8 = 0b1100;
const b: u8 = 0b1010;
std.debug.print("and={b:0>4} or={b:0>4} xor={b:0>4}\n", .{ a & b, a | b, a ^ b });
std.debug.print("not a = {b:0>8}\n", .{~a});
std.debug.print("1 << 5 = {d}, 200 >> 3 = {d}\n", .{ @as(u8, 1) << 5, @as(u8, 200) >> 3 });
}and=1000 or=1110 xor=0110
not a = 11110011
1 << 5 = 32, 200 >> 3 = 25
The shift amount must have a type small enough to be a valid shift: for a u8 it must be a u3 (0–7). Shifting by a constant is fine; a runtime shift amount may need @intCast.
Precedence
Zig's precedence is mostly what you expect (* before +, comparisons before and, and before or). One difference from C fixes a classic bug: bitwise operators bind tighter than comparisons, so flags & mask != 0 means (flags & mask) != 0. Comparisons also cannot be chained — a < b < c is a compile error; write a < b and b < c.
Tip: When in doubt, add parentheses. They cost nothing and make intent clear.
Test yourself
For a u8 with value 250, what is x +| 10?
- 4
- 260
- 255
- panic
For a u8 with value 0, what is x -% 1?
- 0
- -1
- 255
- panic
How do you increment i by one in Zig?
- i++
- ++i
- i += 1
- inc(i)