Skip to content
Tactiverge

School/◇ Explorer/Week 15

Running out of bits: overflow

In 2014 a video got so many views that YouTube's view counter nearly ran out of room. How can a computer run out of numbers?

About 25 min · 3 goals · Checked by automated tests; not yet reviewed by an educator

1.Why should I care?

Machines store numbers in a fixed number of bits. Pick too few and a counter quietly wraps back to zero, which has caused real failures in games, rockets and clocks. Engineers must size their numbers on purpose.

  • ◇ Find the largest number that n bits can store.
  • ◇ Explain what overflow is and why the count wraps back to zero.
  • ◇ Choose enough bits for a machine's counter.

2.Predict

A 4-bit counter shows 1111. You add 1. What does it show?

Guess first. Being wrong here is how the surprise works.

3.Discover

Set the 4-bit register to all on (15) and press 'Add 1'. Read the message. Switch to 8 bits, press 'All on', note the number, and add 1 again. Can you predict the biggest number for 5, 6 and 7 bits?

Binary switches

Each switch is one bit. Its place value doubles from right to left: 1, 2, 4, 8… Add up the places that are ON to read the number.

Register size

Challenge: make 5

8
4
2
1

= 0

4 bits make 16 different patterns, from 0 up to 15.

0000 in binary = 0 = 0.

Can't use the interactive? Read a description instead

In the binary switches diagram, pressing 'All on' with 4 bits shows 1111 = 15. Pressing 'Add 1' then shows 0000 and the message explains that 16 needs 5 bits, so the carry falls off the top. With 8 bits, all on is 255, and adding 1 also wraps to 0.

4.Watch

→ Watch for why each extra digit multiplies how high you can count.

A quick, upbeat explanation of why counting in base 2 works the same way as counting in base 10, just with fewer digits.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

5.Understand

The biggest number

With every bit on, n bits hold 2ⁿ − 1. 4 bits: 1111 = 15. 8 bits: 11111111 = 255. The '− 1' is because one of the 2ⁿ patterns is used for 0.

Overflow: wrapping round

Add 1 to the biggest number and every bit carries, but the final carry has nowhere to go. The counter shows all zeros. It's like a car's old mileage counter rolling from 99999 to 00000. The machine doesn't complain: it just shows a wrong answer, which is what makes overflow dangerous.

Choosing enough bits

Engineers ask: what's the biggest value this number could ever reach? Then they pick enough bits, plus a safety margin. A step counter for a day might reach 50,000 steps: 16 bits (up to 65,535) is enough. A counter of all views on a video needs far more. In 2014 YouTube's view counter, a 32-bit number that tops out at 2,147,483,647, was upgraded to 64 bits as one video got close to that limit.

Explain it another way

A 4-bit counter is like a tiny clock face with only 16 hours. After hour 15 comes hour 0 again.

Worked example 1

A parking-garage counter must count up to 600 cars. How many bits does it need?

2⁹ − 1 = 511 is too small. 2¹⁰ − 1 = 1,023 is enough. So 10 bits.

Common mix-ups

  • A computer can store any number you like.
    ✓ Each stored number has a fixed number of bits and a maximum. Past it, the count overflows.

6.Practice

Practice, not a test. Wrong answers get a hint and another try.

Practice 1 · a fresh version every time

A counter in a machine has 7 bits. Starting from 0, what is the biggest number it can show before it overflows?

Practice 2

An 8-bit counter holds 255 and you add 3. What does it show? (It wraps round after 255.)

Practice 3

What is the smallest number of bits a counter needs to reach 1,000?

bits

Practice 4

Why is overflow dangerous in a machine?

7.Apply it

Design the counters for a smart bin that counts how many times it is opened per day and per year. Estimate the biggest values, choose the bits for each, and explain your safety margin.

Open BUILD 008: Create a Virtual Computer →

8.Reflect

Concepts: Binary, hexadecimal and digital signals · Fixed-width integers, bits and registers