【leetcode】476. Number Complement

problem

476. Number Complement

solution1:

class Solution {
public:
    int findComplement(int num) {
        //正数的补数是对应二进制各位翻转,且从非零高位开始。
        bool start = false;
        for(int i=31; i>=0; i--)//err.
        {
            if(num & (1<<i)) start = true;
            if(start) num ^= (1<<i);
        }
        return num;
    }
};

参考

1. Leetcode_476. Number Complement;

2. GrandYang;

原文地址:https://www.cnblogs.com/happyamyhope/p/10589992.html