In C++, why is INT_MAX + 1 undefined behaviour while UINT_MAX + 1u is well-defined, and did C++20's two's-complement rule change that?
answer
- unsigned types cannot overflow
- arithmetic modulo 2 to the N
- result not representable: undefined
- C++20 fixed representation, not arithmetic
- uint16_t times uint16_t promotes
basics
~20 sUnsigned arithmetic is defined modulo 2^N, so UINT_MAX + 1u is 0. A signed result that does not fit its type is undefined behaviour. C++20 made two's complement mandatory and defined out-of-range conversions, but left signed overflow undefined.
solid answer
~40 sThe standard says unsigned arithmetic is performed **modulo 2^N**, so unsigned types never overflow: `UINT_MAX + 1u` is exactly `0`. For signed types the rule is general: if an expression's result is not representable in its type, the behaviour is **undefined**, so `INT_MAX + 1` has no defined result at all, not even `INT_MIN`. C++20 required two's-complement representation and, with it, made conversion to a signed type defined modulo 2^N (`static_cast<int>(4294967295u)` is `-1`) and defined `<<` on negative values; it did **not** make signed `+`, `-` or `*` wrap. Integral promotion widens the trap: two `std::uint16_t` values promote to `int`, so `65535 * 65535` is signed overflow on a 32-bit `int`. Constant evaluation must diagnose this, so a `constexpr` test catches it at compile time.
code
cpp · 17 lines#include <climits>
#include <cstdint>
#include <cstdio>
constexpr unsigned add_u(unsigned a, unsigned b) { return a + b; }
constexpr int add_i(int a, int b) { return a + b; }
static_assert(add_u(UINT_MAX, 1u) == 0u); // unsigned: wraps by definition
// static_assert(add_i(INT_MAX, 1) < 0); // rejected: overflow is UB
int main() {
int n = static_cast<int>(4294967295u); // C++20: defined, -1
int s = -1 << 1; // C++20: defined, -2
std::uint16_t a = 65535, b = 65535;
long long safe = static_cast<long long>(a) * b; // widen before multiplying
std::printf("%d %d %lld %d\n", n, s, safe, add_i(2, 3));
}go deeper
Recall that unsigned arithmetic wraps modulo 2^N by definition while a signed result that does not fit is undefined behaviour, not INT_MIN.
Explain what C++20's two's-complement rule changed (conversions, shifts of negative values) and what it left undefined, and show the integral-promotion trap with small unsigned types.
Show how you prevent it in real code: pre-checks against INT_MAX - b, widening before the multiply, constexpr tests that reject overflow, and a sanitizer build in CI.
Weigh choosing unsigned, wider or checked arithmetic as a codebase convention against readability and performance, and decide where overflow must be impossible by construction.
## Two different rules for two families of types C++ treats unsigned and signed integers differently on purpose. - **Unsigned types** follow modular arithmetic: the standard says arithmetic for an unsigned type with width N is performed **modulo 2^N**, and its own note adds that unsigned arithmetic does not overflow. `UINT_MAX + 1u` is `0`, and `0u - 1u` is `UINT_MAX`, on every implementation. - **Signed types** fall under a general rule for all expressions: if the result is not mathematically defined or not in the range of representable values for its type, the behaviour is undefined. `INT_MAX + 1` therefore has **no** result, and the program has no meaning from that point on. "It wraps to `INT_MIN`" is what the hardware's add instruction would do, not what the language promises. Compilers rely on the difference: because signed overflow cannot happen in a valid program, they may treat `x + 1 > x` as always true for an `int` (the lesson on optimizer assumptions shows the consequences). ## What C++20 changed, and what it did not Before C++20 the standard allowed three signed representations: two's complement, ones' complement and sign-magnitude. C++20 requires **two's complement**, and several rules that hedged around representation became exact: | Operation (32-bit `int`) | Before C++20 | Since C++20 | |---|---|---| | `static_cast<int>(4294967295u)` | implementation-defined | `-1`, the value congruent modulo 2^32 | | `-1 << 1` | undefined | `-2` | | `INT_MIN >> 1` | implementation-defined | arithmetic shift, rounds towards negative infinity | | `INT_MAX + 1` | undefined | **still undefined** | | `INT_MIN / -1` | undefined | **still undefined** | The representation is fixed; the arithmetic rule is not. A program that "knows" `int` is two's complement still may not overflow it. ## The other signed traps Signed overflow hides in more places than an obvious `+ 1`: 1. **Integral promotion.** Operands narrower than `int` are promoted to `int` before arithmetic. Two `std::uint16_t` values of 65535 multiply as `int`, and 4294836225 does not fit a 32-bit `int`, so the "unsigned" product is signed overflow. 2. **Negation and division.** `-x` and `x / -1` overflow when `x` is `INT_MIN`, and so does `x % -1`. 3. **Shifts.** A shift count that is negative or not smaller than the width of the promoted left operand is undefined for both signed and unsigned types. 4. **Accumulators.** A loop summing `int` values over a large input overflows long before anyone notices; widening the result after the loop is too late. ## Catching it before it ships - **Constant evaluation** must reject undefined behaviour in core-language operations. Apple clang 21 refuses `static_assert(add(INT_MAX, 1) < 0)` for `constexpr int add(int a, int b)` with the note `value 2147483648 is outside the range of representable values of type 'int'`. A `constexpr` unit test is therefore a cheap overflow detector. - **Check before the operation**, not after: `if (b > 0 && a > INT_MAX - b)` tests for overflow without performing it. - **Use an unsigned or wider type deliberately** when wrapping or a larger range is the intended meaning, and cast before the multiply, not after it. - **Run a sanitizer build**; the material on sanitizers covers the flag that traps signed overflow at run time. ## What "undefined" permits here Interviewers often follow up with "so what actually happens?". The honest answer is that the language no longer constrains the program, and real builds show the whole range: - a debug build usually performs the hardware add and shows `INT_MIN`, which is why people believe it wraps; - an optimized build may have removed the comparison that was meant to detect the overflow, because the compiler assumed it could not happen; - a sanitizer build stops with a runtime error at the faulting line; - a constant-evaluated expression refuses to compile. All four are conforming, which is exactly why "it printed `INT_MIN` on my machine" proves nothing about the code. ## Why the language keeps it undefined Two's complement is now universal, yet the committee kept overflow undefined. Defining it as wrapping would make every accidental overflow a silent, valid wrong answer that tools could no longer flag as a bug, and it would forbid optimizations that assume an `int` loop counter never wraps. Unsigned types exist for when wrapping is what you mean.
- Why is multiplying two std::uint16_t values in C++ a possible signed overflow?Integral promotion converts operands narrower than `int` to `int` before the arithmetic, so the multiply happens in signed `int`. With a 32-bit `int`, 65535 * 65535 = 4294836225 exceeds `INT_MAX`, which is undefined behaviour even though both operands were unsigned. Cast one operand to `std::uint32_t` or a wider type before multiplying.
- How do you test for overflow before adding two ints in C++?Compare against the limit without performing the add: overflow happens when `b > 0 && a > INT_MAX - b`, or when `b < 0 && a < INT_MIN - b`. Testing the sum afterwards, as in `a + b < a`, already executed the undefined operation. GCC and Clang also provide `__builtin_add_overflow`, and C++26 adds `ckd_add` in `<stdckdint.h>`.
An unsigned integer is a car odometer: past 999999 it rolls over to 000000 by design. A signed integer has no rollover mechanism at all; driving past the end is not a rollover but a broken instrument.
- Signed overflow is fine since C++20, because int is now guaranteed two's complement.
- INT_MAX + 1 always evaluates to INT_MIN on modern hardware.
- Unsigned overflow is undefined behaviour, just like signed overflow.
- Two std::uint16_t operands are multiplied as unsigned, so the product cannot overflow.
- Casting the product to long long afterwards fixes an int multiplication overflow.
Assumes C++20 or later: two's complement and defined out-of-range signed conversions arrived in C++20; signed overflow is undefined in every standard.