binary addition in python

Solutions on MaxInterview for binary addition in python by the best coders in the world

showing results for - "binary addition in python"
Zane
01 Feb 2019
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)
queries leading to this page
sum 2 binary pyrhonsum of binary pythonpython string two binary numbersbinar number addition pythonadd binary strings in pythonhow to add binary numbers pyhtonadd 2 binary numbers pythonaddition of binary numbers pytonsum of digits in binary pythonimplement a function that adds two numbers together and returns their sum in binary in pythonbinary addition pythonbinary 2 in pyhtonimplement a function that adds two numbers together and returns their sum in binary the conversion can be done before 2c or after the addition pythonadding binary numbers pythonpython adding two binary numbersbinary additionn pythonadd one to binary pythonhow to add 2 binary numbers in pythonaddition of two binary numbers in pythonpython bit addingbinary addition and bitwise operators pythonadding binary numbers in pythonadd binary strings pythonadd binary numbers pythonbinary number addition in pythonbinary add in pythonadd binary numbers in pythonadd binary pythonbinary addition in pythonput numbers in binary pythonappend binary numbers pythonbinary addition python inbuilt functionand two binary numbers in pythonsum binary pythonpython add binarysum of a binary number pythonhow to add two binary numbers in pythonhow to do binary addition in pythonbinary addition code in pythonpython implement a function that adds two numbers together and returns their sum in binary the conversion can be done before 2c or after the addition the binary number returned should be a string make python add binary numbers packagesum 2 binary string pythonadding binary pythonpython adding binarypython add two binary numbersadd two binary numbers in pythonpython add one binary number to anotherpython add binary numberssum 2 binary pythonpython binary additionbinary sum pythonadd two binary numbers pythonadd two binary numbers python orperform binary addition in pythonbinary addition program pythonbinary addition in python