Number and data questions
Conversions, units and file sizes at speed, with the checks that catch mistakes.
Covers OCR J277 1.2.4 Data storage · AQA 8525 3.3.2 Converting between number bases · Edexcel 1CP2 2.1.3 Denary and 8-bit binary
Do this lesson in the simulatorPrint it: worksheet (PDF) · answers (PDF) · as a web page
Binary, hexadecimal, units and character codes come up on every paper, and they are the easiest marks on it: there is one right answer and you can check it yourself. This lesson is the method for each, the checks that catch a slip, and a drill to make them automatic.
Show your working
Most conversion questions carry a mark for the method, so an answer with working can still score when the number is wrong. Write the place values down every time:
128 64 32 16 8 4 2 1
The five conversions
Binary to denary. Write the place values above the bits, add up the ones under a 1. 01101001 is 64 + 32 + 8 + 1 = 105.
Denary to binary. Work left to right along the place values: take the value if it fits, write 1, and subtract; otherwise write 0. Check by adding the ones back up.
Binary to hex. Split into nibbles of four bits from the right, and convert each. 10110110 is 1011 0110 = B6.
Hex to binary. Each digit becomes four bits, leading zeros included. C4 is 1100 0100.
Hex to denary. First digit times 16, plus the second. 3F is 3 × 16 + 15 = 63.
The checks that catch slips
- An 8-bit binary answer has exactly 8 digits. Count them.
- Binary ending in 0 is even; ending in 1 is odd. Does that match your denary?
- Hex
Fis 15, not 16. - The largest 8-bit number is 255, and there are 256 different values: one more than the largest.
- A left shift multiplies, a right shift divides, and a right shift can lose a remainder.
Units and file sizes
bits → bytes → kB → MB → GB, a thousand at a time (or 1024 for the binary prefixes: the question will tell you). Eight bits in a byte.
- Image: width × height × colour depth, in bits.
- Sound: sample rate × duration × bit depth (× channels), in bits.
- Text: characters × bits per character.
Convert to the unit the question asks for, and write the unit down: a number with no unit often loses the mark.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# a 64 by 48 image at 8 bits a pixel
bits = 64 * 48 * 8
print(bits, "bits =", bits / 8, "bytes =", bits / 8 / 1000, "kB")
# 5 seconds of sound, 8 kHz, 8-bit, one channel
bits = 8000 * 5 * 8
print(bits, "bits =", bits / 8 / 1000, "kB")
Character codes
ASCII is 7 bits, so 128 characters. The exam facts worth holding: capital A is 65, lower-case a is 97, and 0 is 48. The gap between a capital and its lower case is 32, and letters run in order, so C is A + 2.
Task: the conversion drill
For each number in numbers, print <denary> = <8-bit binary> = <2-digit hex>, working each one out yourself rather than using bin(), hex() or format(). Then for the image in image (width, height, bits per pixel), print image: <n> bits and image: <n> kB, and for sound (rate, seconds, bit depth) print sound: <n> bits and sound: <n> kB. Give the kB to 1 decimal place.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
numbers = [5, 77, 200, 255]
image = (64, 48, 8) # width, height, bits per pixel
sound = (8000, 5, 8) # samples a second, seconds, bits a sample
Challenges
- Add a column for the number after a left shift of one place. What happens to 200?
- Work out the size of a 10-second stereo recording at 44,100 Hz and 16 bits.
- Time yourself converting ten random numbers. Where do you slow down?