C++ Fast and Short, Finding Highest 1-Bit


  • 0
    M
    class Solution {
    public:
        int findComplement(int num) {
            int n = num, prev = 0;
            while (n) {
                prev = n;
                n &= n - 1;
            }
            return num ^ ((prev << 1) - 1);
        }
    };
    

Log in to reply
 

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