Skip to content

← Computing Further 6–8

What Is the Biggest Number a Computer Can Count To?

Topic 23 · Grades 6–8 · Fixed-size numbers

Binary (Grade 6) and overflow (Grade 7); the blocks from Grade 7, the Python parts from Grade 8 (loops and functions).

What this builds on from class
In this unit 8 sections
  1. Try it
  2. How it works
  3. Program it
  4. What your program shows
  5. Going further still
  6. Challenges
  7. Questions
  8. Where to go next

A computer can count the views of a video, the points in a game, the seconds since it was switched on. Can it keep counting forever? Every number a computer keeps sits in a fixed number of bits, set when the program was written: often 8, 16, 32 or 64. A fixed number of bits has a biggest number. So what happens when a counter gets there and somebody adds one more?

Here is a counter with 8 bits, one byte. It is at 250. Press +1 and watch it climb.

Try it

A one-byte counter

Press +1 five times to reach 255, the biggest number a byte can hold. Then press it once more.

Binary
11111010
Number
250

250 = 128 + 64 + 32 + 16 + 8 + 2

Tap a bulb to switch it on (1) or off (0).

The left bit is off, so the number is 0 or more: the other bits add up as usual.

0 and then all 1s: 255, the biggest number 8 bits can show this way. Add 1 and it becomes 0.

Each bit that is on adds its place value to the number. A bit that is off adds nothing.

Adding 1: the bit worth 1 was off, so it simply came on.

Adding 1: the bits on the right that were on went off, and the next bit came on. That is a carry, like 9 + 1 = 10.

All 8 bits are on: 255, the biggest number 8 bits can show. What will one more press do?

255 + 1 = 256, which needs 9 bits. There is no bit for the carry, so it falls off the left end and the counter shows 0: it has rolled over, like a meter.

255 + 1 carried into the left bit, which is worth −128: the biggest number became the smallest.

0 − 1 needs a borrow from a bit that isn’t there, so every bit comes on: the counter rolls back to 255.

0 − 1 took the left bit away: the smallest number became the biggest, 255.

Taking 1 away: the bit worth 1 was on, so it simply went off.

Taking 1 away: the bits on the right that were off came on, and the next bit that was on went off. That is a borrow, like 10 − 1 = 9.

All 8 bits are on: 255, the biggest number 8 bits can show. To go higher you need one more bit.

Yes, that is one of the numbers to make. Now try another.

You made every number in the row.

A new pattern! Keep switching to find the others.

You have found that pattern already.

You found them all: 8 bits make 16 different patterns.

The counter shows 255, with all eight bits on. What do you think one more press will show?

Pick an answer to see why.

You need: binary numbers (Grade 6) and overflow (Grade 7). The blocks use variables (Grade 7); the Python parts need loops and functions, from Grade 8.

Try it

Use the counter above.

  • a.From 255, press +1. Then press +1 a few more times. What comes after 0?
  • b.Press Back to 0, then −1. What does 0 − 1 give in one byte?
  • c.From 0, how many presses of +1 bring the counter back to 0 again?

Now give the counter two bytes, 16 bits. It starts near the top.

Try it

A two-byte counter

Press +1 until it rolls over. Watch which bits change on the last press.

Binary
1111 1111 1111 1010
Number
65530

65530 = 32768 + 16384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 2

Tap a bit to switch it on (1) or off (0).

The left bit is off, so the number is 0 or more: the other bits add up as usual.

0 and then all 1s: 65535, the biggest number 16 bits can show this way. Add 1 and it becomes 0.

Each bit that is on adds its place value to the number. A bit that is off adds nothing.

Adding 1: the bit worth 1 was off, so it simply came on.

Adding 1: the bits on the right that were on went off, and the next bit came on. That is a carry, like 9 + 1 = 10.

All 16 bits are on: 65535, the biggest number 16 bits can show. What will one more press do?

65535 + 1 = 65536, which needs 17 bits. There is no bit for the carry, so it falls off the left end and the counter shows 0: it has rolled over, like a meter.

65535 + 1 carried into the left bit, which is worth −32768: the biggest number became the smallest.

0 − 1 needs a borrow from a bit that isn’t there, so every bit comes on: the counter rolls back to 65535.

0 − 1 took the left bit away: the smallest number became the biggest, 65535.

All 16 bits are on: 65535, the biggest number 16 bits can show. To go higher you need one more bit.

Yes, that is one of the numbers to make. Now try another.

You made every number in the row.

A new pattern! Keep switching to find the others.

