Arithmetic and NumbersMathematics · Book 1
0 / 0

Shelves / Arithmetic and Numbers

AN·02

Place Value and Bases

Why ten symbols can write every number there is: what each digit's position means, why zero makes it work, how to count and convert in any base (binary, octal, hexadecimal, sixty), and why every number has exactly one way to be written.

30 topics · ~31 min read · 15-question quiz
Updated Suggest a correction

Start here

What this chapter answers

How can ten symbols write numbers of any size? Why does the 3 in 300 mean three hundred but the 3 in 30 mean thirty? What is zero for, if it means nothing? What is binary, why do computers use it, and why do programmers write hexadecimal? How do you turn a number into base 2 or base 16 and back, and why do the methods work? And could anything else have been chosen? (Yes, and parts of it still are: there are sixty minutes in an hour.)

Place value in one picture

Imagine counting a big pile of beads. Counting one at a time is slow and easy to lose track of. So you group: every time you have ten loose beads, you tie them into a bundle. Every time you have ten bundles, you put them in a box. Ten boxes go in a crate. When you're done, you have, say, 3 crates, 4 boxes, 0 bundles and 7 loose beads.

Now write down only the four counts, in a fixed order, biggest group first: 3 4 0 7. That is 3,407. Each position stands for a group size, ten times the one to its right, and the digit says how many of that group you have. The 0 says "no bundles", and it matters: leave it out and you've written 347, a completely different pile.

That's the whole idea. The rest of this chapter makes it exact, shows that ten isn't special, and puts it to work.

How positional notation works

Digits, positions and values

In base ten, each position is worth ten times the position to its right. Counting from the right, starting at 0, position k is worth 10k: ones (100=1), tens (101), hundreds (102), thousands (103), and so on. A digit's value is the digit times its position's worth:

3407=3×103+4×102+0×101+7×100=3000+400+0+7.

Writing a number out like this is called expanded form. It's worth being careful with words: the 4 in 3,407 has face value 4 (the digit itself) and place value 400 (what it contributes).

The two rules hidden in that definition are what make the system work. First, a digit is always smaller than the base: in base ten there's no digit for ten, because ten loose beads must be bundled. Second, zero holds an empty place: without a digit for "none of this size", the positions would slide.

Why zero is needed

The Babylonians wrote numbers in base sixty for well over a thousand years without a sign for an empty place. They left a gap, or nothing, and readers decided from context whether a numeral meant 1, 60 or 3,600. A separate placeholder sign appears in late Babylonian texts, but mostly between digits rather than at the end of a number, so the overall size of a number still had to be inferred. Chinese rod numerals had the same problem, and left a blank space.

Zero as a digit solves this completely: 37, 307, 370 and 3,007 are four different strings. And zero as a number, with rules (a+0=a, a×0=0), is what lets you calculate with those strings without thinking about which positions were empty. The column methods of chapters 3 and 4 depend on it.

Reading large numbers: grouping is a convention

Long numerals are hard to read, so we split them into groups. The international convention groups by threes: 12,345,678 (twelve million, three hundred and forty-five thousand…). The Indian convention groups the first three digits from the right, then by twos: 1,23,45,678 (one crore, twenty-three lakh, forty-five thousand…), with a lakh being 1,00,000 (105) and a crore 1,00,00,000 (107). Both describe the same base-ten number; only the commas and the names differ. Some countries use a full stop or a thin space instead of a comma, and a comma where others write a decimal point, so "1.234" means one thousand two hundred and thirty-four in Germany and just over one in India.

Counting is an odometer

Counting in place value works exactly like a car's odometer: the rightmost digit goes up by one each time; when it would go past the largest digit (9 in base ten), it rolls back to 0 and the digit to its left goes up by one. That roll-over is a carry, and it's the bundling step: ten loose beads become one bundle. Two carries in a row (199 → 200) is ten bundles becoming a box.

This makes some counts easy to see. The two-digit numbers in base ten run from 10 to 99: the first digit has 9 choices (1 to 9, since a leading 0 would make it a one-digit number) and the second has 10, so there are 9×10=90 of them. In general:

