ViewWiki

Lesson 2 of 4

Counting in base 2.

About 30 minutes · 7 practice questions

How many values fit in so many bits, why each bit doubles them, the byte and 255, and converting between base 2 and base ten both ways.

Step 1 of 7 · How many values in how many bits

Step 1 of 7

How many values in how many bits

The screen shows a picture of a sunset, and in the computer it is only numbers. Each pixel stores three numbers, one for its red light, one for green and one for blue, and each of those is stored in Bits per channel bits. The number of bits decides how many different levels each channel can take, from off to full.

Each channel is now stored in 3 bits. Predict, and then Look inside will enlarge a patch of the picture, split each pixel into its three lights, and stand a ruler with a tick for every level beside the columns.

To the question

Step 2 of 7

Each bit doubles

An extra bit does not add two more values; it doubles them all, because every old pattern appears once with a 0 in the new bit and once with a 1. So the number of values in nn⁠ bits is a power of two:

21=2,22=4,23=8,24=16,…2^1 = 2,\quad 2^2 = 4,\quad 2^3 = 8,\quad 2^4 = 16,\quad \ldots⁠

That is exponential growth: it starts slowly and very soon becomes huge. Predict what happens when the bits double.

To the question

Step 3 of 7

By the number of levels

The ruler beside the columns now shows the number of levels at each depth. Change Bits per channel and watch the bands in the sky crowd together as the levels multiply, until to the eye they are gone.

To the question

Step 4 of 7

A byte

Eight bits are a byte. It is the size computers measure memory and files in, and in true colour each channel is one byte. A byte has 28=2562^8 = 256⁠ values. What is the largest of them?

To the question

Step 5 of 7

Reading a byte

To read a byte, write the eight place values over it, from left to right:

1286432168421128 \quad 64 \quad 32 \quad 16 \quad 8 \quad 4 \quad 2 \quad 1⁠

and add the ones with a 1 under them. To keep a long byte readable, it is usually written in two groups of four, like 0100 110120100\,1101_2⁠.

To the question

Step 6 of 7

Writing a byte

The other way, from base ten to base 2, starts from the largest place. Check whether 128128⁠ fits in the number: if it does, write 1 and take it away; if not, write 0. Move to 6464⁠ with what is left, and so on down to the 1s place. At the end you should be left with 00⁠.

To the question

Step 7 of 7

How many bits you need

Sometimes the question is the other way round: you have NN⁠ things, and each needs a number of its own. How many bits do you need? Look for the smallest power of 2 that reaches NN⁠. That is the logarithm in base 2, log⁡2N\log_2 N⁠, rounded up, and it is why it is so useful in computer science: each time NN⁠ doubles, you need just one more bit.

To the question

Interactive scene

Loading the interactive version…

Each step sets up the Scene for you, and a control marked “fixed” stays put for that step.

To the experiment

Practice · Predict, then look

Each of a pixel’s three colour channels, red, green and blue, is stored here in 3 bits. How many different levels can one channel take?

To the experiment

Practice · Predict, then look

At four bits per channel there are 16 levels. How many will there be when the bits double to 8?

To the experiment

Practice · Reach the target

Change Bits per channel so that each channel has exactly 64 levels, and press Check.

To the experiment

Practice · Calculate

In true colour each channel is a byte, 8 bits. What is the largest value such a channel stores, the brightest red?

To the experiment

Practice · Calculate

A pixel’s red channel is stored as the byte 1100 100021100\,1000_2⁠. What is it in base ten?

To the experiment

Practice · Choose

Which byte stores the number 100100⁠?

To the experiment

Practice · Calculate

You want to give each of 1000 people a different number of their own. How many bits does each number need, at least?