The answersDownload the PDF
Worksheet

A7.5 Bitwise operations and characters

Data representation · A level · OCR H446 1.4.1, AQA 7517 4.5.5.1, Eduqas A500QS 2.3 · about 30 min

BugBotLab
NameClassDate

What this lesson is about

Masks with AND, OR and XOR, logical, arithmetic and circular shifts, and characters as codes in ASCII and Unicode.

Questions 6 marks in all

  1. [1 mark]What is 10110110 AND 00001111?

  2. [1 mark]Which operation and mask toggles (flips) the lowest four bits of a byte and leaves the rest unchanged?

    1. AXOR with 00001111
    2. BAND with 00001111
    3. COR with 00001111
    4. DAND with 11110000
  3. [1 mark]Give the result of an arithmetic shift right by one place of the 8-bit two's complement number 11110000.

  4. [1 mark]What does this program print?

    print(0b10110110 >> 2, 0b00010110 << 1)
  5. [1 mark]The ASCII code for the character 0 is 48. What is the denary ASCII code for the character 7?

  6. [1 mark]Why was Unicode introduced?

    1. AASCII could not represent the characters of most of the world's languages
    2. BASCII codes were too long
    3. CUnicode uses fewer bits for every character
    4. DASCII could not store digits

The task: unpack a colour

colour holds 0xFD20, a 16-bit RGB565 colour: bits 15 to 11 are red, bits 10 to 5 green, bits 4 to 0 blue. Using only shifts (>>, <<) and masks (&, |): 1. Extract the fields into r5 (0 to 31), g6 (0 to 63) and b5 (0 to 31) and print r5=<r5> g6=<g6> b5=<b5>. 2. Widen each to 8 bits (0 to 255): r8 is r5 shifted left 3, ORed with r5 shifted right 2; g8 is g6 shifted left 2, ORed with g6 shifted right 4; b8 is made like r8. Print rgb=<r8>,<g8>,<b8> with no spaces. 3. Light the LED with led(r8, g8, b8). Do not use //, %, bin or format, and do not type the answers.

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

colour = 0xFD20

r5 = 0
g6 = 0
b5 = 0
print("r5=" + str(r5), "g6=" + str(g6), "b5=" + str(b5))

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/a7-5-bitwise-operations-and-characters/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Go the other way: pack (255, 128, 64) into RGB565 and print it in hex.
  2. Write rotate_left(byte, n) for an 8-bit circular shift.
  3. Why does masking with 00001111 not work for turning the character A into a number?