Integers & Overflow
Data Representation · Data & Hashing track
6 min read
In this module
How Computers Store Whole Numbers
Earlier modules covered binary and two's complement. Now let's see how programming languages and hardware use those ideas to store integers of different sizes, and what happens when a number outgrows its container.
Signed vs. Unsigned
An unsigned integer treats every bit as part of the magnitude. An 8-bit unsigned value runs 0 to 255. A signed integer reserves the top bit to mark the sign (using two's complement), so an 8-bit signed value runs -128 to 127. Same number of distinct values either way, 256, but the range slides.
Worked Example: One Byte, Two Readings
Take the byte 0xC8, which is 11001000 in binary.
- Read as unsigned: 128 + 64 + 8 = 200.
- Read as signed: the top bit is set, so it's negative. Two's complement says the value is the unsigned reading minus 256, so 200 − 256 = −56.
The bits in memory didn't change between those two lines. Only the type you read them through did. In C, that's the difference between unsigned char and signed char, and the compiler picks different instructions for comparisons depending on which one you declared.
Common Widths
Hardware typically provides integer types in power-of-two widths:
| Width | Unsigned Range | Signed Range |
|---|---|---|
| 8-bit | 0 to 255 | -128 to 127 |
| 16-bit | 0 to 65,535 | -32,768 to 32,767 |
| 32-bit | 0 to ~4.29 billion | -2,147,483,648 to 2,147,483,647 |
| 64-bit | 0 to ~18.4 quintillion | -9.2 quintillion to 9.2 quintillion |
Wider integers hold larger values but burn more memory. Picking the right width is a constant trade-off in systems programming.
Integer overflow occurs when an arithmetic operation produces a value outside the range that the integer's bit width can represent. The result silently wraps around: the extra bits are discarded, and you get a value that looks completely wrong.
Overflow in Practice
Take an unsigned 8-bit counter sitting at 255. Add 1. In binary, 11111111 + 1 = 100000000, a 9-bit result. You only have 8 bits, so the leading 1 falls off and the stored value snaps to 00000000. Counter went from 255 to 0.
Same trick on signed integers. A signed 8-bit value at 127 (01111111) plus 1 lands on 10000000, which two's complement reads as -128. Largest positive value to smallest negative, in one cycle.
Do both by hand on the visualisers below. The first is an unsigned byte holding 255. Adding 1 to 11111111 turns every 1 into a 0 and carries out of the top, so press Clear and you've got the answer. The second reads its byte as signed and holds 127. Adding 1 to 01111111 clears the seven low bits and sets the top one; do that by clicking and watch the decimal read-out jump from 127 to −128 while the hex just goes from 0x7F to 0x80.
The two bytes follow exactly the same binary rules. The only difference is which bit pattern the type considers to be the edge.
Predict: uint8_t x = 250; x += 10; What does x hold afterwards?
4. The true answer, 260, needs nine bits. Unsigned arithmetic wraps modulo 256, so you get 260 − 256 = 4. For unsigned types in C, this wrapping is fully defined behaviour: the language promises it.
Widening and Narrowing
Overflow also sneaks in when a value moves between types of different widths.
Narrowing throws away the high bits. Store the int 300 into an unsigned char and only the low eight bits survive: 300 − 256 = 44. No warning at runtime, and often none at compile time unless you ask for one with -Wconversion.
Widening has to invent the new high bits, and the rule depends on the source type. A signed char holding −56 (0xC8) widens to the int 0xFFFFFFC8, still −56, because the sign bit gets copied upwards. That's sign extension. An unsigned char holding 200 (0xC8) widens to 0x000000C8, still 200, padded with zeros. Same starting byte, different results, all decided by the declared type.
When a length read from a file is stored as a signed byte and then widened to a size_t, that's exactly how −1 becomes 18 quintillion and a bounds check waves through a giant copy. You'll meet that bug properly in Integer Overflows & Type Confusion.
Why Overflow Matters
Overflow bugs have caused very real damage. Ariane 5's first flight in 1996 was destroyed when a 64-bit floating-point value was converted to a 16-bit signed integer that couldn't hold it. In 2015 the FAA ordered Boeing 787 operators to power-cycle the aircraft's generator control units regularly: left on for 248 days straight, they could shut down mid-flight. A counter of hundredths of a second overflows a signed 32-bit integer after 2³¹ ticks, which works out at about 248.6 days.
Languages handle this differently. Rust catches overflow in debug builds and panics; C and C++ treat signed overflow as undefined behaviour, so the compiler is allowed to assume it never happens and optimise accordingly. Understanding overflow is non-negotiable for writing correct, safe low-level code.
Predict, Then Check
Four quick ones. Commit to an answer before you open each, then run the program to confirm.
1. signed char sc = (signed char)0x80; int widened = sc; What's widened, in decimal and in hex?
−128, or 0xFFFFFF80. As a signed byte, 0x80 is −128. Widening copies the sign bit into all 24 new high bits, so the value survives the trip.
2. What are (unsigned char)300 and (unsigned char)-1?
44 and 255. Narrowing keeps the low eight bits, so 300 − 256 = 44. −1 is all ones at any width, and eight ones read as unsigned is 255.
3. Is -1 < 1u true or false?
False. When a signed int meets an unsigned int in a comparison, C converts the signed one to unsigned. −1 becomes 4,294,967,295, which is not less than 1. GCC warns about this with -Wextra (it's -Wsign-compare), and plenty of broken bounds checks have shipped from code built without it.
4. uint32_t zero = 0; uint32_t below = zero - 1; What's below?
4,294,967,295, which is 2³² − 1. Unsigned arithmetic wraps modulo 2³², and the language guarantees it.