The same idea tells you how long a number's numeral is. A number n≥1 has k digits in base b exactly when bk−1≤n<bk; taking logarithms (chapter 10), that's k =⌊logbn⌋+1, which is the curve in chapter 1's lab. Each extra digit multiplies the range by b: that's why place value keeps numerals short.

Other bases

Ten is a choice

Nothing in the definition needs ten. We almost certainly use it because we have ten fingers; the word digit comes from the Latin for finger. Other choices have left traces everywhere:

  • Sixty, from Babylon: 60 seconds in a minute, 60 minutes in an hour or a degree. Sixty has many divisors (1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60), so a third, a quarter or a fifth of an hour is a whole number of minutes.
  • Twenty: the Maya wrote numbers in a mostly base-twenty place system with a sign for zero. French still says quatre-vingts (four twenties) for 80, and old English counted in scores.
  • Twelve: dozens and grosses (122=144), twelve inches in a foot, and twelve months. Twelve divides by 2, 3, 4 and 6, which ten doesn't.
  • Two, eight and sixteen: the bases of computing, below.

A base is just a choice of bundle size. Everything in the first half of this chapter holds for any b≥2, and you can check your understanding by redoing it in a base you didn't grow up with.

Binary: base two

Base two has two digits, 0 and 1, and each position is worth twice the one to its right: 1, 2, 4, 8, 16, 32, …. So

1011012=1×32+0×16+1×8+1×4+0×2+1×1=45.

Counting in binary is the odometer with only 0 and 1: 0, 1, 10, 11, 100, 101, 110, 111, 1000, …. Every count is a carry or a chain of them.

Computers use binary because it's the easiest base to build reliably. A circuit has to tell digits apart by voltage, and two widely separated levels (low and high) can be distinguished despite electrical noise far more easily than ten closely spaced ones. Binary numerals are long, about 3.32 times as many digits as decimal (because 23.32≈10), but long numerals cost a circuit almost nothing. A single binary digit is a bit; eight bits make a byte, which can hold 28=256 different values, 0 to 255.

Octal and hexadecimal: binary made readable

Long strings of 0s and 1s are hard for people to read and copy. Because 8=23 and 16=24, bases 8 and 16 make perfect shorthand: each octal digit stands for exactly three bits, and each hexadecimal digit for exactly four.

Hexadecimal (base sixteen, "hex") needs sixteen digits, so it borrows letters: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. To convert binary to hex, split the bits into groups of four from the right and replace each group:

101101102=B616,since 10112=11=B and 01102=6.

And B616 =11×16+6 =182. Going back is the same table in reverse. No arithmetic is needed in either direction, which is the whole point. You'll meet hex in colour codes (#FF8800: red 255, green 136, blue 0), memory addresses, network hardware addresses and character codes. Octal survives mainly in Unix file permissions, where each octal digit is exactly three yes-or-no switches.

Base sixty, still in use

Clock time is a two-level base-sixty system with a base-24 position on top. 100,000 seconds is

100000=1×(24×3600)+3×3600+46×60+40,

or 1 day 3 h 46 min 40 s. Converting seconds to hours, minutes and seconds is exactly the repeated division of the next section, with mixed bundle sizes. Angles work the same way: 12°30′ is twelve and a half degrees.

Converting between bases

From any base to decimal

Method 1, expand. Write out each digit times its position's worth and add, as with 1011012=45 above.

Method 2, read left to right. Start with 0. For each digit, multiply what you have by the base, then add the digit. For 7EA16 (with E = 14 and A = 10):

0×16+7=7,7×16+14=126,126×16+10=2026.

Method 2 is called Horner's method. It gives the same answer as expanding because it's the expansion with the common factors pulled out:

7×162+14×16+10=(7×16+14)×16+10.

It needs no powers, which makes it the method of choice for mental arithmetic and for programs.

From decimal to any base: repeated division

To write n in base b, divide by b: the remainder is the last digit. Then divide the quotient by b: its remainder is the next digit to the left. Continue until the quotient is 0. Writing 45 in binary:

