Code Review for Adding two numbers without using the + operator Here's my code. Please review it for correctness, readability and efficiency. If you think it can be improved also let me know. This code works fine for positive numbers but doesn't for negative. How can I change it ? Any comments for improvement will be appreciated. 

int sum(int a, int b)
    {
       int sum = 0;
       int carry = 0;
       int mask = 1;
       sum = doAdd(a,b,mask,carry, 0);
       return sum;
    }

    int doAdd(int a,int b, int mask, int carry, int sum)
    {
       int aBit = a&amp;mask;
       int bBit = b&amp;mask;

       if(mask&lt;a||mask&lt;b||carry!=0)
       {
          sum = sum | (carry^aBit^bBit);
          carry = (carry==0) ? (aBit&amp;bBit) : (carry&amp;a|carry&amp;b);
          mask = mask &lt;&lt; 1;
          if(carry!=0)
          carry = mask;
          return (doAdd(a,b,mask,carry, sum));
       }

       return sum;
    }
Your algorithm looks good, but the best algorithm (as far as I know) is following:


Add 2 binary numbers without considering carry (an xor operation)
Construct the difference with the actual solution, i.e., construct a number in the carry positions (a and operation then a left shift)
Add the result of step 1 with step 2 (by recursion), until step2 is 0.


The code will look like this:

int add(int a, int b) {
    if (b == 0) return a;
    return add(a ^ b, (a &amp; b) &lt;&lt; 1);
}