How to add binary numbers using Python?



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
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements