AQA 7517, section 4.5.3.1
The bit and the byte
A bit (binary digit) is the fundamental unit of information. It is either 0 or 1. Every piece of data in a computer, including numbers, text, images and sound, is stored as a pattern of bits.
A byte is a group of 8 bits. A group of 4 bits is sometimes called a nibble.
How many values can bits represent?
Each extra bit doubles the number of different patterns. So bits can represent
For example, 3 bits can be arranged in different ways:
000, 001, 010, 011, 100, 101, 110, 111
| Number of bits | Different values | Example use |
|---|---|---|
| 1 | On/off, true/false | |
| 2 | Four options | |
| 3 | Eight options | |
| 4 | One hex digit | |
| 8 | One byte, such as one character in extended ASCII | |
| 16 | Short integers | |
| 32 | Common integer size |
Note that this is the number of patterns, which isn't the same as the largest number. With unsigned 8-bit numbers, the 256 patterns represent 0 to 255.
How many bits do I need?
To represent different values, you need the smallest such that .
Example: a game has 100 different item types. How many bits are needed for each item's ID?
- 6 bits gives , which is not enough.
- 7 bits gives , which is enough.
So 7 bits are needed.
import math
N = 100
print(math.ceil(math.log2(N))) # 7
Worked example
Task: a traffic light has 3 states (red, amber, green) and another has 5 states, such as the four colour combinations plus "off". How many bits does each need?
- 3 states: 1 bit gives only 2 values, but 2 bits give , so 2 bits are needed. (One pattern, such as
11, is unused.) - 5 states: 2 bits give 4, which is too few, and 3 bits give , so 3 bits are needed.
Exam tip: remember that bits give patterns. To find the bits needed, work upwards through powers of 2 until you reach or pass the number of values. Always round up, never down.
Check yourself
How many different values can be stored in 5 bits?
.
What is a byte?
A group of 8 bits.
What is the smallest number of bits needed to represent 20 different colours?
5 bits, because is too few and is enough.
What happens to the number of possible values when you add one more bit?
It doubles.