Bits, bytes and units

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

F8.1Data representationGCSE12 min

Covers OCR J277 1.2.3 Units · AQA 8525 3.3.3 Units of information · Edexcel 1CP2 2.1.1 Binary represents data · Edexcel 1CP2 2.3.1 Units of storage, in binary multiples

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.

One bit gives 2 patterns, two bits give 4 and three bits give 8, which are the LED's eight colours1 bit2 patterns012 bits4 patterns000110113 bits8 patterns000001010011100101110111× 2× 2the LEDoffbluegreencyanredmagentayellowwhitegreen: the new bit, 0 or 1 in front of every old pattern
Each extra bit doubles the patterns, because every old pattern turns up twice: once after a 0 and once after a 1. Three bits, one each for red, green and blue, are the eight colours the program shows.

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
A bit is one cell, a nibble 4 bits, a byte 8 bits or 2 nibbles; from a byte up, each unit is 1,000 of the one belowbit1nibble: 4 bits1011byte: 8 bits = 2 nibbles10110110× 4× 2bytekB× 1,000MB× 1,000GB× 1,000TB× 1,000PB× 1,000each unit is 1,000 of the one to its left
The small units are counted in bits, and a byte of 8 bits is where the big units start. Each step up the ladder is 1,000 times bigger, so a petabyte is a byte with five lots of 1,000 on top.

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 0.41 GB (about 415 MB), and an hour is about 25 GB, which is why F8.9 is about compression.

The camera frame is 230,400 bytes: times 8 gives 1,843,200 bits; divided by 1,000 gives 230.4 kB, and again gives 0.2304 MB1,843,200bits230,400bytes× 8230.4kB÷ 1,0000.2304MB÷ 1,000one frame: 320 × 240 pixels × 3 bytesto a smaller unit: moreof them, so multiplyto a bigger unit: fewerof them, so divide
Start from the size in bytes and move one step at a time. Going to a smaller unit there are more of them, so multiply; going to a bigger unit there are fewer, so divide.

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?