Python Lab · Experiment 37
Half adder, full adder and parallel adder
PythonWeek 8JNTUH R22 · CS206ES
Aim
To write a Python program that implements a half adder and a full adder using logic gate functions, prints their truth tables, and uses four full adders as a 4-bit parallel (ripple-carry) adder to add two 4-bit binary numbers read from the user.
Algorithm
- Write the gate functions AND, OR and XOR for inputs of 0 and 1.
- Half adder: for inputs a and b, Sum = a XOR b and Carry = a AND b.
- Full adder: for inputs a, b and carry-in c, use a half adder on a and b, then a second half adder on that sum and c. Sum is the second sum, and Carry = carry1 OR carry2.
- Print the truth table of the half adder (4 rows) and of the full adder (8 rows).
- Read two 4-bit binary numbers. Ask again if a number is not exactly 4 characters of 0 and 1.
- Parallel adder: start with carry = 0. For bit positions 0 to 3, from the rightmost bit to the leftmost, pass the two bits and the carry to a full adder, keep its sum bit and pass its carry to the next position.
- Print the carry and sum of each stage, then the final result: the last carry followed by the four sum bits. Check it by printing the same addition in decimal.
Program
def and_gate(a, b):
return a & b
def or_gate(a, b):
return a | b
def xor_gate(a, b):
return a ^ b
def half_adder(a, b):
total = xor_gate(a, b)
carry = and_gate(a, b)
return total, carry
def full_adder(a, b, carry_in):
sum1, carry1 = half_adder(a, b)
total, carry2 = half_adder(sum1, carry_in)
carry_out = or_gate(carry1, carry2)
return total, carry_out
def parallel_adder(x, y):
"""Add two 4-bit strings with a chain of 4 full adders."""
carry = 0
sum_bits = ""
for i in range(4): # i = 0 is the rightmost bit
a = int(x[3 - i])
b = int(y[3 - i])
carry_in = carry
s, carry = full_adder(a, b, carry_in)
print(f" Stage {i}: A={a} B={b} Cin={carry_in} -> Sum={s} Cout={carry}")
sum_bits = str(s) + sum_bits
return sum_bits, carry
def read_4bit(prompt):
value = input(prompt).strip()
while len(value) != 4 or any(ch not in "01" for ch in value):
print("Please enter exactly 4 binary digits (0 or 1)")
value = input(prompt).strip()
return value
print("Half adder")
print(" A B | Sum Carry")
for a in [0, 1]:
for b in [0, 1]:
s, c = half_adder(a, b)
print(f" {a} {b} | {s} {c}")
print("\nFull adder")
print(" A B Cin | Sum Cout")
for a in [0, 1]:
for b in [0, 1]:
for cin in [0, 1]:
s, c = full_adder(a, b, cin)
print(f" {a} {b} {cin} | {s} {c}")
print("\n4-bit parallel adder")
x = read_4bit("Enter first 4-bit number: ")
y = read_4bit("Enter second 4-bit number: ")
sum_bits, carry = parallel_adder(x, y)
print(f"{x} + {y} = {carry}{sum_bits} (carry {carry}, sum {sum_bits})")
print(f"Check in decimal: {int(x, 2)} + {int(y, 2)} = {int(str(carry) + sum_bits, 2)}")Output
Half adder
A B | Sum Carry
0 0 | 0 0
0 1 | 1 0
1 0 | 1 0
1 1 | 0 1
Full adder
A B Cin | Sum Cout
0 0 0 | 0 0
0 0 1 | 1 0
0 1 0 | 1 0
0 1 1 | 0 1
1 0 0 | 1 0
1 0 1 | 0 1
1 1 0 | 0 1
1 1 1 | 1 1
4-bit parallel adder
Enter first 4-bit number: 1021
Please enter exactly 4 binary digits (0 or 1)
Enter first 4-bit number: 1011
Enter second 4-bit number: 0110
Stage 0: A=1 B=0 Cin=0 -> Sum=1 Cout=0
Stage 1: A=1 B=1 Cin=0 -> Sum=0 Cout=1
Stage 2: A=0 B=1 Cin=1 -> Sum=0 Cout=1
Stage 3: A=1 B=0 Cin=1 -> Sum=0 Cout=1
1011 + 0110 = 10001 (carry 1, sum 0001)
Check in decimal: 11 + 6 = 17Recorded by running this exact program. Typed input appears after each prompt.
How it works
A half adder adds two bits. It has no carry input, so it can be used only for the rightmost bit. A full adder adds three bits (two input bits and the carry from the previous position), so full adders can be chained.
The 4-bit parallel adder here is a ripple-carry adder: four full adders side by side, where the carry out of each stage is the carry in of the next one. The carry "ripples" from the rightmost bit to the leftmost. A 4-bit sum can need a fifth bit, which is the final carry out.
Checking the run by hand: 1011 is 11 and 0110 is 6. Stage 0: 1 + 0 = 1, carry 0. Stage 1: 1 + 1 = 0, carry 1. Stage 2: 0 + 1 + 1 = 0, carry 1. Stage 3: 1 + 0 + 1 = 0, carry 1. The result is 10001, which is 17 = 11 + 6.
The input 1021 in the run is rejected because 2 is not a binary digit. int(x, 2) converts a binary string to a decimal number, and is used here only to check the answer.
Common mistakes
Taking the carry out of the full adder as
carry1 AND carry2. The two half-adder carries are never 1 at the same time, so this gives 0 in every row. The carry out iscarry1 OR carry2.Adding the bits from left to right. Binary addition starts at the rightmost (least significant) bit, because the carry moves to the left.
Dropping the final carry out. 1011 + 0110 is 10001, a 5-bit result; printing only the 4 sum bits gives the wrong answer 0001.
Forgetting to convert the characters
'0'/'1'to integers before using&,|and^. These operators do not work on strings.
Viva questions
Try to answer each one aloud before you open it.
What is the difference between a half adder and a full adder?
A half adder adds two bits and gives a sum and a carry. A full adder adds three bits (two input bits and a carry in) and gives a sum and a carry out.
What are the Boolean expressions for the half adder?
Sum = A ⊕ B (XOR) and Carry = A · B (AND).
What are the Boolean expressions for the full adder?
Sum = A ⊕ B ⊕ Cin, and Cout = A·B + Cin·(A ⊕ B), which can also be written as A·B + B·Cin + A·Cin.
How many half adders are needed to make a full adder?
Two half adders and one OR gate.
What is a ripple-carry adder, and what is its drawback?
It is a parallel adder made of full adders in a chain, where each carry out feeds the next carry in. Its drawback is speed: each stage must wait for the carry from the previous stage, so the delay grows with the number of bits.
How many full adders does an n-bit ripple-carry adder need?
n full adders, one per bit. The first stage can also be a half adder, because its carry in is 0.
Learn the concepts
- Python Functions: def, Parameters and return Explained Simply
- Python Operators Explained: Arithmetic, Comparison, Logical and More
- Python return Statement: Getting Values Back From Functions
- Python Strings: Indexing, Slicing, Methods and f-strings
- Python while Loops in Depth: Conditions, Sentinels and Input Loops