Number Complement - Codeprg

Breaking

programing News Travel Computer Engineering Science Blogging Earning

Tuesday 11 August 2020

Number Complement

Number Complement


Number Complement

Given a positive integer num, output its complement number. The complement strategy is to flip the bits of its binary representation.

Input: num = 5
Output: 2
Input: num = 1
Output: 0 

class Solution {
public:
    int findComplement(int num) {
        unsigned int l=0,k=0;
        while(num>0)
        {
            if(num%2==0)
            {
                l+=pow(2,k);
            }
            num/=2;
            ++k;
        }
        return l;
    }
};