One line JAVA code

  • 14

    a^b is the sum of a and b bitwise without carrier, (a&b)<<1 is the carrier computation bitwise, when the carrier is equal to 0, the recursion terminate, the code is much like Euclidean gcd:

    public int getSum(int a, int b) {
        return b == 0 ? a : 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.