• + 5 comments

    Simple O(1) solution:
    XOR a and b. Find its next power of 2. Subtract 1. Flip its bits and AND with either a or b.

    a & ~(nextpowerof2(a^b)-1)

    That's it.