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

### Question

How do I add two numbers without using ++ or + or any other arithmetic operator?

It was a question asked a long time ago in some campus interview. Anyway, today someone asked a question regarding some bit-manipulations, and in answers a beautiful quide Stanford bit twiddling was referred. I spend some time studying it and thought that there actually might be an answer to the question. I don't know, I could not find one. Does an answer exist?

1
52
8/23/2011 2:00:59 PM

This is something I have written a while ago for fun. It uses a two's complement representation and implements addition using repeated shifts with a carry bit, implementing other operators mostly in terms of addition.

``````#include <stdlib.h> /* atoi() */
#include <stdio.h>  /* (f)printf */
#include <assert.h> /* assert() */

int add(int x, int y) {
int carry = 0;
int result = 0;
int i;

for(i = 0; i < 32; ++i) {
int a = (x >> i) & 1;
int b = (y >> i) & 1;
result |= ((a ^ b) ^ carry) << i;
carry = (a & b) | (b & carry) | (carry & a);
}

return result;
}

int negate(int x) {
}

int subtract(int x, int y) {
}

int is_even(int n) {
return !(n & 1);
}

int divide_by_two(int n) {
return n >> 1;
}

int multiply_by_two(int n) {
return n << 1;
}

int multiply(int x, int y) {
int result = 0;

if(x < 0 && y < 0) {
return multiply(negate(x), negate(y));
}

if(x >= 0 && y < 0) {
return multiply(y, x);
}

while(y > 0) {
if(is_even(y)) {
x = multiply_by_two(x);
y = divide_by_two(y);
} else {
}
}

return result;
}

int main(int argc, char **argv) {
int from = -100, to = 100;
int i, j;

for(i = from; i <= to; ++i) {
assert(0 - i == negate(i));
assert(((i % 2) == 0) == is_even(i));
assert(i * 2 == multiply_by_two(i));
if(is_even(i)) {
assert(i / 2 == divide_by_two(i));
}
}

for(i = from; i <= to; ++i) {
for(j = from; j <= to; ++j) {
assert(i + j == add(i, j));
assert(i - j == subtract(i, j));
assert(i * j == multiply(i, j));
}
}

return 0;
}
``````
96
8/23/2011 2:02:41 PM

Or, rather than Jason's bitwise approach, you can calculate many bits in parallel - this should run much faster with large numbers. In each step figure out the carry part and the part that is sum. You attempt to add the carry to the sum, which could cause carry again - hence the loop.

``````>>> def add(a, b):
while a != 0:
#      v carry portion| v sum portion
a, b = ((a & b) << 1),  (a ^ b)
print b, a
return b
``````

when you add 1 and 3, both numbers have the 1 bit set, so the sum of that 1+1 carries. The next step you add 2 to 2 and that carries into the correct sum four. That causes an exit

``````>>> add(1,3)
2 2
4 0
4
``````

Or a more complex example

``````>>> add(45, 291)
66 270
4 332
8 328
16 320
336
``````

Edit: For it to work easily on signed numbers you need to introduce an upper limit on a and b

``````>>> def add(a, b):
while a != 0:
#      v carry portion| v sum portion
a, b = ((a & b) << 1),  (a ^ b)
a &= 0xFFFFFFFF
b &= 0xFFFFFFFF
print b, a
return b
``````

Try it on

``````add(-1, 1)
``````

to see a single bit carry up through the entire range and overflow over 32 iterations

``````4294967294 2
4294967292 4
4294967288 8
...
4294901760 65536
...
2147483648 2147483648
0 0
0L
``````