# 476.Number Complement

**476.Number Complements**

难度:Easy

> 给定一个正整数，输出它的补数。补数是对该数的二进制表示取反。

注意: 给定的整数保证在32位带符号整数的范围内。 你可以假定二进制数不包含前导零位。

```
示例 1:

输入: 5
输出: 2
解释: 5的二进制表示为101（没有前导零位），其补数为010。所以你需要输出2。
示例 2:

输入: 1
输出: 0
解释: 1的二进制表示为1（没有前导零位），其补数为0。所以你需要输出0。
```

直接位运算，代码如下。

```
class Solution {
public:
    int findComplement(int num) {
        int t=num;
        int flag=0;
        int res=0;
        while(t)
        {
             res |= ((t&1^1)<<(flag++));
           // cout<<res<<endl;
            t=t>>1;
           // cout<<t<<endl;
            
        }
        return res;
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/476.number_complement.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
