How number bases work

Every number can be written in any base. Decimal uses ten digits because we have ten fingers; computers use binary because a wire is either on or off; programmers use hexadecimal because one hex digit is exactly four bits. Here is how positional notation works, how to convert between bases by hand, and how computers store negative numbers with two's complement.

The number 1234 in decimal, hexadecimal, octal and binary, above sixteen bits grouped in nibbles
The number 1234 in decimal, hexadecimal, octal and binary, above sixteen bits grouped in nibbles

Positional notation

In a positional system each place is worth the base times the place to its right. In decimal, 1234 means 1 × 1000 + 2 × 100 + 3 × 10 + 4 × 1, the powers of 10. In base b the places are 1, b, b², b³ and so on, and the digits run from 0 to b − 1. So 10 means b in every base: two in binary, eight in octal, sixteen in hexadecimal. Bases above 10 borrow letters for the extra digits: A is 10, F is 15 and Z is 35.

Binary, octal and hexadecimal

Binary uses only 0 and 1, the two states of a transistor, which is why computers count in it. Long rows of bits are hard to read, so programmers group them: three bits make one octal digit and four bits one hexadecimal digit. 0100 1101 0010 is 4D2 in hexadecimal and 1234 in decimal. One byte, eight bits, is always exactly two hex digits, from 00 to FF, which is why colours like #FF8800, memory addresses and character codes are written in hex.

Converting by hand

From decimal to base b, divide by b and write down the remainder, then divide the quotient again, until it reaches 0; the remainders read from the last to the first are the digits. 1234 ÷ 16 = 77 remainder 2, 77 ÷ 16 = 4 remainder 13 (D), 4 ÷ 16 = 0 remainder 4, giving 4D2. From base b to decimal, multiply each digit by its place value and add: 4 × 256 + 13 × 16 + 2 = 1234. Between binary and hexadecimal no arithmetic is needed: swap each group of four bits for its hex digit.

Negative numbers and two's complement

Processors store integers in a fixed number of bits, usually 8, 16, 32 or 64. Unsigned, n bits hold 0 to 2ⁿ − 1. For signed numbers almost every computer uses two's complement: the top bit is worth −2ⁿ⁻¹ instead of +2ⁿ⁻¹. In 8 bits that gives −128 to 127, and 11111111 is −1. To negate a number, invert all its bits and add 1: 22 is 00010110, inverted 11101001, plus 1 gives 11101010, which is −22. Addition then works the same for positive and negative numbers, which is why the scheme won.

Overflow, endianness and fractions

A result that does not fit in its width overflows and wraps around: in 8 bits, 127 + 1 becomes −128, the kind of bug that crashed the Ariane 5 rocket in 1996. When a value spans several bytes, big-endian machines store the most significant byte first and little-endian ones, like x86 and most ARM chips, the least significant first, so 1234 is stored as 04 D2 or D2 04. Fractions use negative powers: 0.101 in binary is 1/2 + 1/8 = 0.625. A fraction only ends in a base whose prime factors cover its denominator, so 0.1 in decimal repeats for ever in binary, which is why 0.1 + 0.2 is not exactly 0.3 in floating point.

Frequently asked questions

Why do programmers use hexadecimal instead of octal?

Because bytes have eight bits, and eight bits split evenly into two hex digits but not into octal digits of three bits. Octal was popular on older machines with 12-, 24- or 36-bit words, and it survives in Unix file permissions such as 755, where each digit is three permission bits.

What is the largest number in 32 or 64 bits?

Unsigned, 32 bits reach 4,294,967,295 (FFFFFFFF) and 64 bits 18,446,744,073,709,551,615. Signed, in two's complement, the largest values are 2,147,483,647 and 9,223,372,036,854,775,807, and the smallest −2,147,483,648 and −9,223,372,036,854,775,808.

Is there a base above 36?

Yes, any whole number from 2 up can be a base, but beyond 36 there are no more digits and letters to write it with, so other symbols are needed. Base 64 encoding, for example, uses A–Z, a–z, 0–9, + and /, and the Babylonians counted in base 60, which survives in 60 minutes and 360 degrees.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Open the Base Converter