You have found that pattern already.

You found them all: 16 bits make 16 different patterns.

Sixteen bits reach 65,535. Then the same thing happens: all sixteen bits are on, the next press carries a 1 off the left end, and the counter is back to 0. More bits only make the lap longer. Every counter with a fixed number of bits has a lap.

How it works

A meter with wheels

Three close-ups of a car's distance meter one above the other: the first reads 99999 with a 9 in the last, white wheel; in the second every wheel is half-turned between 9 and 0; the third reads 00000 with a 0 in the last wheel
Figure 23.1 A car’s distance meter rolling over, after 99,999.9 miles. It has only six wheels, so the next tenth of a mile brings back all zeros. Photo: Hellbus, Public domain

The distance meter on a car or a motorbike has a row of wheels with the digits 0 to 9 on each. When the right wheel goes from 9 to 0, it turns the next wheel on by one: a carry. A meter with five wheels can show 00000 to 99999, which is 100,000 readings. After 99999 every wheel turns from 9 to 0 at once, and the carry has no wheel to go to. The meter shows 00000.

A computer’s counter works the same way, with bits for wheels and only 0 and 1 on each. With n bits there are 2 × 2 × … (n twos) patterns, and the biggest number is one less than that, because one pattern, all 0s, is used for 0.

Bits Patterns Biggest number
8 256 255
16 65,536 65,535
32 4,294,967,296 4,294,967,295
64 18,446,744,073,709,551,616 18,446,744,073,709,551,615

Each extra bit doubles the number of patterns. So 32 bits are not twice as good as 16: they hold 65,536 times as many numbers.

When a number doesn’t fit, the extra bit falls off: overflow, as you met it in Grade 7 Grade 7: Overflow, where a byte said 200 + 100 = 44. A counter that overflows is said to roll over or wrap around, because it goes round to the start like the meter.

Rolling over is a remainder

You don’t need to work in binary to find what a byte will show. The bit that falls off is worth 256, so each lap takes away 256. What is left is the remainder when the true number is divided by 256.

When the left bit is a minus sign

Some numbers need to be negative: a temperature, a bank balance. A common way is to make the left bit worth minus its place value: in 8 bits, −128 instead of 128. Then 8 bits hold −128 to 127. (Grade 9 explains it fully as two’s complement.) Such a number is called signed. Watch what rolling over does to it.

Try it

A signed counter

Press +1 until you pass 127.

Binary
01111101
Number
125

125 = 64 + 32 + 16 + 8 + 4 + 1

Tap a bulb to switch it on (1) or off (0).

The left bit is off, so the number is 0 or more: the other bits add up as usual.

0 and then all 1s: 127, the biggest number 8 bits can show this way. Add 1 and it becomes −128.

Each bit that is on adds its place value to the number. A bit that is off adds nothing.

Adding 1: the bit worth 1 was off, so it simply came on.

Adding 1: the bits on the right that were on went off, and the next bit came on. That is a carry, like 9 + 1 = 10.

All 8 bits are on: 255, the biggest number 8 bits can show. What will one more press do?

255 + 1 = 256, which needs 9 bits. There is no bit for the carry, so it falls off the left end and the counter shows 0: it has rolled over, like a meter.

127 + 1 carried into the left bit, which is worth −128: the biggest number became the smallest.

0 − 1 needs a borrow from a bit that isn’t there, so every bit comes on: the counter rolls back to 255.

−128 − 1 took the left bit away: the smallest number became the biggest, 127.

Taking 1 away: the bit worth 1 was on, so it simply went off.

Taking 1 away: the bits on the right that were off came on, and the next bit that was on went off. That is a borrow, like 10 − 1 = 9.

All 8 bits are on: 255, the biggest number 8 bits can show. To go higher you need one more bit.

Yes, that is one of the numbers to make. Now try another.

You made every number in the row.

A new pattern! Keep switching to find the others.

You have found that pattern already.

You found them all: 8 bits make 16 different patterns.

A signed one-byte counter is at 127. What does one more press show?

Pick an answer to see why.

So a signed 32-bit counter goes no higher than 2,147,483,647, about 2.1 billion. That was the biggest number YouTube’s view counter could hold. In December 2014 the music video Gangnam Style by the South Korean singer Psy became the first video watched more times than that. By then YouTube’s counter had 64 bits: Google said its engineers had seen it coming a couple of months before and had updated their systems. More counters that ran out, or nearly did, are the stories of a later topic in this book.

Program it

With blocks

