//给你一个 正 整数 num ,输出它的补数。补数是对该数的二进制表示取反。 
//
// 
//
// 
// 
//
// 示例 1: 
//
// 
//输入:num = 5
//输出:2
//解释:5 的二进制表示为 101(没有前导零位),其补数为 010。所以你需要输出 2 。
// 
//
// 示例 2: 
//
// 
//输入:num = 1
//输出:0
//解释:1 的二进制表示为 1(没有前导零位),其补数为 0。所以你需要输出 0 。
// 
//
// 
//
// 提示: 
//
// 
// 给定的整数 num 保证在 32 位带符号整数的范围内。 
// num >= 1 
// 你可以假定二进制数不包含前导零位。 
// 本题与 1009 https://leetcode-cn.com/problems/complement-of-base-10-integer/ 相同 
// 
// Related Topics 位运算 
// 👍 202 👎 0

/*
* 476 数字的补数
* 2021-03-12 14:47:01
* @author oxygenbytes
*/ 
#include "leetcode.h" 
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
    int findComplement(int num) {
        int res = 0;
        int t = 0;
/*
 * !是逻辑运算符(与||,&&是一类符号),表示逻辑取反,可以把非0值变成0,把0值变为1
 * ~是位运算符(与|,&是一类符号),表示按位取反,在数值的二进制表示上,将0变为1,将1变为0
 */
        while(num){
            res += ( !(num & 1) ) << t;
            num >>= 1;
            t++;
        }
        return res;
    }
};
//leetcode submit region end(Prohibit modification and deletion)