JAVA 0ms easy to understand!!!!


  • 0
    public int getSum(int a, int b) {
        int carry =0;
        int result =0;
        int bit =0,count=0;
       while(count<32){
           //get last bit
           int aa = a&1;
           a= a>>>1;
           //get last bit
           int bb = b&1;
           // << or <<< all available
           b= b>>>1;
            bit =aa^bb^carry;
            result |= bit<<count++;
            if(aa==1 && bb==1) carry =1;
            else if(carry ==1 && aa==1) carry=1;
            else if(carry ==1 && bb ==1) carry =1;
            else carry =0;
       }
       return result;
        
    }

Log in to reply
 

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