A game keeps its score in one byte. Each time you win points, it adds them and keeps only the remainder ÷ 256, just as the counter does.

Try it

A score in one byte

Press Run and type 100 three times. Watch the score in the trace table. Then press Test.

What the program says and asks appears here.

score – no value
points – no value
StepBlockscorepoints
TestShould sayIt said
100 × 3Score: 44
Up to 255Score: 255
One moreScore: 0
Small winsScore: 60

Press Run to start the program, or Step to do one block at a time.

Loading the blocks…

After 100 and 100 the score is 200, as it should be. The third 100 makes 300, and the byte keeps 44: you won more points and the score went down. The tests show the edge exactly: 200 + 50 + 5 = 255 is fine, and one point more gives 0.

In Python (from Grade 8)

Python’s own numbers never roll over (see below), so a program makes a counter of one byte by keeping the remainder itself: % is Python’s remainder, as in Grade 8.

counter = 250for press in range(8):    counter = (counter + 1) % 256    print(counter)

Output

251252253254255012

That is the widget’s counter, in three lines. Change 256 to 65536 and it is a two-byte counter.

The biggest number in 8, 16, 32 and 64 bits

2 ** 8 is 2 to the power 8, eight 2s multiplied together. In an f-string, :, after a number puts commas in it.

for bits in [8, 16, 32, 64]:    biggest = 2 ** bits - 1    signed_biggest = 2 ** (bits - 1) - 1    print(f'{bits} bits: up to {biggest:,}, or {signed_biggest:,} with a sign')

Output

8 bits: up to 255, or 127 with a sign16 bits: up to 65,535, or 32,767 with a sign32 bits: up to 4,294,967,295, or 2,147,483,647 with a sign64 bits: up to 18,446,744,073,709,551,615, or 9,223,372,036,854,775,807 with a sign

There is 2,147,483,647, the old view counter’s limit, and 9,223,372,036,854,775,807, its new one. (The BBC’s report of the change printed the new limit as …808, one too many: the biggest number is always one less than the number of patterns.)

How long does each one last?

Big numbers are hard to feel. So imagine a counter that goes up by one every second, like a clock, and ask how long it lasts before it rolls over. The function how_long turns seconds into minutes, hours or years, whichever reads best.

def how_long(seconds):    if seconds < 60 * 60:        return f'{seconds / 60:.0f} minutes'    elif seconds < 60 * 60 * 24 * 365:        return f'{seconds / (60 * 60):.0f} hours'    else:        return f'{seconds / (60 * 60 * 24 * 365):,.0f} years' for bits in [8, 16, 32, 64]:    biggest = 2 ** bits - 1    print(f'{bits} bits: {how_long(biggest)}')print(f'32 bits with a sign: {how_long(2 ** 31 - 1)}')

Output

8 bits: 4 minutes16 bits: 18 hours32 bits: 136 years64 bits: 584,942,417,355 years32 bits with a sign: 68 years

(:.0f rounds to no decimal places. A year here is 365 days.)

What your program shows

  • A byte lasts 4 minutes. Counting one a second, it rolls over before your tea is cool. Two bytes last 18 hours: not even a day.
  • 32 bits last 136 years, or 68 years with a sign. That sounds safe, and it is why programmers chose 32 bits for years. But a counter that goes up faster than once a second, or a clock started in 1970, gets there within a lifetime. (A later topic finds the exact day a signed 32-bit clock runs out.)
  • 64 bits last 585 billion years, more than 40 times the age of the universe. Thirty-two more doublings turned a lifetime into forever. It is the same number as the moves for the 64 disks of the Tower of Hanoi, 18,446,744,073,709,551,615, for the same reason: both double 64 times.

So the answer to the title is: it depends on how many bits the program gave the number, and nothing warns you when it runs out. A computer counting in 64 bits will never reach its end; one counting in 8 bits reaches it in four minutes. The skill is choosing enough bits, before anyone needs them.

A level nobody could finish

You can run that drawing loop yourself. This function does what the game’s routine does for the early levels, with the count kept in 8 bits:

def symbols_drawn(level):    counter = level - 1          # the game counts levels from 0    b = (counter + 1) % 256      # adds 1 back, in an 8-bit register    drawn = 0    while True:        drawn = drawn + 1        # draw one fruit        b = (b - 1) % 256        # take 1 away, in 8 bits        if b == 0:            return drawn for level in [1, 2, 5, 7, 256]:    print(f'Level {level}: {symbols_drawn(level)} drawn')

Output

