Another One line C++ Bit Manipulation


  • 0
    D
    class Solution {
    public:
        int findComplement(int num) {
            return (~num) << (32-(int)log2(num)-1) >> 32-(int)log2(num)-1;
        }
    };
    

    This method takes advantage over the fact that we are dealing with integers and that there will be no leading zero in the input.


Log in to reply
 

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