Boolean algebra and logic circuits · A level · OCR H446 1.4.3, AQA 7517 4.6.4.1 · about 20 min
The full adder's expressions, building it from half adders, and chaining full adders into a ripple carry adder.
[1 mark]Which expressions give a half adder's outputs?
[1 mark]Why is a half adder not enough to add multi-bit numbers?
[1 mark]A full adder has A = 1, B = 0 and Cin = 1. Give Cout and S as two digits, Cout first.
[1 mark]How is a full adder built from half adders?
[1 mark]What does this program print?
def full_adder(a, b, cin):
s1, c1 = a ^ b, a & b
return s1 ^ cin, c1 | (s1 & cin)
carry = 0
bits = ""
for a, b in [(1, 1), (1, 0), (0, 1), (1, 0)]:
s, carry = full_adder(a, b, carry)
bits = str(s) + bits
print(bits, carry)Write three functions. Every bit is the integer 0 or 1.
- half_adder(a, b) returns a tuple (sum, carry).
- full_adder(a, b, carry_in) returns a tuple (sum, carry_out), and must be built from two calls to half_adder and an OR.
- add4(x, y) takes two strings of four characters, each "0" or "1", with the most significant bit first. It adds them with a ripple of full_adder calls, starting from the rightmost bit with a carry in of 0, and returns a tuple (total, carry): total a 4-character string of the sum bits, most significant first, and carry the final carry out (0 or 1).
Your program may not use int(), bin(), format() or sum(). For each pair ("0110", "0111"), ("1011", "0110"), ("1111", "0001") and ("0101", "1010"), in that order, print one line in exactly this form:
0110 + 0111 = 1101 carry 0
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def half_adder(a, b):
return 0, 0
def full_adder(a, b, carry_in):
return 0, 0
def add4(x, y):
return "0000", 0Plan your program here, then type it in and press Run.
add4 to add numbers of any length, and add two 8-bit numbers.