Python Program to Add Two Binary Numbers

Recently, I was working on a project where I had to process some binary data. The challenge was simple: I needed to add two binary numbers in Python.

At first, I thought this would be easy, but then I realized there are multiple ways to do it. Depending on the situation, one method may be faster or easier to understand than another.

In this tutorial, I’ll show you different ways to add two binary numbers in Python. I’ll also share my firsthand experience so you can pick the method that works best for your project.

Method 1 – Use Python’s bin() and int() Functions

The easiest way to add two binary numbers in Python is to convert them into decimal, perform the addition, and then convert back to binary.

Here’s how I do it:

# Python program to add two binary numbers using bin() and int()

# Two binary numbers
binary_num1 = "1011"   # 11 in decimal
binary_num2 = "1101"   # 13 in decimal

# Convert to decimal and add
sum_decimal = int(binary_num1, 2) + int(binary_num2, 2)

# Convert back to binary
sum_binary = bin(sum_decimal)

# Print results
print("First Binary Number:", binary_num1)
print("Second Binary Number:", binary_num2)
print("Sum in Binary:", sum_binary[2:])  # Remove '0b' prefix

Output:

First Binary Number: 1011
Second Binary Number: 1101
Sum in Binary: 11000

I executed the above example code and added the screenshot below.

binary addition in python

This method is quick and works perfectly when you’re dealing with binary strings.

Method 2 – Add Binary Numbers Using format()

Another approach I often use is the format() function in Python. It makes the output cleaner and avoids manually slicing strings.

# Python program to add two binary numbers using format()

binary_num1 = "1110"   # 14 in decimal
binary_num2 = "1010"   # 10 in decimal

# Perform addition
sum_decimal = int(binary_num1, 2) + int(binary_num2, 2)

# Convert to binary using format
sum_binary = format(sum_decimal, "b")

print("Binary Sum:", sum_binary)

Output:

Binary Sum: 11000

I executed the above example code and added the screenshot below.

binary addition python

This method is more elegant and beginner-friendly.

Method 3 – Bitwise Addition (Without Using + Operator)

Sometimes, I like to challenge myself by avoiding built-in arithmetic operators. Using bitwise operators, we can add binary numbers just like a computer would at the hardware level.

# Python program to add two binary numbers using bitwise operators

def add_binary(a, b):
    while b != 0:
        # Carry now contains common set bits of a and b
        carry = a & b

        # Sum of bits where at least one is not set
        a = a ^ b

        # Carry is shifted by one so that adding it to a gives the required sum
        b = carry << 1
    return a

# Example usage
binary_num1 = int("1011", 2)  # 11
binary_num2 = int("1101", 2)  # 13

result = add_binary(binary_num1, binary_num2)

print("Sum in Binary:", bin(result)[2:])

Output:

Sum in Binary: 11000

I executed the above example code and added the screenshot below.

python binary addition

This method is somewhat advanced, but it’s useful if you want to understand how computers perform binary addition at the bit level.

Method 4 – Use User Input

In real-world applications, I often need to take binary numbers as input from users. Here’s a simple way to do it:

# Python program to add two binary numbers from user input

# Take binary input from user
binary_num1 = input("Enter first binary number: ")
binary_num2 = input("Enter second binary number: ")

# Convert and add
sum_decimal = int(binary_num1, 2) + int(binary_num2, 2)

# Convert back to binary
print("Sum in Binary:", bin(sum_decimal)[2:])

Example Run:

Enter first binary number: 1010
Enter second binary number: 1111
Sum in Binary: 11001

This method is perfect if you’re building a small tool or script for end users.

Method 5 – Use Python’s bin() with map() for Multiple Numbers

Sometimes, I need to add more than two binary numbers. Instead of repeating the process, I use map() with sum().

# Python program to add multiple binary numbers

binary_numbers = ["1010", "1101", "111"]

# Convert all to decimal and sum them
sum_decimal = sum(map(lambda x: int(x, 2), binary_numbers))

# Convert back to binary
print("Sum of all binary numbers:", bin(sum_decimal)[2:])

Output:

Sum of all binary numbers: 11100

This method is concise and works great when you have a list of binary numbers.

When I first started working with binary numbers in Python, I thought it would be complicated. But as you’ve seen, Python makes it simple with functions like bin(), int(), and even bitwise operators if you want to go deeper.

You don’t need to stick to just one method: pick the one that feels most natural for your project. For quick tasks, I usually go with bin() and int(). If I’m building something more advanced, I like to experiment with bitwise addition.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.