0 votes
1 view
in Python by (12.7k 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.8k points)
0 votes
1 answer
asked Oct 15, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...