# How to add two numbers without using ++ or + or another arithmetic operator

The algorithm implements concepts of basic Base 10 addition with a twist. The twist is we need to visualize this basic addition in terms of binary computation.

Here is how you can add two numbers without using any arithmetic operators. The code uses bitwise operators. This code pretty much implements a full adder which adds three bits and returns two bits ( a carry and a sum).

So lets begin the Base 10 calculation: To add 578 + 698

1. Add them but do not carry, so we get 166

2. Now add again but this time list out the carry’s i.e 1110

3. Add them both 1110+166 = 1276

Similarly in Binary we can achieve this

1. Add them but do not carry, for bits in A if we have 0 and bits in B we have 0 , we have result 0 , similarly 1 and 1 we have 0, for others we get 1 irrespective of XOR or simple addition .

XOR

0 0 0

0 1 1 — we get this even with normal addition

1 0 1 — we get this with normal addition

1 1 0

so its best to use a XOR here to get 166.

Let us try on two numbers 2 and 3

0010

0011

——

0001 — with XOR

0010

0011

———

0001 — addition by ignoring carrying

so we get the same number and hence use XOR

2.Now add considering only carry. we have carry 1 if previous values added were 1 and 1 of the two numbers. this is like AND and left shift once

1

1

—-

10 — 1 AND 1 = 1 and left shift of 1 is 10

3. Do this recursively till there is nothing to carry

#include “stdio.h”

int main()

{

int num1,num2,value;

printf(“Enter the first numbern”);

scanf(“%d”,&num1;);

printf(“enter the second numbern”);

scanf(“%d”,&num2;);

value = add(&num1;,&num2;);

printf(“the sum is : %dn”,value);

return 0;

}

int add(int *a,int *b)

{

int sum,carry;

if (*b == 0) return *a;

sum = *a ^ *b; // add without carrying

carry = (*a & *b) << 1; // carry, but don’t add

return add(?,&carry;); // recurse

}

**OUTPUT:**

Enter the first number

578

enter the second number

698

the sum is : 1276

laptop:~/code$ ./a.out

Enter the first number

34

enter the second number

45

the sum is : 79

Thanks for reading!