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.
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.
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
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.
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.
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
251252253254255012That 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 signThere 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 drawnLevel 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
18446744073709551616184467440737095516173022⁶⁴ 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.
★★ 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))★★★ 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))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 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
Explain why a signed 8-bit counter at 127 shows −128 after one more count, not 0.
Answer
Why did Pac-Man’s level 256 break, though the level number itself (255 inside the game) still fitted in a byte?
Answer
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
- Grade 6, Counting in binary: place values and carries, the counter’s wheels.
- Grade 7, Overflow: when an answer doesn’t fit, and the Ariane 5 rocket that was lost to it.
- Grade 9, Two’s complement: signed numbers, and how a computer subtracts by adding.
- How many moves to move 64 disks? The same 18,446,744,073,709,551,615, from a puzzle that doubles with every disk.
