Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def AND(a, b):
- if a == 1 and b == 1:
- return 1
- return 0
- def NOT(a):
- if a == 1:
- return 0
- elif a == 0:
- return 1
- def XOR(a, b):
- if a == 1 and b == 1:
- return 0
- return 1
- def OR(a, b):
- if a == 1 or b == 1:
- return 1
- return 0
- def HALF_ADD(p, q):
- # Add bits through half adder
- a = AND(p, q)
- o = OR(p, q)
- return (a, AND(NOT(a), o))
- def FULL_ADD(p, q, r):
- # Add 3 bits through full adder
- c1 = HALF_ADD(p, q)[0]
- s1 = HALF_ADD(p, q)[1]
- c2 = HALF_ADD(s1, r)[0]
- s2 = HALF_ADD(s1, r)[1]
- return OR(c1, c2), s2
- def main():
- # 0 1
- # CarryOut, Sum
- a = [0, 1, 0] # 4 2 1
- b = [1, 1, 1] # 4 2 1
- sum1 = HALF_ADD(a[2], b[2])[1]
- c1 = HALF_ADD(a[2], b[2])[0]
- sum2 = FULL_ADD(a[1], b[1], c1)[1]
- c2 = FULL_ADD(a[1], b[1], c1)[0]
- sum3 = FULL_ADD(a[0], b[0], c2)[1]
- c3 = FULL_ADD(a[0], b[0], c2)[0]
- '''
- print("Sum1 is: ", sum1)
- print("c1 is: ", c1)
- print("Sum2 is: ", sum2)
- print("c2 is: ", c2)
- print("Sum3 is: ", sum3)
- print("c3 is: ", c3)
- '''
- print(a)
- print("+++")
- print(b)
- print("results in:")
- print(c3, sum3, sum2, sum1)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement