Full adder circuit in Python
A full adder is a digital logic circuit that adds binary numbers. It takes three single-bit inputs (two operand bits plus a carry-in bit) and produces a sum bit and a carry bit. Below is how I modelled a multi-bit full adder step by step in Python.
Full adder logic diagram
A standard full adder is built from:
- Two
XORgates - Two
ANDgates - One
ORgate

As shown in the diagram, the circuit takes three inputs — a, b, and the carry-in bit c — and produces two outputs: sum and carry-out c.
Truth table
Here is the full truth table covering all eight input combinations:

For example, setting the inputs to:
a = 1
b = 0
c = 1
Produces the following outputs:
sum = 0
c = 1
You can verify this directly in a logic circuit simulator:

Design approach
To test the Python version, I picked two 5-bit binary numbers and worked out the answer on paper first:
a = 10101
b = 00001
sum = 010110
Planned functions
Rather than relying on built-in arithmetic, I split the circuit into five small functions that mirror the hardware gates:
XORANDORfulladdercalculate
Handling the carry bit
Each 1-bit full adder produces a sum bit and a carry bit c. Starting with c = 0 at the least significant bit, the carry output from each step is fed into the next pair of bits as we move left across the binary numbers.
Building the logic gates in Python
XOR gate
An XOR (exclusive OR) gate outputs 1 when an odd number of its inputs are 1, and 0 otherwise.
def XOR(a, b):
if a != b:
return 1
else:
return 0

AND gate
An AND gate outputs 1 only when both inputs are 1; in all other cases it outputs 0.
def AND(a, b):
if a == 1 and b == 1:
return 1
else:
return 0

OR gate
An OR gate outputs 1 when at least one input is 1, and 0 only when both inputs are 0.
def OR(a, b):
if a == 0 and b == 0:
return 0
else:
return 1

Full adder function
A 1-bit full adder combines those three gates so it can be chained together to add 8-bit, 16-bit, or wider binary numbers. Looking back at the schematic with all inputs at 0:

