binary addition in python

#Efficient Binary Addition

def binaryAddEfficient(a, b):
   if len(a)< len(b):
      a = (len(b)-len(a))*'0'+a
   elif len(b)<len(a):
      b = (len(a)-len(b))*'0'+b

   sumList=[]  #Using a list instead of a string
   carryover=0

   for i in range(len(a)-1, -1,-1):
        sum = (int(a[i]) + int(b[i]) +carryover) % 2    
        carryover = (int(a[i]) + int(b[i]) +carryover) // 2
        sumList.append(str(sum))  #Appending to the list on the right.

   if carryover:
        sumList.append(str(carryover))

   sumList.reverse()  #Reverse, because appending gets done on the right.
   return "".join(sumList)

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
addition of two binary numbers in python adding binary python binary addition and bitwise operators python add two binary numbers python binary number addition in python binary addition python inbuilt function adding binary numbers in python python add binary numbers how to do binary addition in python add binary numbers in python binary add in python adding binary numbers python perform binary addition in python add two binary numbers python or ADDITION OF BINARY NUMBERS PYTON python add binary append binary numbers python binary sum python how to add binary numbers pyhton python binary addition implement a function that adds two numbers together and returns their sum in binary in python binary addition program python python Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition. The binary number returned should be a string. add two binary numbers in python and two binary numbers in python python bit adding add binary strings python binar number addition python binary addition python binary 2 in pyhton add one to binary python add binary python add binary numbers python sum of a binary number python sum of binary python sum of digits in binary python python add one binary number to another sum 2 binary string python sum 2 binary python sum 2 binary pyrhon sum binary python Implement a function that adds two numbers together and returns their sum in binary. The conversion can be done before, or after the addition. python put numbers in binary python python adding binary how to add two binary numbers in python python string two binary numbers python add two binary numbers add 2 binary numbers python add binary strings in python how to add 2 binary numbers in python make python add binary numbers package binary addition code in python binary addition in python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source