java straightforward solution


  • 0
    L


    public int getSum(int a, int b) {
    int carry = 0;
    int curBitOfA,curBitOfB,curBit;
    int res = 0;
    for(int i = 0; i < 32; i++){
    curBitOfA = (a >> i) & 1;
    curBitOfB = (b >> i) & 1;
    curBit = curBitOfA ^ curBitOfB ^ carry;
    res |= (curBit << i);
    if((curBitOfA & curBitOfB) == 1 || ((curBitOfA | curBitOfB) & carry) == 1) carry = 1;
    else carry = 0;
    }
    return res;
    }


Log in to reply
 

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