Java simple solution

  • 0

    The idea is to get the bitwise XOR first and then do bitwise AND. non-zero bitwise AND means XOR did not count in all the values thus iteration goes on.

    public int getSum(int a, int b) {
        int xor = a ^ b;
        int and = a & b;
        // do this until two number has no overlap
        while (and != 0) {
            a = xor;
            b = and << 1;
            xor = a ^ b;
            and = a & b;
        return xor;


Log in to reply

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