Math Tools Math Tools

How Computers Represent Numbers

How Computers Represent Numbers

By Math Tools ·

How Computers Represent Numbers

On June 4, 1996, the European Space Agency's first Ariane 5 rocket veered off course and self-destructed 37 seconds after launch. The cause wasn't a mechanical failure. A piece of software tried to squeeze a 64-bit floating-point number into a 16-bit integer. The value was too big, the conversion failed, and the guidance system shut down.

Numbers inside a computer aren't the ideal, infinite numbers of mathematics. They're fixed-size patterns of bits, and knowing their limits is part of writing correct software.


127 + 1 Can Equal −128

In an 8-bit signed integer, the largest value is 127. Add 1 and you don't get 128. You get −128.

  01111111   (127)
+ 00000001   (1)
= 10000000   (−128)

That's integer overflow, and it's not a bug in the CPU. It's exactly how the representation is defined. The number line has been bent into a circle.


Unsigned Integers: Plain Binary

The simplest representation is unsigned binary. Each bit is a power of 2:

1011₂ = 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 8 + 0 + 2 + 1 = 11

With n bits you can store 2ⁿ values, from 0 to 2ⁿ − 1:

Bits Unsigned range
8 0 to 255
16 0 to 65,535
32 0 to 4,294,967,295
64 0 to 18,446,744,073,709,551,615

That 8-bit limit of 255 is why color channels in images run from 0 to 255. Try converting values yourself with the binary to decimal converter.


Negative Numbers: Two's Complement

How do you store −5? An obvious idea is a sign bit: use the first bit for + or −. But that creates two zeros (+0 and −0) and makes addition circuits complicated.

Nearly every modern computer uses two's complement instead. To negate a number, flip all the bits and add 1:

 5 = 00000101
flip → 11111010
 +1  → 11111011 = −5

Now ordinary binary addition just works:

  00000101   (5)
+ 11111011   (−5)
= 00000000   (0, carry discarded)

With n bits, two's complement stores −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. For 32 bits that's −2,147,483,648 to 2,147,483,647.

Mathematically, two's complement is modular arithmetic mod 2ⁿ. The bit pattern for −5 is the same as for 251, because −5 ≡ 251 (mod 256).


A Famous Overflow: Gangnam Style

In December 2014, YouTube announced that PSY's "Gangnam Style" video had been viewed so many times it exceeded the maximum value of a signed 32-bit integer: 2,147,483,647. YouTube had already upgraded its view counter to 64 bits, which can count past 9.2 quintillion.

A related issue is the Year 2038 problem. Many systems store time as a signed 32-bit count of seconds since January 1, 1970. That counter overflows at 03:14:07 UTC on January 19, 2038.


Fractions: Floating Point

Integers can't hold 3.14 or 6.02 × 10²³. For that, computers use floating-point numbers, standardized as IEEE 754 in 1985. It's scientific notation in binary:

value = (−1)^sign × significand × 2^exponent

A 64-bit "double" divides its bits into:

  • 1 sign bit
  • 11 exponent bits
  • 52 fraction bits (the significand)

That gives about 15–17 significant decimal digits and a range up to about 1.8 × 10³⁰⁸.

The standard's lead designer, William Kahan of UC Berkeley, received the 1989 Turing Award for his work on numerical computation. Before IEEE 754, the same program could produce different answers on different brands of computers.


Why 0.1 Isn't Exact

In decimal, 1/3 = 0.333… repeats forever. In binary, 1/10 repeats forever:

0.1₁₀ = 0.000110011001100110011…₂

A float must cut it off after 52 bits, so the stored value is very slightly off. That's why in most programming languages:

0.1 + 0.2 = 0.30000000000000004

Only fractions whose denominators are powers of 2 (like ½, ¼, ⅜) are exact in binary floating point. For money, programmers use integers of cents or decimal types instead.


Special Values

IEEE 754 also defines values ordinary math doesn't have:

  • +∞ and −∞, from results like 1.0 / 0.0
  • NaN ("Not a Number"), from 0.0 / 0.0 or √−1
  • −0.0, a negative zero that compares equal to +0.0

NaN has a strange property: NaN ≠ NaN. It's the only value that isn't equal to itself, which is how programmers test for it.


Two Concepts Worth Knowing

Positional Notation

Every number system here, decimal, binary or hexadecimal, uses positional notation: each digit is multiplied by a power of the base. The table of bases shows the same numbers in many bases side by side.

Precision vs. Range

Adding bits to the exponent extends range. Adding bits to the significand improves precision. Every floating-point format is a trade-off between the two, which is why AI hardware now uses compact 16-bit and even 8-bit float formats.


Quick Answer: How Do Computers Store Numbers?

Computers store whole numbers as fixed-width binary integers, using two's complement for negative values. Fractional numbers are stored as IEEE 754 floating point, a binary form of scientific notation with a sign bit, an exponent and a significand. Both have limited range and precision.


Try Them Yourself

Convert 255, 256 and 65,535 to binary. Once you see the bit patterns, overflow stops being mysterious.