Add Two Numbers in Python Without Using Arithmetic Operators

Recently, I was working on a Python project where I needed to add two integers, but without using the + or – operators.

At first, it sounded like a strange requirement, but it turned out to be a fun challenge that deepened my understanding of how computers actually perform addition at the binary level.

In this tutorial, I’ll show you how to add two numbers in Python without using arithmetic operators. I’ll walk you through multiple methods, including bitwise operations, recursion, and loop-based logic.

Sum of Two Numbers without Using Arithmetic Operators in Python

To find the sum of two numbers without using arithmetic operators, Python has several methods, which you will learn section-wise, such as bitwise operations, the sum() function, the half adder logic, and the fsum () function.

Method 1: Add Two Numbers Using Python Bitwise Operations

In bitwise operations, bits like 0 and 1 are involved, so using the bitwise operation, you can perform arithmetic operations like addition.

For example, look at the function below add_bitwise(), which adds two numbers.

def add_bitwise(a, b):
    while b != 0:
        carry = a & b  # calculate the carry
        a = a ^ b      # sum of bits where at least one of the bits is not set
        b = carry << 1 # carry is shifted by one so that adding it to 'a' gives the required sum
    return a


print(add_bitwise(5, 3))

You can see the output in the screenshot below.

Add Two Numbers Bitwise Operations

From the output, you can see that when the numbers 5 and 3 are passed to the add_bitwise() function, the sum is 8.

Method 2: Add Two Numbers Using Sum() in Python

Python has a sum() function that takes an iterable of integers, such as a list, and returns the sum of those integers.

For example, you can add the same numbers 5 and 8 as in the above section using the sum() function.

# passing the 5 and 3 to sum() function to add the numbers
sum = sum([5,3])
print(sum)

You can see the output in the screenshot below.

Add Two Numbers Using Sum()

From the output, you can see that the two numbers are passed as a list [5, 3] to the sum() function to add them together; as a result, it returns the sum as 8.

Method 3: Add Two Numbers Using Half-Adder Logic

The half-adder logic is used in the combinational circuit, where two single-bit numbers are added to produce a sum or a carry. You can apply this logic to add two numbers in Python.

For example, let’s again add the two numbers using the half adder logic as shown below.

def half_adder(a, b):
    while b != 0:
        carry = a & b
        a = a ^ b
        b = carry << 1
    return a

print(half_adder(5, 3))

You can see the output in the screenshot below.

Add Two Numbers Using Half Adder Logic

From the output, you can see that using the half-adder logic, two numbers, 5 and 3, are added together to produce a sum of 8.

Method 4: Add Two Numbers Using the fsum() Function

Python has a library called math which includes a function named fsum(), this fsum() function takes an iterable list a sum() function, but it takes elements as floating point.

Let’s use this function and see how to add two numbers.

import math

def add_using_fsum(a, b):
    return math.fsum([a, b]) # using fsum() function to sum the float values

print(add_using_fsum(5, 3))

You can see the output in the screenshot below.

Add Two Numbers Using fsum() Function

From the output, you can see that two numbers, 5 and 3, are passed as an iterable like a list [5, 3] to the fsum() function, and as a result, it returns the sum as 8.0, which is correct but in float type.

So, that’s how you can add two numbers in Python without using arithmetic operators.
We explored multiple methods, bitwise, half-adder logic, sum(), and fsum() functions, and saw how each one works under the hood.

I personally prefer the bitwise method, as it’s the cleanest and most efficient. It also mirrors how real computers perform addition at the binary level.

You may like to 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.