Number Complement using bitwise AND


  • 0
    R
    class Solution {
        public int findComplement(int num) {
            int digit = 0, power = 1, newnum = 0;
            while(num > 0){
                digit = num & 1; 
                if(digit == 0){
                    newnum += power; 
                }
                power=power*2;
                num = num>>1;
            }
            return newnum;
        }
    }
    

Log in to reply
 

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