Level 1: 1 drawnLevel 2: 2 drawnLevel 5: 5 drawnLevel 7: 7 drawnLevel 256: 256 drawn

Level 1 draws one fruit, level 7 draws seven, and level 256 draws 256. (From level 8 on, the routine goes to another part that draws exactly seven, the newest. At level 256 the register holds 0, which looks less than 8, so the early-level part runs.) Two roll-overs, one up and one down, and the game had an ending nobody planned.

Going further still

Python never rolls over. Try it:

print(2 ** 64)print(2 ** 64 + 1)print(len(str(2 ** 1000)))

Output

1844674407370955161618446744073709551617302

2⁶⁴ needs 65 bits, and Python simply uses more. 2¹⁰⁰⁰ has 302 digits, and Python keeps them all. A Python whole number uses as many bits as it needs, so its only limit is the computer’s memory. That is why the programs above had to make their counters roll over with % 256. Many other languages, and the processor itself, use fixed sizes, because a fixed size is fast and its space is known in advance.

Another game’s timer. In Donkey Kong (Nintendo, 1981), each level starts with a bonus timer whose hundreds are worked out as 10 × the level + 40, in one byte, and then made no more than 80. Write a program that prints the starting bonus (the hundreds × 100) for levels 1 to 24. Which level gives the player almost no time?

An open question for you. A counter that must never roll over could be made bigger whenever it gets near its end, as Python does. Why don’t all programs do that? Think of a game on a small 1980s machine with a few thousand bytes of memory, and of a phone counting billions of things at once.

Challenges

★ A whole lap (blocks)

Finish the program so that it says how many presses of +1 take a counter of the typed number of bits all the way round, back to 0. (For 8 bits it says 256.) Then press Test.

Try it

★ A whole lap

What the program says and asks appears here.

bits – no value
presses – no value
done – no value
StepBlockbitspressesdone
TestShould sayIt said
1 bit2
3 bits8
8 bits256
16 bits65536
20 bits1048576

Press Run to start the program, or Step to do one block at a time.

Loading the blocks…

Hint
Answer

★★ What does a signed counter show? (Python, from Grade 8)

Write signed(n, bits): the number a signed counter of bits bits shows after counting from 0 up to n. For 8 bits, 127 stays 127, 128 shows −128, and 300 shows 44.

def signed(n, bits):    # n counted on a signed counter of this many bits    return n print(signed(128, 8))

Try it

★★ What does a signed counter show?

  1. 128 in 8 bits signed(128, 8) Should give -128 Yours —
  2. 127 in 8 bits signed(127, 8) Should give 127 Yours —
  3. 300 in 8 bits signed(300, 8) Should give 44 Yours —
Tests passed
— / 10
Challenge
★★ solved

Write your signed in the program above (Edit), then check it.

A test your function gets wrong, anything it prints, and any error appear here.

Hint
Another hint
Answer

★★★ How many bits are enough? (Python, from Grade 8)

You are choosing the size of a counter. Write bits_needed(per_second, years): the fewest bits for a counter that starts at 0, goes up by per_second every second, and must not roll over for years years (of 365 days). A clock counting seconds for 100 years needs 32 bits.

def bits_needed(per_second, years):    return 0 print(bits_needed(1, 100))

Try it

★★★ How many bits are enough?

  1. Once a second for 100 years bits_needed(1, 100) Should give 32 Yours —
  2. Once a second for a year bits_needed(1, 1) Should give 25 Yours —
  3. Once a second for 10 years bits_needed(1, 10) Should give 29 Yours —
Tests passed
— / 8
Challenge
★★★ solved

Write your bits_needed in the program above (Edit), then check it.

A test your function gets wrong, anything it prints, and any error appear here.

Hint
Answer

Questions

What is the biggest number 16 bits can hold, without a sign?

Pick an answer to see why.

A one-byte counter shows 3. Which could be the true number of things it counted?

Pick an answer to see why.

a.

A meter on a water tank has four wheels of digits. It shows 9997, and 5 more liters flow through it. What does it show? Explain how a one-byte counter at 253 that counts 5 more is the same.

Answer
b.

Explain why a signed 8-bit counter at 127 shows −128 after one more count, not 0.

Answer
c.

Why did Pac-Man’s level 256 break, though the level number itself (255 inside the game) still fitted in a byte?

Answer
d.

A school builds a counter for the people who pass its gate. A programmer says 16 bits are plenty, because the school has only 900 students. Is she right? What would you choose, and why?

Answer

Where to go next