Interactive diagram: Repeated division: writing 45 in binary. It needs JavaScript; the text around it says the same.
Division Quotient Remainder
45 ÷ 2 22 1
22 ÷ 2 11 0
11 ÷ 2 5 1
5 ÷ 2 2 1
2 ÷ 2 1 0
1 ÷ 2 0 1

Reading the remainders from the bottom up gives 1011012.

To convert between two bases neither of which is ten, either go through decimal (expand, then divide), or, when both are powers of the same base like 2, 8 and 16, regroup the bits.

Every number has exactly one numeral

Two different strings of digits could, in principle, name the same number: Roman numerals allow IIII and IV for four. Place value doesn't, once you ban leading zeros.

Uniqueness is why comparison is easy in place value: of two numbers with no leading zeros, the one with more digits is larger; with the same number of digits, compare from the left, and the first difference decides. Try that with Roman numerals (is XC bigger than LXX?).

The base shows up in the arithmetic

Some familiar facts are facts about base ten, not about numbers. A number is divisible by 10 exactly when its last digit is 0, because every other position is already a multiple of 10. In base 2, the same reasoning says a number is even exactly when its last bit is 0. The test "a number is divisible by 9 when its digit sum is" works because 10=9+1, so every power of ten is one more than a multiple of 9; chapter 5 proves it and shows the base-b version (divisibility by b−1). Multiplying by 10 appends a zero only because ten is the base: in binary, appending a zero doubles.

Lab: bases in code

Experiment 1: write your own converter, and check it

The two methods above, as code: repeated division to write a number in base b, and Horner's method to read it back. The last lines check both against Python's own int(text, base) for every number below 20,000 in every base from 2 to 36 (36 because there are ten digits and 26 letters).

Python
DIGITS = "0123456789abcdefghijklmnopqrstuvwxyz"

def to_base(n, b):
    """Write n in base b by repeated division: the remainders are the digits, last digit first."""
    if n == 0:
        return "0"
    out = ""
    while n > 0:
        n, r = divmod(n, b)
        out = DIGITS[r] + out
    return out

def from_base(s, b):
    """Read s in base b left to right: multiply what you have by b, then add the next digit."""
    n = 0
    for ch in s:
        n = n * b + DIGITS.index(ch)
    return n

for b in (2, 8, 12, 16):
    print(f"2026 in base {b:2}: {to_base(2026, b)}")
print(from_base("11111101010", 2), from_base("7ea", 16))
# check both against Python's own int(text, base) for every base from 2 to 36
wrong = sum(1 for b in range(2, 37) for n in range(20000)
            if int(to_base(n, b), b) != n or from_base(to_base(n, b), b) != n)
print("pairs checked:", 35 * 20000, " wrong:", wrong)
Text
2026 in base  2: 11111101010
2026 in base  8: 3752
2026 in base 12: 120a
2026 in base 16: 7ea
2026 2026
pairs checked: 700000  wrong: 0

Seven hundred thousand round trips and no disagreements: the proof in the last section, checked by brute force. Notice the octal and binary agree digit by digit in groups of three: 0111111010102 is 37528.

Experiment 2: how many digits?

Python
import math
for n in (9, 10, 99, 100, 999_999, 1_000_000, 2**64 - 1):
    print(f"{n:>20}  decimal {len(str(n)):2} digits  binary {n.bit_length():2} digits  hex {len(format(n, 'x')):2} digits")
print("binary digits per decimal digit:", round(math.log2(10), 4))
Text
                   9  decimal  1 digits  binary  4 digits  hex  1 digits
                  10  decimal  2 digits  binary  4 digits  hex  1 digits
                  99  decimal  2 digits  binary  7 digits  hex  2 digits
                 100  decimal  3 digits  binary  7 digits  hex  2 digits
              999999  decimal  6 digits  binary 20 digits  hex  5 digits
             1000000  decimal  7 digits  binary 20 digits  hex  5 digits
18446744073709551615  decimal 20 digits  binary 64 digits  hex 16 digits

