Simple 0ms C routine without Carry


  • 0
    H

    int getSum(int a, int b) {

    if(((a^b)&((a&b)<<1))==0)
         return (a^b)|((a&b)<<1); 
                         //this always works unless the addition (read OR) of the two halves involves yet another carry,                                     //hence the next step of recursion 
    
    else return(getSum((a^b),((a&b)<<1)));
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.