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

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

This was a question asked a long time ago in an interview on campus. Anyway, today someone asked a question about some manipulations with bits, and the beautiful quie was mentioned in the answers

+50
c ++ c algorithm bit-manipulation
Jul 19 '09 at 13:45
source share
20 answers

This is what I wrote a while ago for fun. It uses two additions and implements adding using repeated shifts with the carry bit, implementing other operators mainly from the point of view 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) { return add(~x, 1); } int subtract(int x, int y) { return add(x, negate(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 { result = add(result, x); y = add(y, -1); } } 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; } 
+94
Jul 19 '09 at 15:59
source share

Or, instead of Jason's bitwise approach, you can calculate many bits in parallel - this should work much faster with large numbers. At each step, find out the bearing part and the part that is the sum. You are trying to add transfer in the amount that can lead to transfer again - hence the cycle.

 >>> 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 1 bit, so the sum of that 1 + 1 carries over. In the next step, you add 2 to 2 and enter four into the correct amount. It 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: To make it work easily on signed numbers, you need to enter an upper limit for 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

 add(-1, 1) 

to see that one bit carries over the entire range and overflows through 32 iterations

 4294967294 2 4294967292 4 4294967288 8 ... 4294901760 65536 ... 2147483648 2147483648 0 0 0L 
+50
Jul 19 '09 at 21:56
source share
 int Add(int a, int b) { while (b) { int carry = a & b; a = a ^ b; b = carry << 1; } return a; } 
+19
Jul 20 '09 at 1:58
source share

You can convert the adder circuit into an algorithm. They only perform bitwise operations =)

+16
Jul 19 '09 at 13:54
source share

Well, to implement the equivalent with Boolean operators is simple enough: you make a beaten sum (which is XOR), with a carry (which is AND). Like this:

 int sum(int value1, int value2) { int result = 0; int carry = 0; for (int mask = 1; mask != 0; mask <<= 1) { int bit1 = value1 & mask; int bit2 = value2 & mask; result |= mask & (carry ^ bit1 ^ bit2); carry = ((bit1 & bit2) | (bit1 & carry) | (bit2 & carry)) << 1; } return result; } 
+7
Jul 19 '09 at 23:18
source share

You have already received several responses to manipulations. Here is something else.

In C, arr[ind] == *(arr + ind) . This allows us to confuse (but legal) things a bit, such as int arr = { 3, 1, 4, 5 }; int val = 0[arr]; int arr = { 3, 1, 4, 5 }; int val = 0[arr]; .

Thus, we can define a custom add function (without explicitly using the arithmetic operator):

 unsigned int add(unsigned int const a, unsigned int const b) { /* this works b/c sizeof(char) == 1, by definition */ char * const aPtr = (char *)a; return (int) &(aPtr[b]); } 

