0Pricing
Zig Academy · Lektion

Überlauf: + vs +% vs +|

Mathematik mit Abbruch, Überlauf und Sättigung.

Überlauf: + vs +% vs +| ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🛡️

Häufig gestellte Fragen

Ist die Lektion „Überlauf: + vs +% vs +|“ kostenlos?

Ja — der vollständige Text von „Überlauf: + vs +% vs +|“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Überlauf: + vs +% vs +|“?

Mathematik mit Abbruch, Überlauf und Sättigung. Du übst Zig Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Zig Academy zu starten?

Keine Vorkenntnisse erforderlich. Zig Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Überlauf: + vs +% vs +|“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Zig Academy-Lektion Code schreiben und ausführen?

Ja. Jede Zig Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Arithmetische und Vergleichsoperatoren
  2. Überlauf: + vs +% vs +|
  3. Bitweise Operatoren
  4. Boolesche Logik und Kurzschlussauswertung
← Zurück zu Zig Academy