Write a program in Python to add two binary numbers and output the sum.

In Python, we can add two binary numbers using the following syntax

sum = int(binary_1, 2) + int(binary_2, 2)

where binary_1 and binary_2 are two binary numbers in string format.

int() method in python convert the string into integer of the specified base. If we pass 2 as the second parameter to int() then we get the binary number as the return result (Binary have a base value of 2 whereas Decimals have base value 10).

After converting the string into binary, we add them.

Since the addition in Python returns an integer of base 10 (decimal), so we have to convert it to base 2 (binary) using bin() method.

Let’s see an example of adding two binary numbers in Python.

binary_1 = '10010'
binary_2 = '11111'

sum = int(binary_1, 2) + int(binary_2, 2)
sum = bin(sum)




Hope the program is easy for you. If you have any suggestions or doubts then please comment below.

This Post Has 2 Comments

  1. Hima

    The output should be in 110001 not in ob11001 is it possible to get that output?

Leave a Reply