The function takes a, b, and c, computes the intermediate nodes R, S, and T in order, and then returns the final SUM and carry c:
def fulladder(a, b, c):
R = XOR(a, b)
S = AND(a, b)
T = AND(c, R)
SUM = XOR(R, c)
c = OR(T, S)
return SUM, c
Multi-bit calculation function
To add two 5-bit binary strings, calculate() loops through the bits from right to left, passing each bit pair and the running carry c into fulladder(). Each SUM bit is appended to answer, and the final carry bit is added at the end.
def calculate():
a = '10101'
b = '00001'
c = 0
answer = ''
for i in range(len(a)-1, -1, -1):
#print(f'a and b =',a[i],b[i])
SUM, c = fulladder(int(a[i]), int(b[i]), c)
#print(f'SUM and c =', SUM, c)
answer += str(SUM)
#print(f'answer =', answer, '\n')
answer += str(c)
print(f'We want =', '010110')
print(f'We have ', answer[::-1])
Because we process the bits from right to left while appending to answer from left to right, the string ends up backwards. Reversing it with answer[::-1] gives the final binary sum.
Complete Python implementation
Here is the full script, including a helper function to pad binary inputs of different lengths with leading zeros:
def checkLength(a, b):
if a == '': a = '10101'
if b == '': b = '00001'
max_len = max(len(a), len(b))
b1 = a.zfill(max_len)
b2 = b.zfill(max_len)
return a, b
def XOR(a, b):
if a != b:
return 1
else:
return 0
def AND(a, b):
if a == 1 and b == 1:
return 1
else:
return 0
def OR(a, b):
if a == 0 and b == 0:
return 0
else:
return 1
def fulladder(a, b, c):
R = XOR(a, b)
S = AND(a, b)
T = AND(c, R)
SUM = XOR(R, c)
c = OR(T, S)
return SUM, c
def calculate():
a = '10101'
b = '00001'
c = 0
answer = ''
for i in range(len(a)-1, -1, -1):
#print(f'a and b =',a[i],b[i])
SUM, c = fulladder(int(a[i]), int(b[i]), c)
#print(f'SUM and c =', SUM, c)
answer += str(SUM)
#print(f'answer =', answer, '\n')
answer += str(c)
print(f'We want =', '010110')
print(f'We have =', answer[::-1])
calculate()
Sample output
We want = 010110
We have = 010110
Raspberry Pi GPIO draft
I also drafted a version intended for a Raspberry Pi using DIP switches as binary inputs and LEDs as outputs. The GPIO lines are commented out so it can be tested on any machine before wiring up the hardware:
# import library
# import RPi.GPIO as GPIO
# set mode to BOARD to call pins by numbers, autodetect RPI model
#
# GPIO.setmode(GPIO.BOARD)
pinsDipSwitch = [1, 2, 3, 4, 5, 6, 7, 8]
pinsLedOutput = [9, 10, 11, 12, 13]
def xorGate(input1, input2):
output1 = input1 and (not input2)
output2 = (not input1) and input2
return int(output1 or output2)
def halfAdder(input1, input2):
return (xorGate(input1, input2), input1 and input2)
def fullAdder(input1, input2, carry=0):
sum1, carry1 = halfAdder(input1, input2)
sum2, carry2 = halfAdder(sum1, carry)
return (sum2, carry1 or carry2)
def binaryCircuitAdder(input1, input2):
carry = 0
result = ''
for i in range(len(input1)-1, -1, -1):
summ, carry = fullAdder(int(input1[i]), int(input2[i]), carry)
result += str(summ)
result += str(carry)
return result[::-1]
def setIO(inputs, outputs):
for i in pinsDipSwitch:
pass
# setup specific pins as inputs
#
# GPIO.setup(i, GPIO.IN)
print(f'Input pins: ', pinsDipSwitch)
for o in pinsLedOutput:
pass
# setup specific pins as output
#
# GPIO.setup(o, GPIO.OUT)
print(f'Output pins: ', pinsLedOutput)
def getNumbersDipSwitches(pinsDipSwitch):
# read input pins
for p in pinsDipSwitch[0:4]:
# read status of 4 first input pins
#
# binaryNumber1 += str((GPIO.input(p)))
pass
print('Reading dip switches positions for 1st binary number done.')
for p in pinsDipSwitch[4:8]:
# read status of 4 last input pins
#
# binaryNumber2 += str((GPIO.input(p)))
pass
print('Reading dip switches positions for 2nd binary number done.')
# If not run on real RPi, let's assign some numbers here
binaryNumber1 = '1001'
binaryNumber2 = '0001'
return binaryNumber1, binaryNumber2
def setOutputLED(binaryNumber):
countUp = 0
for pin in pinsLedOutput:
if int(binaryNumber[countUp]) == 1:
pass
print(f'Setting output pin ', pin, 'to HIGH (', binaryNumber[countUp], ')')
# GPIO.output(pin, GPIO.HIGH)
else:
pass
print(f'Setting output pin ', pin, 'to LOW (', binaryNumber[countUp], ')')
# GPIO.output(pin, GPIO.LOW)
countUp += 1
print('Done, exiting...')
exit()
def mainFunction():
setIO(pinsDipSwitch, pinsLedOutput)
binaryNumber1, binaryNumber2 = getNumbersDipSwitches(pinsDipSwitch)
print(f'1st binary number is set to: ', binaryNumber1)
print(f'2nd binary number is set to: ', binaryNumber2)
addedNumbers = binaryCircuitAdder(binaryNumber1, binaryNumber2)
print(f'Result of the adder circuit: ', addedNumbers)
setOutputLED(addedNumbers)
if __name__ == "__main__":
mainFunction()
else:
print('Please run directly, exiting...'); quit()