Alternatively, if we want to avoid this trick, and if by the arithmetic operator they include | , & and ^ (therefore, direct bit manipulation is not allowed), we can do this through the lookup table:

 typedef unsigned char byte; const byte lut_add_mod_256[256][256] = { { 0, 1, 2, /*...*/, 255 }, { 1, 2, /*...*/, 255, 0 }, { 2, /*...*/, 255, 0, 1 }, /*...*/ { 254, 255, 0, 1, /*...*/, 253 }, { 255, 0, 1, /*...*/, 253, 254 }, }; const byte lut_add_carry_256[256][256] = { { 0, 0, 0, /*...*/, 0 }, { 0, 0, /*...*/, 0, 1 }, { 0, /*...*/, 0, 1, 1 }, /*...*/ { 0, 0, 1, /*...*/, 1 }, { 0, 1, 1, /*...*/, 1 }, }; void add_byte(byte const a, byte const b, byte * const sum, byte * const carry) { *sum = lut_add_mod_256[a][b]; *carry = lut_add_carry_256[a][b]; } unsigned int add(unsigned int a, unsigned int b) { unsigned int sum; unsigned int carry; byte * const aBytes = (byte *) &a; byte * const bBytes = (byte *) &b; byte * const sumBytes = (byte *) &sum; byte * const carryBytes = (byte *) &carry; byte const test[4] = { 0x12, 0x34, 0x56, 0x78 }; byte BYTE_0, BYTE_1, BYTE_2, BYTE_3; /* figure out endian-ness */ if (0x12345678 == *(unsigned int *)test) { BYTE_0 = 3; BYTE_1 = 2; BYTE_2 = 1; BYTE_3 = 0; } else { BYTE_0 = 0; BYTE_1 = 1; BYTE_2 = 2; BYTE_3 = 3; } /* assume 4 bytes to the unsigned int */ add_byte(aBytes[BYTE_0], bBytes[BYTE_0], &sumBytes[BYTE_0], &carryBytes[BYTE_0]); add_byte(aBytes[BYTE_1], bBytes[BYTE_1], &sumBytes[BYTE_1], &carryBytes[BYTE_1]); if (carryBytes[BYTE_0] == 1) { if (sumBytes[BYTE_1] == 255) { sumBytes[BYTE_1] = 0; carryBytes[BYTE_1] = 1; } else { add_byte(sumBytes[BYTE_1], 1, &sumBytes[BYTE_1], &carryBytes[BYTE_0]); } } add_byte(aBytes[BYTE_2], bBytes[BYTE_2], &sumBytes[BYTE_2], &carryBytes[BYTE_2]); if (carryBytes[BYTE_1] == 1) { if (sumBytes[BYTE_2] == 255) { sumBytes[BYTE_2] = 0; carryBytes[BYTE_2] = 1; } else { add_byte(sumBytes[BYTE_2], 1, &sumBytes[BYTE_2], &carryBytes[BYTE_1]); } } add_byte(aBytes[BYTE_3], bBytes[BYTE_3], &sumBytes[BYTE_3], &carryBytes[BYTE_3]); if (carryBytes[BYTE_2] == 1) { if (sumBytes[BYTE_3] == 255) { sumBytes[BYTE_3] = 0; carryBytes[BYTE_3] = 1; } else { add_byte(sumBytes[BYTE_3], 1, &sumBytes[BYTE_3], &carryBytes[BYTE_2]); } } return sum; } 
+6
Jul 21 '09 at 6:31
source share

All arithmetic operations are decomposed into bitwise operations, which must be implemented in electronics using the NAND, AND, OR gates, etc.

The composition of the adder can be seen here .

+5
Jul 19 '09 at 13:54
source share

For unsigned numbers, use the same adding algorithm as in the first class, but for base 2 instead of base 10. Example for 3 + 2 (base 10), that is 11 + 10 in base 2:

  1 โ€น--- carry bit 0 1 1 โ€น--- first operand (3) + 0 1 0 โ€น--- second operand (2) ------- 1 0 1 โ€น--- total sum (calculated in three steps) 
+5
Jul 19 '09 at 15:16
source share

If you feel like a comedy, always this impressively awful approach to adding two (relatively small) unsigned integers. There are no arithmetic operators anywhere in your code.

In C #:

 static uint JokeAdder(uint a, uint b) { string result = string.Format(string.Format("{{0,{0}}}{{1,{1}}}", a, b), null, null); return result.Length; } 

In C, using stdio (replace snprintf with _snprintf on Microsoft compilers):

 #include <stdio.h> unsigned int JokeAdder(unsigned int a, unsigned int b) { return snprintf(NULL, 0, "%*.*s%*.*s", a, a, "", b, b, ""); } 
+4
Jul 19 '09 at 15:59
source share

Here's a compact solution C. Sometimes recursion is more readable than loops.

 int add(int a, int b){ if (b == 0) return a; return add(a ^ b, (a & b) << 1); } 
+3
Feb 26 '15 at 10:06
source share
 short int ripple_adder(short int a, short int b) { short int i, c, s, ai, bi; c = s = 0; for (i=0; i<16; i++) { ai = a & 1; bi = b & 1; s |= (((ai ^ bi)^c) << i); c = (ai & bi) | (c & (ai ^ bi)); a >>= 1; b >>= 1; } s |= (c << i); return s; } 
