The worksheetDownload the PDF
Answers

F8.3 Hexadecimal

Data representation · GCSE · OCR J277 1.2.4, AQA 8525 3.3.2, Edexcel 1CP2 2.1.6 · about 15 min

BugBotLab

What this lesson is about

Base 16, converting to binary and denary, and the LED's colour codes.

Questions 6 marks in all

  1. [1 mark]Convert 10110110 to hexadecimal.

    Answer: B6. 1011 is B and 0110 is 6.
  2. [1 mark]What is hex 3F in denary?

    Answer: 63. 3 × 16 + 15.
  3. [1 mark]Convert 255 to hexadecimal.

    Answer: FF. 15 sixteens and 15 ones: FF.
  4. [1 mark]Why do programmers use hexadecimal?

    1. AIt is shorter and easier to read than binary, and easy to convert
    2. BComputers store data in hexadecimal
    3. CIt takes less memory
    4. DIt is more accurate than binary
    Answer: A. Hex is for people; the computer still stores binary.
  5. [1 mark]What colour is #00FF00 on the LED?

    1. AGreen
    2. BRed
    3. CBlue
    4. DWhite
    Answer: A. Red 00, green FF, blue 00.
  6. [1 mark]How many bits does one hex digit stand for?

    Answer: 4. One hex digit is a nibble.

The task: mix a colour

Write to_hex(n) yourself (no hex or format) that turns 0 to 255 into two hex digits. Use it to build the colour code for red 255, green 128, blue 64, print it as #FF8040, and show it on the LED by passing your code to led.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

DIGITS = "0123456789ABCDEF"

def to_hex(n):
    return "00"

The hint students can ask for: Each byte becomes two hex digits: the first is how many sixteens, the second is what is left over. Look each one up in the digit string, join the six digits behind a hash, print it and give it to the LED.

A solution

from bugbot import *
connect()
DIGITS = "0123456789ABCDEF"

def to_hex(n):
    return DIGITS[n // 16] + DIGITS[n % 16]

code = "#" + to_hex(255) + to_hex(128) + to_hex(64)
print(code)
led(code)

Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.