toolery

How number systems work

Why computers count in binary, and how binary, octal, and hex relate to the decimal numbers we use every day.

Every number system is just a different way of grouping quantities. Decimal groups in tens because we have ten fingers. Binary groups in twos because a transistor only has two reliable states: off and on.

Why binary

A computer’s memory is built from switches. Each switch is either off (0) or on (1), so binary is not a design choice, it is a description of the hardware itself. A single switch is a bit. Eight of them together, a byte, can represent 256 distinct values, from 0 to 255.

Reading a binary number

Each position in a binary number represents a power of two, the same way each position in a decimal number represents a power of ten:

1010 in binary
= (1 × 8) + (0 × 4) + (1 × 2) + (0 × 1)
= 10 in decimal

Where hex and octal fit in

Hexadecimal (base 16) and octal (base 8) exist because binary is long and easy to misread. Four binary digits map exactly to one hex digit, so hex is really just a shorthand for binary that is easier for a human to type and check. That is why you will see colors written as hex codes like #e8734a, and memory addresses written in hex rather than binary.

Try it yourself in the number base converter.