Add two numbers without using + operator in Python

In this Python tutorial, I will explain how to add two numbers without using + operator in Python using different methods with some illustrative examples.

In traditional mathematics, we use the + operator to add two numbers. However, in the realm of programming, and especially in Python, there are multiple ingenious ways to achieve the same without using the + operator.

Methods to add two numbers without using + operator in Pythons

There are four different Python methods to add numbers without + operator.

  • Using a For Loop
  • Using sum()
  • Using the math library
  • Using Bitwise Addition

Let’s see them one by one using demonstrative examples:

Method 1: Use for loop to add two numbers without + in Python

This method in Python employs a for loop that incrementally adds one to the first number (a) and subtracts one from the second number (b) until b becomes zero. The result is the sum of and decrement and b.

Scenario: Let’s say two friends in Chicago decide to collect postcards from each state they visit. One collects postcards from 10 states, while the other collects from 5 states. How many states have they visited combined? Let’s add two numbers without using + operator in Python.

def add(a, b):
    while b != 0:
        a += 1
        b -= 1
    return a

friend1_states = 10
friend2_states = 5
total_states_visited = add(friend1_states, friend2_states)
print('Total state visited:', total_states_visited)

Output: The for loop method in Python increments one number while decrementing the other until they’ve combined.

Total state visited: 15
add two numbers without using + operator in python

This way, we can add two numbers without + operator using for loop in Python.

Method 2: Python add two numbers without + with sum()

The sum() function in Python directly computes the aggregate of a sequence of numbers. The two numbers can be summed up concisely by placing them in a tuple.

Scenario: In a Texas BBQ competition, one participant cooks 20 ribs, and another cooks 30. How many ribs are there in total? Adding two numbers without + operator in Python using sum().

def add(a, b):
    return sum((a, b))

ribs_cook1 = 20
ribs_cook2 = 30
total_ribs = add(ribs_cook1, ribs_cook2)
print('Total numbers of ribs cooked:', total_ribs)

Output: The Python sum() function can directly calculate the total of any number of values when passed as an iterable.

Total numbers of ribs cooked: 50
Python add numbers without + operator

This way, we can use sum() to add two numbers without using the + operator in Python.

Method 3: Add two numbers without + operator Python using the math library

The math library Python method leverages properties of logarithms and exponentials to perform addition. The exponents of the two numbers are multiplied, and then the logarithm provides the combined value.

Scenario: In a food festival in Maine, one stall sells 100 lobsters and another sells 60. How many lobsters are sold in total? Let’s try to add these two numbers in Python without + operator.

import math

def add(a, b):
    return math.log(math.exp(a) * math.exp(b))

lobsters_stall1 = 100
lobsters_stall2 = 60
total_lobsters = add(lobsters_stall1, lobsters_stall2)
print('Total number of lobsters sold are:', total_lobsters)

The output is:

Total number of lobsters sold are: 160.0
two numbers addition without + Python

This way we can use the math library as a substitute for + operator in Python to add two numbers.

Method 4: Using bitwise addition for two numbers in Python

At the heart of computers, numbers are represented using binary digits. The bitwise approach to adding numbers in Python exploits this representation by using operations that work on individual bits. For this method, we primarily rely on the bitwise XOR ^, left shift <<, and bitwise AND & operations.

Here’s a step-by-step breakdown:

  • Bitwise XOR (^): This operation returns 1 for each position where either of the corresponding bits of numbers is 1, but not both. This essentially gives us a sum of the two numbers, but without considering the carry.
  • Bitwise AND (&): This operation returns 1 for each position where both corresponding bits are 1. This represents the carry for the addition.
  • Left Shift (<<): By shifting the result of the AND operation to the left by one position, we get the actual carry values.

The above three steps give us two numbers – the sum without the carry and the carry itself. We recursively add these two until the carry is 0.

Let’s see the Python function which will add two numbers using bitwise addition:

def add(x, y):
    # Iterate till there is no carry
    while y != 0:
        # Carry contains common set bits of x and y
        carry = x & y

        # Sum of bits of x and y where at least one of the bits is not set
        x = x ^ y

        # Carry is shifted by one so that adding it to x gives the required sum
        y = carry << 1

    return x

Scenario-Based Example: Imagine me and my friend are driving across the USA. I cover 2,450 miles from New York to Los Angeles, and my friend covers an additional 1,200 miles exploring the West Coast. How many total miles did we cover together without + operator in Python?

def add(x, y):
    while y != 0:
        carry = x & y
        x = x ^ y
        y = carry << 1
    return x

miles_you = 2450
miles_friend = 1200
total_miles = add(miles_you, miles_friend)
print('Total miles covered is:', total_miles)

Output:

Total miles covered is: 3650
Python addition of two numbers without + Operator

There is a recursion method in Python, which is a bitwise method where the addition occurs without carry first (using XOR operation). Then, the carry is computed (using AND operation). This process is recursively repeated until there’s no carry left, leading to the final sum.

Scenario: In New York, a skyscraper has 80 floors, and another one nearby has 50 floors. How many floors are there combined in both skyscrapers?

def add(a, b):
    if b == 0:
        return a
    else:
        sum_ = a ^ b
        carry = (a & b) << 1
        return add(sum_, carry)

skyscraper1_floors = 80
skyscraper2_floors = 50
total_floors = add(skyscraper1_floors, skyscraper2_floors)
print(total_floors)

Output:

130
Python two number addition without +

This way, we can add two numbers without the + operator in Python.

Conclusion

This tutorial explains how to add two numbers without using + operator in Python using four different methods like sum(), for loop, math library, or doing bitwise addition with some illustrative examples.

You may also like to read: