Exam preparation · GCSE · OCR J277 1.2.4, AQA 8525 3.3.2, Edexcel 1CP2 2.1.3 · about 15 min
Conversions, units and file sizes at speed, with the checks that catch mistakes.
[1 mark]Convert 200 to 8-bit binary.
[1 mark]Convert 10110110 to hexadecimal.
[1 mark]How many different values can 8 bits hold?
[1 mark]A 64 by 48 image at 8 bits a pixel. How many bits?
[1 mark]What is the ASCII code for capital A?
[1 mark]Your binary answer has 7 digits. What should you check?
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
The hint students can ask for: For the binary, work down the place values from 128, taking each one that fits. For the hex, the first digit is how many sixteens and the second is what is left, looked up in a string of digits. A size in bits is the three numbers multiplied; in kB it is that divided by eight and then by a thousand.
from bugbot import *
connect()
numbers = [5, 77, 200, 255]
image = (64, 48, 8)
sound = (8000, 5, 8)
DIGITS = "0123456789ABCDEF"
def to_binary(n):
bits = ""
for value in [128, 64, 32, 16, 8, 4, 2, 1]:
if n >= value:
bits = bits + "1"
n = n - value
else:
bits = bits + "0"
return bits
def to_hex(n):
return DIGITS[n // 16] + DIGITS[n % 16]
for n in numbers:
print(f"{n} = {to_binary(n)} = {to_hex(n)}")
bits = image[0] * image[1] * image[2]
print("image:", bits, "bits")
print(f"image: {bits / 8 / 1000:.1f} kB")
bits = sound[0] * sound[1] * sound[2]
print("sound:", bits, "bits")
print(f"sound: {bits / 8 / 1000:.1f} kB")
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.