1#Efficient Binary Addition
2
3def binaryAddEfficient(a, b):
4 if len(a)< len(b):
5 a = (len(b)-len(a))*'0'+a
6 elif len(b)<len(a):
7 b = (len(a)-len(b))*'0'+b
8
9 sumList=[] #Using a list instead of a string
10 carryover=0
11
12 for i in range(len(a)-1, -1,-1):
13 sum = (int(a[i]) + int(b[i]) +carryover) % 2
14 carryover = (int(a[i]) + int(b[i]) +carryover) // 2
15 sumList.append(str(sum)) #Appending to the list on the right.
16
17 if carryover:
18 sumList.append(str(carryover))
19
20 sumList.reverse() #Reverse, because appending gets done on the right.
21 return "".join(sumList)