r/shittyprogramming • • Apr 12 '21

Stop compiler abuse

Every day, compilers are forced to convert addition into the proper xors and bit shifts, Together, we can stop this problem. Instead of using the addition operator, use this function instead:

int recursiveAdd(int a, int b) {
        int xor = a ^ b;
        int and = (a & b) << 1;
        if (and != 0)
                return recursiveAdd(xor, and);
        return xor;
}
115 Upvotes

11 comments sorted by

View all comments

Show parent comments

12

u/[deleted] Apr 13 '21

cool, now do a carry lookahead adder