Binary and denary
Inside a computer, everything is built from switches that are either off or on. So all data (numbers, text, images, sound and program instructions) is converted to binary, where each bit is 0 or 1, so that logic gates can process it and registers can store it.
Three number systems
Binary, denary and hexadecimal write the same value in different ways. Learn each one’s digits and place values: every conversion uses them.
Denary · base 10
Digits 0–9; each place is worth ×10.
1000 · 100 · 10 · 1
Binary · base 2
Digits 0 and 1; each place is worth ×2.
… 16 · 8 · 4 · 2 · 1
Hexadecimal · base 16
Digits 0–9, then A–F = 10–15; ×16.
4096 · 256 · 16 · 1
Convert between binary and denary
Both directions use the same row of place values. Write it above the bits first, then add or subtract.
Binary → denary
Add the place values that hold a 1.
Denary → binary
Subtract the largest place value that fits and write 1; otherwise write 0.
How many values can n bits hold?
Each extra bit doubles the number of patterns. Read across a row to find what a register of that size can hold.
| Register | Different values | Denary range | Largest in hex |
|---|---|---|---|
| 4 bits | 16 | 0 to 15 | F |
| 8 bits | 256 | 0 to 255 | FF |
| 12 bits | 4 096 | 0 to 4 095 | FFF |
| 16 bits | 65 536 | 0 to 65 535 | FFFF |
Why 2ⁿ − 1?
n bits make 2ⁿ different patterns. One of them is 0, so the largest value is 2ⁿ − 1.
12 bits: 2¹² = 4096 patterns, so the largest value is 4095 (hex FFF).
1 Convert 01001101 to denary.
2 Convert 90 to 8-bit binary.
3 What is the largest denary value that 6 bits can hold?
- 164 + 8 + 4 + 1 = 77
- 264 + 16 + 8 + 2, so
01011010 - 32⁶ − 1 = 63
Hexadecimal
A long row of 0s and 1s is hard for a person to read or copy without a slip. Hexadecimal (base 16) shortens it: one hex digit stands for exactly four bits. It uses sixteen digits, 0–9 and then A–F for 10–15.
Conversion strip: 0 to F
Find a hex digit in the top row, then read its four bits and its denary value underneath.
| Hex | Binary | Denary |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| Hex | Binary | Denary |
|---|---|---|
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| A | 1010 | 10 |
| B | 1011 | 11 |
| C | 1100 | 12 |
| D | 1101 | 13 |
| E | 1110 | 14 |
| F | 1111 | 15 |
Convert in groups of four bits
Four bits make exactly one hex digit, so binary and hex swap group by group. Denary needs place values of 16 instead.
Binary ↔ hexadecimal
Group bits in fours from the right. Keep four bits for each hexadecimal digit.
Hexadecimal ↔ denary
Multiply each digit by its place value. To go back, divide by 16 and keep the remainder.
Hexadecimal in use
Give two reasons people use it, and name where it is seen.
Why use hexadecimal?
- Shorter: one hex digit replaces four bits, so it takes less space on screen.
- Easier for people to read, write and remember, with fewer mistakes.
- Easier to spot errors and debug.
Where you will see it
- Memory addresses and memory dumps · error codes
- MAC addresses and IPv6 addresses
- HTML colour codes, e.g. #FF8000 · assembly code · URLs
1 Convert 1011 0110 to hexadecimal.
2 Convert 3F to binary.
3 Convert A5 to denary.
- 11011 is B and 0110 is 6, so
B6 - 2
0011 1111 - 310 × 16 + 5 = 165
Binary addition, overflow and shifts
Binary adds like denary, column by column, but you carry as soon as a column makes 2. A register has only so many bits, so an answer can be too big to fit. A logical shift moves every bit left or right.
Add binary from right to left
Start at the right-hand column and carry 1 whenever a column adds up to 2 or more. Watch what the second sum does to the 9th bit.
Rules
25 + 67 = 92
Result 01011100 = 64 + 16 + 8 + 4 = 92. It fits in 8 bits.
200 + 100: overflow
- An 8-bit register holds 0 to 255.
- 300 needs a 9th bit (256), which has nowhere to go: overflow error.
- The 8 bits left show 44.
An 8-bit register holds 0 to 255. 300 needs a 9th bit (256), which has nowhere to go: overflow error. The 8 bits left show 44.
Logical shifts
Every bit moves the same number of places. Bits shifted off the end are lost; 0s fill the empty places.
Logical shift left 1 place
The most significant bit (left-most) is lost; a 0 enters as the least significant bit.
Logical shift right 1 place
The least significant bit (right-most) is lost; a 0 enters as the most significant bit.
Shifting more than one place
Each place left doubles the value and each place right halves it, until a 1 falls off the end.
| Shift | Before | After | Effect |
|---|---|---|---|
| Left 2 | 00010110 (22) | 01011000 (88) | × 4 = 88 |
| Right 2 | 00010110 (22) | 00000101 (5) | ÷ 4 = 5.5 → 5 (remainder lost) |
| Left 3 | 00101100 (44) | 01100000 (96) | should be 352, but a 1 was lost: result 96 |
1 Add 00110101 and 00011011.
2 Add 10010110 and 01111000 in an 8-bit register. What goes wrong?
3 Shift 00010110 two places left. What is the result in denary?
- 1
01010000(53 + 27 = 80) - 2150 + 120 = 270 needs a 9th bit, so there is an overflow error; the 8 bits left are
00001110 - 3
01011000= 88, which is 22 × 4
Two’s complement and character sets
A bank balance can drop below zero and a message is made of letters, yet a register holds only 0s and 1s. Both problems are solved the same way: agree a code, so the same bits can stand for a negative number or a character.
Two’s complement: 8-bit signed integers
Binary has no minus sign, so negative numbers need a code of their own. In 8-bit two’s complement, the left-most bit is worth −128 instead of +128.
In 8-bit two’s complement the left-most bit is worth −128 instead of +128. Every other place value stays the same.
Denary → two’s complement (negative)
- Positive numbers are written as normal binary, always with a leading 0.
- 8 bits cover −128 (10000000) to +127 (01111111).
Two’s complement → denary
Method 1: the most significant bit is worth −128; add the place values holding a 1.
Method 2: if the MSB is 1, invert and add 1 to find the size: 00100010 + 1 = 00100011 = 35, so the value is −35.
Text: a character set gives each character a unique code
Text is stored as the code of each character, one after another in the same order: “fox” → 102, 111, 120. Within the lower-case alphabet, codes are consecutive: if f = 102, then h = 102 + 2 = 104.
ASCII and Unicode
Compare them on bits per character, the range of characters and storage space.
ASCII
- Uses 7-bit codes for 128 characters; extended ASCII uses 8-bit codes, up to 256 characters.
- Enough for English letters, digits, punctuation and control codes.
- Upper and lower case have different codes: A = 65, a = 97.
Unicode
- Can use more bits per character than ASCII.
- Represents a far greater range of characters: many languages, symbols and emojis.
- Characters needing more bits take more storage space.
1 Write −20 in 8-bit two’s complement.
2 What denary value is 11110110 in two’s complement?
3 The code for A is 65. What is the code for D?
- 1+20 is
00010100; invert and add 1:11101100 - 2−128 + 64 + 32 + 16 + 4 + 2 = −10
- 368
Images
Zoom far enough into a photo on a screen and it breaks up into tiny squares. Each square is a pixel, and the image is stored as a grid of them: the colour of each pixel is a binary number.
Four terms to define
Define each in one sentence, using the words in bold.
Pixel
The smallest component of an image. An image is broken down into pixels; each pixel’s colour is stored as a binary value.
Resolution
The number of pixels in the image: width × height.
Colour depth
The number of bits used for each pixel’s colour.
Metadata
Extra data stored with the image, e.g. width, height, colour depth, date taken.
Each row of the icon is stored as one binary number. Add one bit of colour depth and the number of colours doubles.
Change a setting
Say what happens to the image and to the file size.
Increase the resolution
More pixels → more detail and a sharper image, but a larger file.
Increase the colour depth
More bits per pixel → more colours, more accurate shades, and a larger file.
Image file size
Multiply the three settings to get bits, then divide down to the unit the question asks for.
Worked example
Bits, then bytes
The formula gives bits. Divide by 8 for bytes, then by 1024 for each larger unit (KiB, MiB, …).
1 An image is 64 × 32 pixels with a colour depth of 4 bits. Find its size in bits.
2 Give that size in KiB.
3 The colour depth is doubled to 8 bits. What happens to the file size?
- 164 × 32 × 4 = 8192 bits
- 28192 ÷ 8 = 1024 bytes = 1 KiB
- 3It doubles to 2 KiB: every pixel now needs twice as many bits
Sound
A sound wave changes smoothly, but a computer can only store numbers. So the wave is sampled: its amplitude is measured at regular time intervals, each sample is converted to a binary value, and the values are stored in sequence.
A sound wave changes smoothly, but a computer can only store a list of numbers. So the wave is sampled and stored in three steps.
- 1Its amplitude is measured at regular time intervals.
- 2Each sample is converted to a binary value.
- 3The values are stored in sequence.
Blue dots show stored samples of the same wave. Change one setting at a time to see its effect.
Two settings
Define each one and say what increasing it does.
| Setting | Meaning | Increase it and… |
|---|---|---|
| Sample rate | Number of samples taken per second (hertz, Hz) | more of the wave is captured: a more accurate recording, but a larger file |
| Sample resolution | Number of bits per sample | more amplitude levels (n bits → 2ⁿ levels): each sample is more precise, but the file is larger |
Sound file size
The file holds every sample’s bits: sample rate × resolution × seconds. Put each value into the right unit before you multiply.
Worked example
| If the question gives… | Do this first |
|---|---|
| Sample rate in kHz | × 1000 (not 1024: frequency is not storage): 44.1 kHz = 44 100 Hz |
| Length in minutes | Convert to seconds: 3 minutes = 180 s |
| Several tracks | Find one file’s size, then multiply by the number of files |
1 A sound is recorded for 4 seconds at 2 kHz with a sample resolution of 8 bits. Find its size in bits.
2 Give that size in bytes.
3 The sample rate is increased. Give two effects.
- 12000 × 8 × 4 = 64000 bits
- 264000 ÷ 8 = 8000 bytes
- 3The recording is more accurate, and the file is larger
Measuring data storage
A text message takes a few bytes; a film takes billions. Sizes that far apart are counted in bits, bytes and much larger units. From the kibibyte up, each unit is 1024 times the one before.
Storage units, in order of size
The number by each arrow is how many of the smaller unit make the next one up.
1 nibble = 4 bits · 1 byte = 8 bits · 1 KiB = 1024 bytes · 1 MiB = 1024 KiB · and so on up to the exbibyte.
Convert step by step
Show each step and give the answer in the unit the question asks for.
Converting between units
Several files · answer in MiB
Too big? Compress it
When a file is too big to store or send easily, compression reduces its size.
Why compress a file?
- Less storage space is needed.
- Shorter transmission time: faster to upload, download or stream.
- Less bandwidth is used, e.g. on a limited data plan.
- Fits within limits such as an email attachment size.
1 How many KiB are there in 3 MiB?
2 Convert 16 384 bits to KiB.
3 Give two reasons to compress a file.
- 13 × 1024 = 3072 KiB
- 216 384 ÷ 8 = 2048 bytes; ÷ 1024 = 2 KiB
- 3Two from: less storage space, faster to send, less bandwidth used
Compression
Compression makes a file smaller by writing the same data more briefly, or by throwing some away. Lossless keeps every bit of the data; lossy removes some of it for good.
A phone photo can be too big to email or stream comfortably. Compression makes a file smaller by writing the same data more briefly, or by throwing some away. Lossless keeps every bit of the data; lossy removes some of it for good.
Lossless
No data is permanently removed. Decompression rebuilds the exact original file.
Use when every detail matters: text, program code, spreadsheets, a photo for professional printing.
Lossy
Data is permanently removed. The original cannot be rebuilt exactly.
Gives a much smaller file; often unnoticeable for photos, music and video.
Lossless: run-length encoding (RLE)
RLE finds runs of consecutive, identical data and stores each run once, as a count and a value.
- Text works the same way: AAAABBBCCD → 4A 3B 2C 1D.
- To decode (3, 0), repeat the value three times: write 000.
- RLE only helps when the data has long runs; with few repeats the file can get bigger.
Lossy methods
Name a method for an image or for sound, and say what it removes.
Lossy methods for images
- Reduce the resolution (the number of pixels).
- Reduce the colour depth (fewer bits per pixel).
- Merge similar colours that the eye cannot easily tell apart.
Lossy methods for sound
- Reduce the sample rate or sample resolution.
- Remove sounds people cannot hear, e.g. quiet sounds masked by louder ones.
Choose a method
Choose lossy or lossless, then link the reason to the scenario.
| Scenario | Choose | Linked reason |
|---|---|---|
| Emailing a written report | Lossless | every character must be kept, so the meaning is unchanged |
| Streaming video on a limited data plan | Lossy | a much smaller file uses less data and buffers less |
| Sending a photo for a large printed poster | Lossless | no detail is lost, so the print stays sharp |
1 Encode WWWWWBBW with run-length encoding.
2 Decode 2A 4B 1C.
3 A program file must be compressed. Lossy or lossless? Why?
- 1
5W 2B 1W - 2
AABBBBC - 3Lossless: every character must come back exactly, or the program will not run