Simulating 4bit FullAdder in python, not getting correct output

23 views Asked by At

Simulation of FullAdder Logic Gate, takes in carry and 2 things it needs to add

class FullAdder:
    def __init__(self,a,b,c):
        self.a = a
        self.b = b
        self.c = c

        def output(self):
            CandS1 = HalfAdder(self.a,self.b).output() # Sends out: [Carry, Sum] either 1 or 0
            CandS2 = HalfAdder(CandS1[1], self.c).output() #Again Sends out: [Carry, Sum] either 1 or 0
            Cout = ORGate(CandS2[0],CandS1[0]).output() #Sends out final bit either 1 or 0
            return [Cout, CandS2[1]] # Final Carry out bit + Sum of second Half Adder

Current Problem: When I input a 1111 and b 1111 newnum = 11110 its correct, but when I input a = 1011 b = 1111 newnum = 11100 which is incorrect as it should produce: 11010

if __main == "__name__":

    newnum = []
    a = [1,0,1,1] #4 bits, if a = [1,1,1,1], b = [1,1,1,1] then output is 11110 correct
    b = [1,1,1,1]

    carry = 0 #intital carry bit

    for i in range(0,4): #goes through 4 adders
        output = FullAdder(a[i], b[i], carry).output() #Takes bits from A & B, and puts in most recent carry
        newnum.append(output[1]) #SUM, appended to final answer array: newnum
        carry = output[0] #CARRY matches the output, of most recent FullAdder, [Carry or Sum], output[0] = carry


    newnum.append(carry)
    print(newnum)
    newnum.reverse()
    print(newnum)
    print("1111 + 1111 = ", newnum)
0

There are 0 answers