LOGBOOK

HELP

Quiz Entry - updated: 2026.07.07

Why is unsigned arithmetic well-defined but signed overflow undefined in C?

Unsigned overflow is defined as wrapping (mod 2^w); signed overflow is left undefined so compilers can optimize freely and so the standard fits historical non-two's-complement hardware.

Unsigned: Defined as modular arithmetic (mod 2^w) - always wraps predictably.

Signed: Undefined behavior allows:

  • Compiler optimizations assuming no overflow
  • Compatibility with non-two's complement machines (historical)
  • Trapping on overflow (some architectures)

Practical consequence:

// Compiler might optimize this to always true!
int x;
// Could be removed!
if (x + 1 > x) { ... }

Safe practice: Use unsigned for bit manipulation; check for overflow explicitly with signed integers.

From Quiz: REVE1 / Number Representations | Updated: Jul 07, 2026