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;
}
};