Data representation · GCSE · OCR J277 1.2.4, AQA 8525 3.3.4, Edexcel 1CP2 2.1.4 · about 15 min
Adding in binary with carries, overflow, and shifting to multiply and divide.
[1 mark]Add 01011010 and 00110111 in binary.
[1 mark]What is overflow?
[1 mark]In 8 bits, 200 + 100 overflows. What value do the 8 bits kept hold?
[1 mark]What does this program print?
print(13 << 1, 13 << 2, 13 >> 1)
[1 mark]What does shifting a binary number two places left do?
Write add_binary(a, b) yourself (no int(..., 2) or bin inside it) that adds two 8-bit strings and returns the 8-bit answer and the carry out. Print 01011010 + 00110111 = 10010001 and, on the next line, 11001000 + 01100100 = 00101100 overflow, working both answers out with your function.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def add_binary(a, b):
return "00000000", 0Plan your program here, then type it in and press Run.
shift_left(bits) on strings: drop the first character and add a 0 at the end. Check it doubles the number.