题目描述:给定一个正整数,输出它的补数。补数是对该数的二进制表示取反。
题目链接:476.数字的补数
class Solution {
public:
int findComplement(int num) {
long long tip=1;
long long ans=0;
while (num>0){
if ((num&1)==0){
ans+=tip;
}
tip*=2;
num/=2;
}
return ans;
}
};