The decimal length jumps exactly at powers of ten (99 → 100) and the binary length at powers of two, which aren't the same places. The largest 64-bit number, 264−1, takes 64 bits, exactly 16 hex digits (four bits each), and 20 decimal digits. The ratio between binary and decimal length settles near log210≈3.32. The plot shows how many digits the number 10k needs as the base b runs from 2 to 36; slide k to choose the number. With k=10 (ten billion), binary needs 34 digits and base 36 needs 7.

236040
Digits needed to write 10^k in base b (k = 10)

Bigger bases give shorter numerals, but with sharply diminishing returns: going from base 2 to base 16 divides the length by 4, while going from 16 to 36 saves much less. The price of a bigger base is more digit symbols to learn and a bigger multiplication table.

Experiment 3: JavaScript reads bases too, with a trap

JavaScript
console.log((255).toString(2), (255).toString(16), (255).toString(36));
console.log(parseInt("ff", 16), parseInt("101", 2), parseInt("z", 36));
console.log(parseInt("12px"), Number("12px"));
console.log(parseInt(0.0000005), String(0.0000005), parseInt("0.0000005"));
console.log(parseInt("2", 2), parseInt("12", 2));
console.log(0x1F, 0o17, 0b1010);
Text
11111111 ff 73
255 5 35
12 NaN
5 5e-7 0
NaN 1
31 15 10

toString(base) writes and parseInt(text, base) reads, like the Python above. The trap is that parseInt reads as many characters as make sense and silently stops: "12px" gives 12, where the stricter Number gives NaN ("not a number"). That's useful for CSS sizes and dangerous everywhere else. parseInt(0.0000005) gives 5, because the number is first turned into the text "5e-7" and parsing stops at the "e". And parseInt("12", 2) gives 1: "2" isn't a binary digit, so it reads "1" and stops, instead of reporting an error. The last line shows how both languages let you write numbers in other bases directly: 0x for hex, 0o for octal and 0b for binary.

Experiment 4: bases in everyday computing

Python
import stat
for mode in (0o755, 0o644, 0o600):
    print(oct(mode), format(mode, "09b"), stat.filemode(stat.S_IFREG | mode))
colour = "#ff8800"
print(colour, "->", tuple(int(colour[i:i + 2], 16) for i in (1, 3, 5)))
seconds = 100_000
m, s = divmod(seconds, 60); h, m = divmod(m, 60); d, h = divmod(h, 24)
print(f"{seconds} s = {d} d {h} h {m} min {s} s")
Text
0o755 111101101 -rwxr-xr-x
0o644 110100100 -rw-r--r--
0o600 110000000 -rw-------
#ff8800 -> (255, 136, 0)
100000 s = 1 d 3 h 46 min 40 s

A Unix file permission is nine yes-or-no switches: read, write and execute for the owner, the group and everyone else. Written in binary, 111 101 101 lines up exactly with rwx r-x r-x, and in octal each group of three bits becomes one digit: 755. That's why the octal form survived. A colour code is three bytes in hex, each pair of hex digits being 0 to 255. And converting seconds to days, hours and minutes is repeated division with the bundle sizes 60, 60 and 24.

Worked examples

Reading place value

Converting by hand

Mixed bundles

Exercises

Practice

  1. Write in expanded form with powers of ten: (a) 6,083; (b) 40,700; (c) 1,000,001.
  2. What is the place value of the 7 in (a) 7,215; (b) 3,170; (c) 7,00,000?
  3. Write in the international and the Indian grouping: (a) 123456789; (b) 50000000.
  4. Count from 10112 to 100102 in binary.
  5. Convert to decimal: (a) 1101102; (b) 7778; (c) FF16; (d) 1203.
  6. Convert 2,000 to binary, then to hex by grouping, then to octal by grouping.
  7. How many three-digit numbers are there in base ten? How many four-bit numbers (counting 0000)?
