TactivergeLite

Lite / Discovering How the World Works / 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 · Reviewed by the founders' mentors · Full interactive version

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.

Predict

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

  1. 10000
  2. 0000
  3. 1111 (it stays stuck)
Guess first, then reveal

0000. 1111 + 1 = 10000 needs five bits, but there are only four, so the carry falls off the top and the counter wraps round to 0. That's called overflow.

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?

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: 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.

“Binary Numbers and Base Systems as Fast as Possible” by Techquickie (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

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

Intuitive

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

Technical

Unsigned n-bit arithmetic is arithmetic modulo 2ⁿ: the result is kept as (a + b) mod 2ⁿ, and the carry out of the top bit signals overflow. Signed counters lose one bit to the sign, so 32-bit signed tops out at 2³¹ − 1 = 2,147,483,647.

Mathematical

Max(n) = 2ⁿ − 1: Max(8) = 255, Max(10) = 1,023, Max(16) = 65,535. Bits needed for N: the smallest n with 2ⁿ − 1 ≥ N. For N = 1,000: 2¹⁰ − 1 = 1,023, so 10 bits.

Applied

In the Virtual Computer Build, the 4-bit processor uses overflow on purpose: adding 1111 to a number is the same as subtracting 1, because the carry falls off the top.

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

bits

Practice 4. Why is overflow dangerous in a machine?

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.

Reflect

Overflow is silent. What could an engineer add to a machine so it notices when a counter is about to overflow?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Converting: binary to decimal and backAND, OR, NOT: the three little deciders →