---
title: "Bits and Bytes: Counting in Binary"
book: "Primary & Middle School Computer Science"
subject: computer-science
language: en
chapter: 11
exercises: 11
source: https://one-course.com/books/computer-science/1/en/chapter/11-bits-and-bytes-counting-in-binary
license: CC-BY-NC-SA-4.0
credit: "One Computer Science Book, One Course (one-course.com)"
---

# Chapter 11 — Bits and Bytes: Counting in Binary

It is getting dark, and Sami and Noor stand at the two ends of the playground, each with a torch. With one torch, Sami can send only two messages: light on means “come here”, light off means “stay there”. So he takes a second torch. Now there are four ways to hold them: both off, only the left one on, only the right one on, both on. Four messages! With a third torch they would have eight. A [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) does exactly the same thing, with billions of tiny switches.

![Signalling across the playground: each torch is on or off.](https://one-course.com/images/onecourse/chapters/computer-science-1/g5-bits-and-bytes/img-deb544cb11fa.jpg)

*Signalling across the playground: each torch is on or off.*

**You already know.**

Sizes in a [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) are counted in [bytes](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte); one [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) holds one letter of plain text. The [storage](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-storage) keeps information without electricity ([Chapter 8](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#ch-g4-memory-and-storage)).

## 11.1 Two states are enough

Inside a [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer), every tiny part that holds information can be in one of two states only. A switch is on or off. A tiny area of a [hard disk](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-hard-disk) is magnetised one way or the other. A cell of [flash memory](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-flash) holds an electric charge or does not. Two states are easy to make, easy to tell apart, and hard to mix up.

**Definition 11.1 (Bit).**

A *bit* is the smallest piece of information: one choice between two states. We write the two states 0 and 1.

![The same eight bits, 10110010, kept in two ways. On a hard disk, each tiny area is magnetised to the right or to the left; in flash memory, each cell holds a charge (the dots) or not. The drawing is much simplified.](https://one-course.com/images/onecourse/chapters/computer-science-1/g5-bits-and-bytes/fig-4a2895903525.svg)

*The same eight [bits](#def-g5-bits-and-bytes-bit), 10110010, kept in two ways. On a [hard disk](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-hard-disk), each tiny area is magnetised to the right or to the left; in [flash memory](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-flash), each cell holds a charge (the dots) or not. The drawing is much simplified.*

## 11.2 Counting with two digits

How can numbers be written with only 0 and 1? Take five cards. The card on the right has 1 dot, the next one 2 dots, then 4, then 8, then 16: each card has twice as many dots as the card on its right. Each card is either face up, and its dots count, or face down, and it counts nothing.

![The number 13 with five cards: 8, 4 and 1 face up, 16 and 2 face down (the blue backs). Reading the cards from left to right, face up as 1 and face down as 0, gives 01101.](https://one-course.com/images/onecourse/chapters/computer-science-1/g5-bits-and-bytes/fig-162e110c9f39.svg)

*The number 13 with five cards: 8, 4 and 1 face up, 16 and 2 face down (the blue backs). Reading the cards from left to right, face up as 1 and face down as 0, gives 01101.*

With the five cards, any number from 0 (all face down) to 31 (all face up, $16 + 8 + 4 + 2 + 1$) can be shown, each in exactly one way.

**Definition 11.2 (Binary).**

Writing a number in *binary* is writing it with the digits 0 and 1 only, where each place is worth twice the place on its right: 1, 2, 4, 8, 16, and so on, from the right. A number written this way is a *binary number*.

**Method 11.3 (Writing a number in binary).**

Start with the largest card that is not more than the number: turn it face up and take its value away from the number. Go on with the next cards to the right: turn a card face up if its value is not more than what is left, and take it away. When nothing is left, read the cards: 1 for face up, 0 for face down.

**Example 11.4 (Writing 22 and reading 10011).**

For 22: 16 fits, 6 is left; 8 does not fit; 4 fits, 2 is left; 2 fits, 0 is left; 1 does not fit. So 22 is 10110 in [binary](#def-g5-bits-and-bytes-binary). To read 10011, add the values of the places that hold a 1: $16 + 2 + 1 = 19$.

Counting in [binary](#def-g5-bits-and-bytes-binary) is like counting with ordinary numbers, but each place is full after 1 instead of after 9: 0, 1, 10, 11, 100, 101, 110, 111, 1000…

## 11.3 A byte is eight bits

The [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) of the chapter on [memory](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-memory) and [storage](https://one-course.com/books/computer-science/1/en/chapter/6-inside-the-computer#def-g3-inside-the-computer-storage) is made of [bits](#def-g5-bits-and-bytes-bit): a [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) is eight [bits](#def-g5-bits-and-bytes-bit). With eight cards, worth 128, 64, 32, 16, 8, 4, 2 and 1, any number from 0 to 255 can be shown: $128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255$. So one [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) can hold any whole number from 0 to 255.

![A byte as eight lamps: on for 1, off for 0. Above each lamp, the value of its place. The byte 00101101 is the number 45.](https://one-course.com/images/onecourse/chapters/computer-science-1/g5-bits-and-bytes/fig-05ef93d3e567.svg)

*A [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) as eight lamps: on for 1, off for 0. Above each lamp, the value of its place. The [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) 00101101 is the number 45.*

## 11.4 Each extra bit doubles the patterns

How many different patterns can a few [bits](#def-g5-bits-and-bytes-bit) make? One [bit](#def-g5-bits-and-bytes-bit) has two patterns: 0 and 1. Two [bits](#def-g5-bits-and-bytes-bit) have four: 00, 01, 10, 11. For three [bits](#def-g5-bits-and-bytes-bit), take each of these four and add a 0 or a 1 at the end: that makes twice as many, eight patterns.

![1 bit: 2 patterns 2 bits: 4 patterns 3 bits: 8 patterns The tree of choices. Each new bit splits every pattern in two, so the number of patterns doubles.](https://one-course.com/images/onecourse/chapters/computer-science-1/g5-bits-and-bytes/fig-dac034649052.svg)

*1 [bit](#def-g5-bits-and-bytes-bit): 2 patterns 2 [bits](#def-g5-bits-and-bytes-bit): 4 patterns 3 [bits](#def-g5-bits-and-bytes-bit): 8 patterns The tree of choices. Each new [bit](#def-g5-bits-and-bytes-bit) splits every pattern in two, so the number of patterns doubles.*

**Proposition 11.5 (Doubling).**

Each extra [bit](#def-g5-bits-and-bytes-bit) doubles the number of different patterns: 2 patterns with one [bit](#def-g5-bits-and-bytes-bit), 4 with two, 8 with three, 16 with four, and so on. With eight [bits](#def-g5-bits-and-bytes-bit), one [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte), there are 256 patterns: the numbers 0 to 255.

**Proof.** Each pattern of $n$ [bits](#def-g5-bits-and-bytes-bit) gives two patterns of $n + 1$ [bits](#def-g5-bits-and-bytes-bit), one ending in 0 and one ending in 1. Every pattern of $n + 1$ [bits](#def-g5-bits-and-bytes-bit) comes from exactly one pattern of $n$ [bits](#def-g5-bits-and-bytes-bit): the one left when its last [bit](#def-g5-bits-and-bytes-bit) is taken away. So there are twice as many patterns of $n + 1$ [bits](#def-g5-bits-and-bytes-bit). ∎

Doubling grows very fast. Keep doubling from 2, eight times: 2, 4, 8, 16, 32, 64, 128, 256. Twenty more doublings would take us past 268 million.

## 11.5 Exercises

**Exercise 11.1 ★.**

Write 5, 9 and 20 in [binary](#def-g5-bits-and-bytes-binary) with five cards.

**Solution of Exercise 11.1.**

$5 = 4 + 1$: 00101. $9 = 8 + 1$: 01001. $20 = 16 + 4$: 10100.

**Exercise 11.2 ★.**

Which number is written 11010 in [binary](#def-g5-bits-and-bytes-binary)?

**Solution of Exercise 11.2.**

$16 + 8 + 2 = 26$.

**Exercise 11.3 ★.**

What is the largest number that can be shown with four cards? With five? With eight?

**Solution of Exercise 11.3.**

Four cards: $8 + 4 + 2 + 1 = 15$. Five cards: $16 + 15 = 31$. Eight cards: $128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255$.

**Exercise 11.4 ★.**

How many different patterns can 4 [bits](#def-g5-bits-and-bytes-bit) make? Find it by doubling.

**Solution of Exercise 11.4.**

1 [bit](#def-g5-bits-and-bytes-bit): 2; 2 [bits](#def-g5-bits-and-bytes-bit): 4; 3 [bits](#def-g5-bits-and-bytes-bit): 8; 4 [bits](#def-g5-bits-and-bytes-bit): 16 patterns.

**Exercise 11.5 ★.**

Count from 0 to 7 in [binary](#def-g5-bits-and-bytes-binary), using three digits each time.

**Solution of Exercise 11.5.**

000, 001, 010, 011, 100, 101, 110, 111.

**Exercise 11.6 ★.**

How many [bits](#def-g5-bits-and-bytes-bit) are needed to write 40 in [binary](#def-g5-bits-and-bytes-binary)? Try with the cards.

**Solution of Exercise 11.6.**

Five cards reach only 31, which is less than 40, so a sixth card, worth 32, is needed: $40 = 32 + 8$, written 101000. Six [bits](#def-g5-bits-and-bytes-bit).

**Exercise 11.7 ★.**

These eight lamps show a [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) (yellow is on). Which number is it?

![](https://one-course.com/images/onecourse/chapters/computer-science-1/g5-bits-and-bytes/fig-ab9137e9422e.svg)

**Solution of Exercise 11.7.**

The [byte](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-byte) is 10010011: $128 + 16 + 2 + 1 = 147$.

**Exercise 11.8 ★.**

Give three things, in a [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) or elsewhere, that have exactly two states.

**Solution of Exercise 11.8.**

For example: a light switch (on or off); a tiny area of a [hard disk](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-hard-disk) (magnetised one way or the other); a cell of [flash memory](https://one-course.com/books/computer-science/1/en/chapter/8-memory-and-storage#def-g4-memory-and-storage-flash) (charged or not); a door (open or shut); a coin on a table (heads or tails).

**Exercise 11.9 ★★.**

The cards show 0111. Add 1: which cards turn over? What is the new number in [binary](#def-g5-bits-and-bytes-binary), and in ordinary numbers?

**Solution of Exercise 11.9.**

$0111$ is $4 + 2 + 1 = 7$. Adding 1 gives 8: the cards 4, 2 and 1 turn face down and the card 8 turns face up. The new number is 1000 in [binary](#def-g5-bits-and-bytes-binary), 8 in ordinary numbers. (It is like $999 + 1 = 1000$: every full place goes back to 0 and carries 1 to the left.)

**Exercise 11.10 ★★.**

The teacher wants to give each of the 50 pupils of the school’s two classes a different code of 6 [bits](#def-g5-bits-and-bytes-bit). Are there enough codes?

**Solution of Exercise 11.10.**

6 [bits](#def-g5-bits-and-bytes-bit) make 2, 4, 8, 16, 32, 64 patterns: 64 codes, more than 50. Yes, there are enough (and 14 to spare).

**Exercise 11.11 ★★.**

Why must the value of each card be twice the value of the card on its right? What would go wrong with cards worth 1, 2, 3, 4 and 5?

**Solution of Exercise 11.11.**

Each card must be worth one more than all the cards on its right together: $1 + 2 + 4 = 7$, and the next card is worth 8. Then every number can be shown, and in one way only. With cards worth 1, 2, 3, 4 and 5, some numbers can be shown in several ways (3 is the card 3, or $1 + 2$), so a pattern of cards would not tell a number in a single way; and with all five cards face up, the largest number would be 15 instead of 31.
