Open In App

Bitwise recursive addition of two integers

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two integers x and y, the task is to perform bitwise recursive addition of two integers.

Examples : 

Input: x = 45, y = 45
Output: 90

Input: x = 4, y = 78
Output: 82

Approach:

The idea is to mimic the manual process of binary addition, where we calculate the carry and add it to the sum of x and y. Using bitwise operations, the XOR (x ^ y) gives the sum without carry, and the AND (x & y) identifies the carry, which is then shifted left. The recursion continues until the carry becomes 0, ensuring complete addition.

XOR Operation (x ^ y): This operation adds the two numbers without considering the carry. It gives a result where each bit is 1 if one of the corresponding bits of x or y is 1, but not both. Essentially, it is like performing addition without considering the carry.

AND Operation (x & y): This operation identifies the carry bits. A carry occurs when both corresponding bits of x and y are 1. The AND operation gives 1 for these positions, which is then shifted left (<< 1) to represent the carry for the next higher bit position.

The function first calculates the carry and adds it to the sum using the XOR operation. It then recursively calls itself with the new sum (from XOR) and the new carry (from AND shifted left). This continues until there is no carry left, at which point the sum is fully computed. When the carry becomes 0, the recursion stops, and the sum of the two numbers is returned.

C++
// C++ Program to add two numbers 
// without using arithmetic operators
#include <bits/stdc++.h>
using namespace std;

// Recursive function to add two numbers 
// without arithmetic operators
int addTwoNum(int x, int y) {

    // Base case: if no carry, return x
    if (y == 0) {
        return x;
    }

    // Calculate carry and update x and y 
    int carry = (x & y) << 1;
    return addTwoNum((x ^ y), carry);
}

int main() {
  
    int x = -10, y = 5;

    cout << addTwoNum(x, y);
    return 0; 
}
C
// C Program to add two numbers 
// without using arithmetic operators
#include <stdio.h>

// Recursive function to add two numbers 
// without arithmetic operators
int addTwoNum(int x, int y) {

    // Base case: if no carry, return x
    if (y == 0) {
        return x;
    }

    // Calculate carry and update x and y 
    int carry = (x & y) << 1;
    return addTwoNum(x ^ y, carry);
}

int main() {
    int x = -10, y = 5;

    printf("%d", addTwoNum(x, y));
    return 0;
}
Java
// Java Program to add two numbers 
// without using arithmetic operators
class GfG {

    // Recursive function to add two numbers 
    // without arithmetic operators
    static int addTwoNum(int x, int y) {

        // Base case: if no carry, return x
        if (y == 0) {
            return x;
        }

        // Calculate carry and update x and y 
        int carry = (x & y) << 1;
        return addTwoNum((x ^ y), carry);
    }

    public static void main(String[] args) {

        int x = -10, y = 5;

        System.out.println(addTwoNum(x, y));
    }
}
Python
# Python Program to add two numbers 
# without using arithmetic operators

# Recursive function to add two numbers 
# without arithmetic operators
def addTwoNum(x, y):
    
    # Mask to simulate 32-bit integer overflow
    mask = 0xFFFFFFFF
    maxInt = 0x7FFFFFFF

    # Base case: if no carry, return x
    if y == 0:
        
        # If x exceeds maxInt, interpret
        # it as a negative number
        return x if x <= maxInt else ~(x ^ mask)

    # Calculate carry and update x
    # and y with 32-bit masking
    carry = (x & y) << 1
    return addTwoNum((x ^ y) & mask, carry & mask)

if __name__ == "__main__":
    x = -10
    y = 5

    print(addTwoNum(x, y))
C#
// C# Program to add two numbers 
// without using arithmetic operators
using System;

class GfG {

    // Recursive function to add two numbers 
    // without arithmetic operators
    static int addTwoNum(int x, int y) {

        // Base case: if no carry, return x
        if (y == 0) {
            return x;
        }

        // Calculate carry and update x and y 
        int carry = (x & y) << 1;
        return addTwoNum((x ^ y), carry);
    }

    static void Main(string[] args) {

        int x = -10, y = 5;

        Console.WriteLine(addTwoNum(x, y));
    }
}
JavaScript
// JavaScript Program to add two numbers 
// without using arithmetic operators

// Recursive function to add two numbers 
// without arithmetic operators
function addTwoNum(x, y) {

    // Base case: if no carry, return x
    if (y === 0) {
        return x;
    }

    // Calculate carry and update x and y 
    let carry = (x & y) << 1;
    return addTwoNum((x ^ y), carry);
}

const x = -10, y = 5;
console.log(addTwoNum(x, y));

Output
-5

Time Complexity: O(n), where n is the number of bits in the larger number, since each recursive call reduces the number of bits being carried.
Auxiliary Space: O(n), due to the recursion depth, where n is the number of bits in the larger number.


Article Tags :

Similar Reads