0Pricing
Zig Academy · Lesson

Overflow: + vs +% vs +|

Trapping, wrapping, and saturating math.

Overflow: + vs +% vs +| is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Numbers Have Limits

Every sized integer has a maximum it can hold. Push past it and you hit overflow, the moment a value tries to grow beyond its range.

Zig Catches Overflow

By default, the plain + operator traps overflow. In a safe build, going past the limit stops your program instead of corrupting the value.

var x: u8 = 255;
x = x + 1; // panics: overflow

Why Trapping Helps

A loud crash beats a silent wrong answer. Trapping turns a sneaky bug into an obvious failure you can find and fix fast.

Wrapping Math with +%

Sometimes you want the value to roll over on purpose. The +% operator wraps around, so 255 + 1 in a u8 becomes 0.

var x: u8 = 255;
x = x +% 1; // 0

When Wrapping Is Right

Wrapping shines for counters, hashes, and checksums where roll-over is the intended behavior. You ask for it with the % suffix, never by accident.

Saturating Math with +|

The +| operator clamps to the limit instead of wrapping. A u8 at 255 stays 255, perfect for values that must not exceed a ceiling.

var x: u8 = 255;
x = x +| 1; // 255

Three Choices, One Decision

So + traps, +% wraps, and +| saturates. You pick the behavior that matches your intent, making overflow handling explicit in your code.

The Same Trio for Subtraction

These suffixes are not just for addition. -, -%, and -| give you trapping, wrapping, and saturating subtraction the very same way.

var x: u8 = 0;
x = x -| 1; // 0

And for Multiplication

Multiplication follows the pattern too: *, *%, and *|. The single rule, choose your overflow behavior, applies across the arithmetic operators.

const big = a *% b; // wrap on overflow

Default Builds Stay Safe

In Debug and ReleaseSafe modes, plain operators check for overflow. You only opt out where you have a real reason to wrap or saturate.

Intent Lives in the Code

Because each behavior has its own symbol, a reader sees your overflow intent at a glance. Nothing is assumed; the operator spells it out.

Quick Check

You want a u8 counter to roll over from 255 back to 0. Which operator?

Recap: Pick Your Overflow

Plain math traps, the % suffix wraps, and the | suffix saturates. Choose deliberately, and overflow is never a hidden surprise. 🛡️

Frequently asked questions

Is the “Overflow: + vs +% vs +|” lesson free?

Yes — the full text of “Overflow: + vs +% vs +|” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Overflow: + vs +% vs +|”?

Trapping, wrapping, and saturating math. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Overflow: + vs +% vs +|” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic and Comparison Operators
  2. Overflow: + vs +% vs +|
  3. Bitwise Operators
  4. Boolean Logic and Short-Circuiting
← Back to Zig Academy