Numbers: converting between bases
Three ranges define everything in this section, and questions stay inside them:
• denary 0 to 255 • binary 00000000 to 11111111 • hexadecimal 00 to FF
Those are the same range written three ways — one byte, eight bits.
Two terms are required. The most significant bit is the leftmost bit, worth the most (128 in an 8-bit number). The least significant bit is the rightmost, worth 1.
You should be able to deal with binary numbers of between 1 and 8 bits, padding with leading zeros where needed: 11010 is the same as 00011010.
Worked example
Convert denary 154 to binary, and then to hexadecimal.
- Work left to right through the place values 128, 64, 32, 16, 8, 4, 2, 1.
- 128 fits into 154 — write 1, leaving 26. 64 does not fit into 26 — write 0. 32 does not — write 0. 16 fits, leaving 10 — write 1.
- 8 fits into 10, leaving 2 — write 1. 4 does not — write 0. 2 fits, leaving 0 — write 1. 1 does not — write 0.
- So 154 = 10011010. Check by adding back: 128 + 16 + 8 + 2 = 154.
- For hexadecimal, split into nibbles: 1001 and 1010. 1001 = 8+1 = 9. 1010 = 8+2 = 10, which is A.
Answer: 154 = 10011010 in binary, and 9A in hexadecimal.
Hexadecimal uses sixteen digits: 0 to 9 then A to F, where A is 10 and F is 15. It is used because it is far more compact than binary and easier for people to read without error — one hex digit replaces four bits exactly, so a byte is always two hex digits.
- Most significant bit
- the leftmost bit in a binary number, worth the most — 128 in an 8-bit number
- Least significant bit
- the rightmost bit in a binary number, worth 1
- Hexadecimal
- base 16, used as a shorter and less error-prone way of writing binary; one byte is written as two hexadecimal digits from 00 to FF
Binary addition and overflow
Binary addition works exactly like denary addition, carrying when a column exceeds what one digit can hold. The four rules:
0 + 0 = 0 0 + 1 = 1 1 + 1 = 0, carry 1 1 + 1 + 1 = 1, carry 1
An overflow error occurs when the result needs more bits than are available. With 8 bits the largest value is 255, so any sum reaching 256 or more overflows: the carry has nowhere to go and is lost, leaving a wrong answer.
That matters practically — a program that overflows does not usually crash, it silently produces an incorrect value, which is far harder to spot.
Worked example
Add 11010110 and 01101011 using 8-bit binary, and explain the result.
- Add column by column from the right, carrying where needed.
- 0+1=1; 1+1=0 carry 1; 1+0+1=0 carry 1; 0+1+1=0 carry 1; 1+0+1=0 carry 1; 0+1+1=0 carry 1; 1+1+1=1 carry 1; 1+0+1=0 carry 1.
- The eight-bit result is 01000001, with a carry of 1 left over.
- Check in denary: 214 + 107 = 321, which is greater than 255.
- The leftover carry cannot be stored in 8 bits.
Answer: The 8-bit result is 01000001 with a carry out, which is an overflow error — the true answer, 321, needs 9 bits and cannot be stored in 8.
Binary shifts move every bit left or right by a number of places.
• A shift left by one place multiplies the number by 2. A shift left by n places multiplies by 2ⁿ. • A shift right by one place divides by 2. A shift right by n places divides by 2ⁿ.
Bits shifted off the end are lost, and zeros fill in behind. So a left shift can push a 1 off the most significant end, causing overflow, and a right shift can push a 1 off the least significant end, losing precision — a right shift of an odd number rounds it down.
- Overflow error
- an error that occurs when the result of a calculation needs more bits than are available; with 8 bits the largest value is 255, so any sum reaching 256 overflows
Characters
Characters are represented in binary using a character set — an agreed table matching each character to a binary code.
The relationship between bits per character and how many characters can be represented is the key idea: n bits give 2ⁿ different codes. So 7 bits give 128 characters, and 8 bits give 256.
Character sets are logically ordered, which is examined directly. The codes for letters run in sequence, so the code for 'B' is one more than the code for 'A', and the code for 'z' is one more than 'y'. That is why a program can compare or sort text by comparing character codes.
It also means you can work out an unknown code from a given one: if 'A' is 65, then 'D' is 68. Memorisation of character set codes is not required, so any code you need will be given.
- Character set
- an agreed table matching each character to a binary code; n bits give 2ⁿ different codes, so 7 bits give 128 characters and 8 bits give 256
Images and sound
Images. A bitmap image is stored as a grid of pixels, each pixel having a specific colour represented by a binary code.
• Colour depth is the number of bits used for each pixel. More bits mean more available colours: 1 bit gives 2 colours, 8 bits give 256, and 24 bits give over 16 million. • Resolution is the number of pixels in the image, usually given as width × height. More pixels mean more detail.
Increasing either improves quality and increases file size, because file size is colour depth × height × width.
Metadata is additional information stored with the image — its height, width, colour depth, and often the date taken and camera settings. It is needed so that software knows how to display the file: without the width, a long string of pixel values cannot be arranged into a picture.
Sound. Sound is analogue — a continuously varying wave — and must be converted to binary to be stored. This is done by sampling: measuring the height of the wave at regular intervals and storing each measurement as a binary number.
• Sample rate is how many samples are taken each second, measured in hertz (Hz). • Duration is how many seconds of audio the file contains. • Bit depth is the number of bits used to store each sample, for example 16-bit.
A higher sample rate captures more of the wave's detail, and a greater bit depth records each measurement more precisely — so both improve playback quality, and both increase file size, since file size is sample rate × duration × bit depth.
Common mistake
Colour depth and resolution are different things, and so are sample rate and bit depth. Colour depth and bit depth are about how precisely each individual value is recorded; resolution and sample rate are about how many values there are.
- Colour depth
- the number of bits used for each pixel in a bitmap image; more bits mean more available colours
- Resolution
- the number of pixels in an image, usually given as width by height
- Sample rate
- the number of samples taken per second when recording sound, measured in hertz
- Bit depth
- the number of bits used to store each sound sample
Before you practise
- Ranges: denary 0–255, binary 00000000–11111111, hexadecimal 00–FF.
- Most significant bit is leftmost; least significant is rightmost. Pad short binary numbers with leading zeros.
- Binary to denary: add the place values where there is a 1. Denary to binary: work left to right through the place values.
- Binary to hex: split into two nibbles of four bits and convert each, using A–F for 10–15.
- Binary addition carries like denary; an overflow error is a result needing more bits than are available.
- A left shift multiplies by 2 per place, a right shift divides by 2 — bits pushed off the end are lost.
- n bits give 2ⁿ characters. ASCII uses 8 bits in the exam; Unicode uses 16 or more and covers far more characters.
- Character sets are logically ordered, so 'B' is one more than 'A'. Codes need not be memorised.
- Images: colour depth is bits per pixel, resolution is how many pixels. Metadata records height, width and colour depth.
- Sound: sample rate is samples per second, bit depth is bits per sample. Both raise quality and file size.