+1
Nov 24 '09 at 10:33
source share
 #include<stdio.h> int add(int x, int y) { int a, b; do { a = x & y; b = x ^ y; x = a << 1; y = b; } while (a); return b; } int main( void ){ printf( "2 + 3 = %d", add(2,3)); return 0; } 
+1
Sep 21 '10 at 8:33
source share
 ## to add or subtract without using '+' and '-' ## #include<stdio.h> #include<conio.h> #include<process.h> void main() { int sub,a,b,carry,temp,c,d; clrscr(); printf("enter a and b:"); scanf("%d%d",&a,&b); c=a; d=b; while(b) { carry=a&b; a=a^b; b=carry<<1; } printf("add(%d,%d):%d\n",c,d,a); temp=~d+1; //take 2 complement of b and add it with a sub=c+temp; printf("diff(%d,%d):%d\n",c,d,temp); getch(); } 
+1
Sep 30 '11 at 19:44
source share

The following steps will work.

 x - (-y) 
0
Jul 19 '09 at 21:38
source share

Code for implementing addition, multiplication without using the + , * operator; for subtraction pass 1 addition +1 numbers to the add function

 #include<stdio.h> unsigned int add(unsigned int x,unsigned int y) { int carry=0; while (y != 0) { carry = x & y; x = x ^ y; y = carry << 1; } return x; } int multiply(int a,int b) { int res=0; int i=0; int large= a>b ? a :b ; int small= a<b ? a :b ; for(i=0;i<small;i++) { res = add(large,res); } return res; } int main() { printf("Sum :: %u,Multiply is :: %d",add(7,15),multiply(111,111)); return 0; } 
0
Sep 17 '12 at 11:23
source share

This can be done recursively:

 int add_without_arithm_recursively(int a, int b) { if (b == 0) return a; int sum = a ^ b; // add without carrying int carry = (a & b) << 1; // carry, but don't add return add_without_arithm_recursively(sum, carry); // recurse } 

or iteratively:

 int add_without_arithm_iteratively(int a, int b) { int sum, carry; do { sum = a ^ b; // add without carrying carry = (a & b) << 1; // carry, but don't add a = sum; b = carry; } while (b != 0); return a; } 
0
Jan 14 '14 at
source share

The question asks how to add two numbers, so I donโ€™t understand why all solutions suggest adding two integers? What if two numbers were floating, i.e. 2.3 + 1.8 , are they also not considered numbers? Or the question needs to be reviewed or answered.

For float, I believe that numbers should be broken down into their components, i.e. 2.3 = 2 + 0.3 , then 0.3 should be converted to an integer representation, multiplying it by the coefficient of the exponent, i.e. 0.3 = 3 * 10^-1 do the same for a different number, and then add an integer segment using one of the bit shifting methods specified as a solution to the situations described above to transfer by one digit, i.e. 2.7 + 3.3 = 6.0 = 2+3+0.7+0.3 = 2 + 3 + 7x10^-1 + 3x10^-1 = 2 + 3 + 10^10^-1 (this can be processed as two separate additions 2+3=5 and then 5+1=6 )

0
Feb 02 '15 at
source share
 int add_without_arithmatic(int a, int b) { int sum; char *p; p = (char *)a; sum = (int)&p[b]; printf("\nSum : %d",sum); } 
-one
Feb 03 '15 at 10:14
source share

With the answers above, this can be done using a single line code:

 int add(int a, int b) { return (b == 0) ? a : add(a ^ b, (a & b) << 1); } 
-one
Jan 08 '17 at 11:10
source share

I think this code would be useful for adding two numbers without an operator

 #include<stdio.h> int main() { int a, b, c; printf("enter two no. : "); scanf("%d%d", &a, &b); c = (a - ~b - 1); printf("%d\n", c); return 0; } 
-one
Jul 27 '17 at 20:07
source share



All Articles