Answers
  1. (a) 6×103+0×102+8×10+3; (b) 4×104+0×103+7×102+0×10+0; (c) 1×106+1, with zeros in the five positions between.
  2. (a) 7,000; (b) 70; (c) 7,00,000, seven lakh.
  3. (a) 123,456,789 and 12,34,56,789; (b) 50,000,000 and 5,00,00,000 (five crore).
  4. 1011, 1100, 1101, 1110, 1111, 10000, 10001, 10010 (that is, 11 to 18).
  5. (a) 32+16+4+2=54; (b) 7×64+7×8+7 =511 =83−1; (c) 15×16+15=255; (d) 1×9+2×3+0 =15.
  6. 2000=111110100002. Grouping by fours from the right: 11111010000=7D016. Grouping by threes: 11111010000=37208. Check: 7×256+13×16 =2000.
  7. 9×102=900 (100 to 999). Four bits allow 24=16 patterns, 0 to 15.

Harder

  1. Explain, using bundles, why multiplying a base-ten number by 10 appends a zero. What does appending a zero do in base 2? In base 16?
  2. A number written in base b is 121b, and it equals 49. Find b. Then show that 121b is a perfect square in every base b≥3.
  3. Two programs exchange the text "1.250". One reads it as one and a quarter, the other as one thousand two hundred and fifty. Explain how both happen, and suggest a format that avoids it.
  4. Without converting to decimal, decide which is larger: 37208 or 7CF16. Explain your method.
  5. Prove that a number written in binary is divisible by 4 exactly when its last two bits are 00.
Answers
  1. Multiplying by 10 turns every loose bead into a bundle, every bundle into a box, and so on: each digit moves one position left, and the ones position is left empty, hence a 0. In base 2, appending a zero multiplies by 2; in base 16, by 16. "Appending a zero multiplies by the base" is the general rule.
  2. b2+2b+1=49, so (b+1)2=49 and b=6. In general 121b =b2+2b+1 =(b+1)2, a perfect square whenever the digit 2 is allowed, that is, b≥3.
  3. The first uses "." as the decimal point; the second uses "." to group thousands (common in Germany and much of Europe). A format with one fixed meaning, such as always using "." as the decimal point and no grouping at all (as JSON and most programming languages do), or an explicit locale, removes the ambiguity.
  4. Convert both to binary by expanding each digit: 37208 =0111110100002 and 7CF16 =0111110011112. Lining up the bits, both are 11 bits long after dropping leading zeros; compare from the left: 11111010000 against 11111001111. They first differ at the seventh bit from the left, where the first has 1 and the second 0, so 37208 (2000) is larger than 7CF16 (1999).
  5. Write n =(higher bits)×4+2d1+d0, since every position from the third onward is worth a multiple of 4. So n is divisible by 4 exactly when 2d1+d0 is, and 2d1+d0 is between 0 and 3, so it must be 0: both bits are 0.

Challenge

  1. Balanced ternary uses base 3 with digits −1, 0 and 1 (often written T, 0, 1). Write 5, 8 and −5 in it. Why does every whole number, positive or negative, have a balanced-ternary numeral without a separate minus sign? Which part of the uniqueness proof still works, and what changes?
Discussion

5 = 9 − 3 − 1 = 1TT; 8 = 9 − 1 = 10T; −5 is 5 with every digit negated: T11. Repeated division still works if each remainder is chosen from −1, 0, 1 instead of 0, 1, 2 (a remainder of 2 becomes −1, with one added to the quotient), and the quotient shrinks towards 0 whether you start positive or negative; that's why no minus sign is needed. Uniqueness survives because among −1, 0, 1 there's exactly one number with each remainder on division by 3, which is the property the proof used. The experimental Soviet computer Setun (1958) used balanced ternary; Knuth's The Art of Computer Programming, volume 2, calls it perhaps the prettiest number system of all.

In real life

Misconceptions and limits

The model to keep

Common misconceptions

