title: Adding two 4 digit binary numbers using adder circuits
body: <p>I need to write a program that adds two 4 digit binary numbers using an adder circuit that gives us an answer. I need to use the int() function to convert this from binary to decimal (both values need to be displayed). I am struggling to create a code that creates a correct binary AND decimal output. We're entering 2 sets of four digits. The digits are supposed to go through the circuit and the results should be them added together. Right now the gates are all good. But, I cannot get the digits to add together correctly. The binary addition through the circuit is incorrect. The converter and inputs are all correct.</p>
  
  <p>The user needs to input a 4 digit binary number for x (x1,x2,x3,x4) and y (y1,y2,y3,y4), and ci = 0. For example: x = 1111 y = 0000</p>
  
  <p><a href="https://i.stack.imgur.com/4KD5R.png" rel="nofollow noreferrer">This is the adder circuit along with the diagram that shows how the circuit will look after adding the two binary numbers(I can't embed images yet)</a></p>
  
  <p>this is my current code:</p>
  
  <pre><code>import string
  
  print('Simple Circuits for Final Project')
  
  x = input('x| enter 4 digits: ')
  y = input('y| enter 4 digits: ')
  
  list1 = list(x)
  print(list1)
  
  list2 = list(y)
  print(list2)
  
  
  
  #define all the gates
  def xorgate(x,y):
      if x == y:
          return '0'
      else:
          return '1'
  
  def andgate (x,y):
      if x == '1' and y == '1':
          return '1'
      else:
          return '0'
  
  def orgate (x,y):
      if x == '1' or y == '1':
          return '1'
      else:
          return '0'
  
  #define the entire circuit and input the list of inputs for the circuit.
  #include the outputs based on the gates defined above.
  def circuit(x,y,ci):
      a = 3 #starting value for the while loop, get approp position for our list
      adder = ['0','0','0','0','0']#adder list only has strings of zero. list to hold binary numbers from the output of the adding circuit
      b = 4
      while a &gt;= 0:
  
  
  
          xorout = xorgate(x[a],y[a])
          print("xor:",(xorout))
          s = xorgate(ci,xorout)
          print("Ci:",ci)
          andout = andgate(xorout, ci)
          print("and1:",andout)
          and2out = andgate(x[a],y[a])
          print("and2:",and2out)
          co = orgate(andout,and2out)
  
          print('s:',s)
          print('co:',co)
          print('-----------------')
  
          ci = co
          adder[b] = str(s)
          a-=1
          b-=1
  
  
      adder[4]=ci
      #print(final)
      print('The Final Binary Output is:', adder[::-1])
  
      #OUR CONVERTER IS RIGHT, BUT WE CAN'T GET THE BINARY ADDITION ACCURATE
      outputfinal = ((int(adder[4]) * 16) + (int(adder[3]) * 8)+(int(adder[2]) * 4)+(int(adder[1]) * 2)+(int(adder[0]) * 1))
      print('The Final Decimal Output is:', outputfinal)
  hold = '0'
  circuit(list1,list2,hold)# 3 value for circuit
  </code></pre>
  
  <p>This is the part we feel is wrong:</p>
  
  <pre><code>    ci = co
      adder[b] = str(s)
      a-=1
      b-=1
  
  
  adder[4]=ci
  #print(final)
  print('The Final Binary Output is:', adder[::-1])
  </code></pre>
  
  <p>This is how my current output is, which is wrong:</p>
  
  <pre><code>x| enter 4 digits: 1111
  y| enter 4 digits: 0000
  ['1', '1', '1', '1']
  ['0', '0', '0', '0']
  xor: 1
  Ci: 0
  and1: 0
  and2: 0
  s: 1
  co: 0
  -----------------
  xor: 1
  Ci: 0
  and1: 0
  and2: 0
  s: 1
  co: 0
  -----------------
  xor: 1
  Ci: 0
  and1: 0
  and2: 0
  s: 1
  co: 0
  -----------------
  xor: 1
  Ci: 0
  and1: 0
  and2: 0
  s: 1
  co: 0
  -----------------
  The Final Binary Output is: ['0', '1', '1', '1', '0']
  The Final Decimal Output is: 14
  </code></pre>