Sum of two integers


  • 1
    A

    hi, I passed this question with the following code, but I found if I declare int carry = 0 outside while loop, it got "Internal Error". I don't know why this way cannot pass the test cases and got error. Do anyone know why?

    int getSum(int a, int b) {
    
        
        //int carry = 0;
        while(b!=0)
        {
            int carry = (a&b) << 1;
            a = a^b;
            b = carry;    
            
        }
        
        return a;
    
        
        
    }

  • 0
    L

    oh wait I made a mistake, your code works fine, don't see any error here. try to see if there is a syntax error


  • 0
    A

    but I think in the while loop, carry = (a&b) << 1 will modify the value, does this thought wrong?


Log in to reply
 

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