Misconception What's actually true Where it's shown
"Zero means nothing, so leaving it out doesn't matter." Zero holds an empty position; without it 307 becomes 37. Place value doesn't work without a placeholder. Why zero is needed
"Base ten is the natural or correct base." Ten is a choice, almost certainly from counting on fingers. Base sixty, twenty, twelve, two and sixteen are all in use. Ten is a choice
"Binary numbers are a different kind of number." Binary is a different numeral for the same numbers: 1011012 and 45 are the same quantity. Binary: base two
"The digit 10 in base 16 is written 10." In base 16, "10" means sixteen. Ten is the single digit A; base b needs b single-symbol digits. Octal and hexadecimal: binary made readable
"Repeated division gives the digits from left to right." The first remainder is the last digit; read the remainders from the bottom up. From decimal to any base: repeated division
"Multiplying by 10 always means adding a zero." Appending a zero multiplies by the base: by 10 in decimal, by 2 in binary. And for decimals (chapter 7) it doesn't work at all: 2.5 × 10 isn't 2.50. The base shows up in the arithmetic
"Indian grouping and international grouping give different numbers." 1,23,456 and 123,456 are the same number, grouped two ways. Separators are reading aids, not digits. Reading large numbers: grouping is a convention
"parseInt converts text to a number or reports an error." It reads as far as it can and stops silently: "12px" gives 12 and parseInt("12", 2) gives 1. Experiment 3: JavaScript reads bases too, with a trap

Limits and trade-offs

  • Every base has a cost. A larger base gives shorter numerals but needs more symbols and a larger times table (base 60 needs 3,600 single-digit products); a smaller base is simple to build but gives long numerals. Ten sits in a comfortable middle for people; two is ideal for circuits.
  • Some fractions never end. A fraction has a finite expansion in base b only when its denominator divides a power of b. That's why 13 never ends in decimal and 110 never ends in binary (chapter 1's 0.1 + 0.2). Chapter 7 proves it.
  • Fixed-width storage caps the range. A computer stores a number in a fixed number of bits, so k bits hold only 2k values; going past the largest one wraps around or fails, depending on the type. Chapter 9 shows how negative numbers fit into the same bits.

Revision sheet

Key ideas

  • Place value: each position is worth the base times the position to its right; a digit's value is digit × bposition.
  • Zero holds an empty place; without it positions slide and numerals become ambiguous.
  • Digits are smaller than the base: base b uses 0 to b−1; hex uses A–F for 10–15.
  • Counting is an odometer: a digit rolls over to 0 and carries one to the left.
  • k-digit numbers in base b run from bk−1 to bk−1; there are (b−1)bk−1 of them; n has ⌊logbn⌋+1 digits.
  • To decimal: expand, or read left to right with "× base, + digit" (Horner).
  • From decimal: repeated division; remainders are the digits, last first.
  • Binary ↔ octal/hex: group bits in threes or fours from the right.
  • Every number has exactly one numeral in base b (no leading zeros).
  • Grouping (millions or lakhs and crores) and separators are conventions, not part of the number.

Glossary

  • Place value: the value a digit contributes because of its position: the digit times the position's power of the base.
  • Face value: the value of a digit on its own, regardless of position.
  • Base (radix): the number of distinct digits in a positional system, and the factor between neighbouring positions.
  • Positional notation: a way of writing numbers in which a symbol's value depends on its position.
  • Expanded form: a number written as the sum of each digit times its place value.
  • Placeholder: a symbol, our zero, that marks an empty position so the other digits keep their places.
  • Carry: moving one unit to the next position left when a position reaches the base.
  • Binary: base two, with digits 0 and 1.
  • Bit: a binary digit; the smallest unit of information a computer stores.
  • Byte: eight bits, able to hold 256 different values.
  • Octal: base eight; each octal digit corresponds to exactly three bits.
  • Hexadecimal: base sixteen, with digits 0–9 and A–F; each hex digit corresponds to exactly four bits.
  • Sexagesimal: base sixty, used by the Babylonians and still in time and angle measurement.
  • Horner's method: evaluating a numeral (or polynomial) left to right by repeatedly multiplying by the base and adding the next digit.
  • Lakh and crore: Indian names for 105 (1,00,000) and 107 (1,00,00,000).