Computing

Integer Overflow Explained With 8-Bit Examples

Integer overflow happens when a result does not fit its type. 8-bit wraparound on live bits, plus what C, JavaScript and Python did in our tests.

Integer overflow happens when the result of a calculation is too large or too small for the integer type that stores it. With 8 bits, unsigned 255 + 1 wraps to 0, and signed 127 + 1 wraps to −128. Whether that wrap is allowed, trapped or undefined depends on the language.

Try itPress +1 at 01111111 (signed overflow) or at 11111111 (unsigned overflow). Flags appear when either reading wraps.

64 + 32 + 16 + 8 + 4 + 2 + 1

Unsigned 127 Signed (two’s complement) 127 Hex 0x7F

The boundaries to watch

TypeMinimumMaximumMax + 1 wraps to
8-bit unsigned02550
8-bit signed−128127−128
16-bit unsigned065,5350
16-bit signed−32,76832,767−32,768
32-bit unsigned04,294,967,2950
32-bit signed−2,147,483,6482,147,483,647−2,147,483,648

Going below the minimum (0 − 1 for unsigned) wraps the other way, to the maximum. Some people call that underflow; strictly, underflow is a floating-point term, and integer results on either side are overflow.

Why it wraps: the bits run out

Adding 1 to 1111 1111 produces 1 0000 0000, a 9-bit result. An 8-bit register keeps only the low eight bits, 0000 0000. The carry out of the top bit is lost (the processor records it in a carry flag). For signed values the bits are identical, but 0111 1111 + 1 = 1000 0000 crosses into the negative half of the two’s complement range, so 127 becomes −128.

That is why overflow is easy to spot in the bits: unsigned overflow sets the carry out of the top bit; signed overflow happens when two numbers with the same sign produce a result with the opposite sign.

What languages actually do

We ran the same boundary cases in three languages. This is a C program compiled with clang’s undefined-behavior sanitizer:

// excerpt of overflow.c (full program prints each result)
uint8_t u = 255; u = u + 1;        // unsigned: defined wraparound
int8_t s = 127;  s = (int8_t)(s + 1);
unsigned int ui = UINT_MAX; ui = ui + 1;
volatile int i = INT_MAX; int j = i + 1;   // signed int overflow
$ clang -O0 -fsanitize=undefined -o ov overflow.c && ./ov
overflow.c:12:15: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
uint8_t 255 + 1 = 0
int8_t (int8_t)(127 + 1) = -128
UINT_MAX + 1 = 0
INT_MAX + 1 = -2147483648
  • C and C++: unsigned arithmetic is defined to wrap. Signed overflow is undefined behavior: the standard places no requirement on the result, and optimizers assume it never happens. The sanitizer flagged only the int addition. The int8_t line did not overflow int (127 + 1 is computed as an int); the narrowing conversion back to 8 bits produced −128 on clang. Compiling with -fwrapv asks the compiler to define signed overflow as wraparound; our -O2 -fwrapv build printed the same −2147483648.
  • JavaScript: ordinary numbers are 64-bit floats and do not wrap, but typed arrays and bitwise operators do. Node.js 25 printed Uint8Array 255+1 -> 0, Int8Array 127+1 -> -128, and ((2**31-1)+1)|0 gave −2147483648. Plain numbers silently lose precision above 2⁵³ instead: 2**53 + 1 printed 9007199254740992.
  • Python: integers are unbounded, so 2**63 - 1 + 1 printed 9223372036854775808; nothing overflows. Fixed-width types from ctypes or NumPy do wrap: ctypes.c_int8(128).value returned −128.

Other languages make different choices: Rust panics on overflow in debug builds and wraps in release builds unless you call checked_add or wrapping_add; Java wraps silently and offers Math.addExact to throw instead.

Why it matters

  • Wrong results that look plausible. A total that exceeds 2,147,483,647 in a 32-bit counter turns into a large negative number.
  • Security bugs. If a buffer size is computed as count * size and the multiplication wraps, the program allocates a small buffer and then writes a large amount of data into it.
  • Time and counters. Systems that count milliseconds or ticks in a fixed-width integer eventually wrap. A signed 32-bit count of seconds since 1970 runs out on 19 January 2038.

How to prevent it

  1. Pick a wide enough type. If a value can exceed 2³¹ − 1, use 64 bits.
  2. Check before the operation. For addition: if a > MAX − b, the sum would overflow.
  3. Use checked arithmetic where available: __builtin_add_overflow in GCC and Clang, checked_add in Rust, Math.addExact in Java.
  4. Test with a sanitizer. -fsanitize=undefined caught the signed overflow in our run immediately.

The binary addition calculator shows the carry out of the top bit, and the signed binary converter shows how a wrapped pattern reads as a negative number.

Sources

  1. Arithmetic operators (overflows) — cppreference.com. unsigned arithmetic wraps; signed overflow is undefined behavior
  2. UndefinedBehaviorSanitizer — LLVM/Clang documentation
  3. Numeric types — int, float, complex — Python documentation. integers have unlimited precision

How we made this

Measured. A small C program was compiled with Apple clang 21 three ways (with -fsanitize=undefined, and with -O2 -fwrapv); typed-array cases were run in Node.js 25 and the unbounded-integer case in Python 3.14.3. The outputs below are pasted verbatim.

Performed by Claude, an AI assistant working in the BinConvert repository (Apple Silicon Mac, Apple clang 21.0.0, Node.js 25, Python 3.14.3), November 16, 2026.

This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; the C, JavaScript and Python examples were re-run and their output compared. Review: automated test suite and source check, November 16, 2026.

Spotted an error? Tell us and we will correct it.

Copied to clipboard!