Sum Two Integers Without Using Arithmetic Operators in C/C++



Given are the two integer variables, a and b. To calculate the sum without using any arithmetic operators such as + or -. We can achieve this by using bitwise operations, which allow us to calculate the sum.

Here, we have some approaches in C and C++ to solve this problem as follows:

Sum of Two Integers Using Bitwise XOR (^)

To perform the sum of two integers, you can use a bitwise operator that operates on the bits of binary data. Let's take two variables namely a and b in which first there is calculated using a & b that identifies the bit position where both numbers have 1s. Then calculate the sum as a = a ^ b which doesn't consider the carry using bitwise XOR. Next, the carry is shifted by 1 using the left shift operator for adding iteration. This way add two integers without using the arithmetic operator.

Here, we use the expression a = a ^ b that performs bitwise XOR operation between a and b. Below are the rules of XOR (exclusive OR) operator as follows:

0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

Example

In this example, you will see the usage of bitwise operator to add two integers.

C C++
#include <stdio.h>

// Sum function using bitwise operations
int sum(int a, int b) {
   while (b != 0) {
       
	   // Carry bits where both a and b are 1
       int carry = a & b;  

       // Sum without carry	   
       a = a ^ b;           

	   // Shift carry to the left
       b = carry << 1;       
   }
   return a;                
}

int main() {
   int a = 5, b = 20;

    // Calculating the sum using the sum function
    int result = sum(a, b);

    // display the result
    printf("Sum of %d and %d is: %d\n", a, b, result);

    return 0;
}

Output

The above program produces the following result:

Sum of 5 and 20 is: 25
#include <iostream>
using namespace std;

// Sum function using bitwise operations
int sum(int a, int b) {
   while (b != 0) {
   
       // Carry bits where both a and b are 1
       int carry = a & b;    
	   
       // Sum without carry
	   a = a ^ b;           

       // Shift carry to the left	   
       b = carry << 1;       
   }
   return a;                
}

int main() {
   int a = 5, b = 20;

   // Calculating the sum using the sum function
   int result = sum(a, b);
    
   // display the result
   cout << "Sum of " << a << " and " << b << " is: " << result << endl;

   return 0;
}

Output

The above program produces the following result:

Sum of 5 and 20 is: 25

Sum of Two Integers Using Vector

To ignore the arithmetic operator when adding two integers, you have a vector as an option that calculates the sum of given integers. Here, we create a custom function named sum() that accepts two parameters say (2, 3) representing them as vectors of 1s and combine these vectors for calculating sum. Next, the main() function returns the total of 1s in the combined vector and displays the result (2 + 3 = 5).

Example

The following is a C++ program that demonstrates the sum of two integers without using the arithmetic operator.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int sum(int a, int b) {

   // Represent the number as a vector of 1's
   vector<int> v1(a, 1);  
   vector<int> v2(b, 1); 

   // Combine the vectors for addition
   // Append v2 to v1
   v1.insert(v1.end(), v2.begin(), v2.end());  

   return v1.size();  
}
int main() {
    
   // Pass the values of two integers
   int add = sum(2, 3);  
   cout << add << endl;  
    return 0;
}

Output

The above program produces the following result:

5

Sum of Two Integers Using malloc() and free()

In C, adding two integers without using an arithmetic operator is possible using the functions like malloc() and free(). The malloc() easily allocated the memory by assigning the array size of two integers which represents each integer as a collection of memory blocks. Then use the while loop to calculate the sum using XOR and then return the result without any use of the + operator. Therefore, this way we can solve this problem.

Example

This is C program to show the usage of two integers sum using malloc() and free().

#include <stdio.h>
#include <stdlib.h>

int sum(int a, int b) {
   // Dynamically allocate memory for arrays of size 'a' and 'b'
   int *arr1 = (int *)malloc(a * sizeof(int)); 
   int *arr2 = (int *)malloc(b * sizeof(int)); 
   
   // initialize array with 1
   for (int i = 0; i < a; i++) {
       arr1[i] = 1;
   }
   for (int i = 0; i < b; i++) {
       arr2[i] = 1;
   }
   
   // Use bitwise operations to calculate the sum (without +)
   while (b != 0) {
       int carry = a & b;  
       a = a ^ b;         
       b = carry << 1;     
   }

   // Free the allocated memory
   free(arr1);
   free(arr2);
   
   // Return the result without using + operator  
   return a;  
}

int main() {
   int add = sum(5, 3);  
   printf("%d\n", add);   
   return 0;
}

The above code produces the following result:

8
Updated on: 2025-04-29T18:59:46+05:30

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements