Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
Let's say I have 2 numbers in binary structure as a string, and I need to add them I will do it digit by digit, from the rightmost end. So 001 + 010 = 011 But assume I need to do 001+001, by what means would it be advisable for me to make a code to sort out some way to take persist reactions?

1 Answer

0 votes
by (26.4k points)

Here, int and bin will be very useful:

a = '001'

b = '011'

c = bin(int(a,2) + int(b,2))

# 0b100

int permits you to indicate what base the first argument is in while changing over from a string (for this situation two), and bin changes over a number back to a binary string.

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...