Bits, bytes and units

Why computers use binary, and the units from bits to petabytes.

F8.1Data representationGCSE12 min

Do this lesson in the simulator

Everything a computer stores, every number, word, picture and sound, is stored as binary: long runs of 1s and 0s. This module is about how. It starts here, with why computers use binary at all, and the units used to measure how much data there is.

Why binary

A computer's processor and memory are made of billions of transistors, tiny switches that are either on or off. Two states are easy to make reliably, even when a signal is a little noisy: a voltage is clearly high or clearly low. So everything is stored with two symbols, 1 for on and 0 for off. One binary digit is a bit.

BugBot's LED shows it in miniature. Each of its three colours is on or off here, so three bits give eight colours:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

for red in [0, 1]:
    for green in [0, 1]:
        for blue in [0, 1]:
            print(red, green, blue)
            led(red * 255, green * 255, blue * 255)
            wait(0.4)
led("off")

Run this in the simulator

Every extra bit doubles the number of possible patterns: 1 bit gives 2, 2 bits give 4, 3 bits give 8, and n bits give 2 to the power n.

Units

Unit Size
bit a single 1 or 0
nibble 4 bits
byte 8 bits
kilobyte (kB) 1,000 bytes
megabyte (MB) 1,000 kilobytes
gigabyte (GB) 1,000 megabytes
terabyte (TB) 1,000 gigabytes
petabyte (PB) 1,000 terabytes

Because computers count in powers of 2, memory is often measured in units of 1,024 instead: a kibibyte (KiB) is 1,024 bytes, a mebibyte (MiB) 1,024 KiB, and so on. The boards differ on which they expect, so check your exam box below.

How big is a picture?

BugBot's camera sees 320 by 240 pixels, and each pixel uses a byte each for red, green and blue:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

width, height, bytes_per_pixel = 320, 240, 3
frame_bytes = width * height * bytes_per_pixel
print("one frame:", frame_bytes, "bytes")
print("        =", frame_bytes * 8, "bits")
print("        =", frame_bytes / 1000, "kB")
print("        =", frame_bytes / 1000 / 1000, "MB")
print("a minute of video at 30 frames a second:", round(frame_bytes * 30 * 60 / 1000 / 1000 / 1000, 2), "GB")

Run this in the simulator

Converting between units is multiplying or dividing: bytes to bits, multiply by 8; bytes to kilobytes, divide by 1,000; and so on down the table. A minute of uncompressed video from a small robot camera is already over 12 GB, which is why F8.9 is about compression.

Task: frame sizes

The robot's camera can also capture a small 64 by 48 frame with 3 bytes per pixel. Work out and print three lines: bits: <n>, bytes: <n> and kB: <n> (kilobytes of 1,000 bytes, as a decimal). Store the width, height and bytes per pixel in variables and calculate each line; do not type the answers.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

width = 64

Challenges

  1. How many different colours can a pixel with 24 bits have? Work it out with **.
  2. How many 1 MB photos fit on a 64 GB memory card? Use 1,000 for each step.
  3. Repeat the frame calculation using kibibytes and mebibytes. How big is the difference?