How to add binary numbers using Python?

PythonProgrammingServer Side Programming

If you have binary numbers as strings, you can convert them to ints first using int(str, base) by providing the base as 2. Then add the numbers like you'd normally do. Finally convert it back to a string using the bin function. For example,

a = '001'
b = '011'
sm = int(a,2) + int(b,2)
c = bin(sm)
print(c)

This will give the output:

0b100
raja
Published on 20-Apr-2018 08:11:19
Advertisements