## 476. Number Complement 原

初雪之音

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
2. You could assume no leading zero bit in the integer’s binary representation.

Example 1:

```Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.```

Example 2:

```Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.```

Code：

``````public class Solution {
public int findComplement(int num) {
int result = 0;

if (num > 0) {
int temp = ~0;
while ((temp & num) != 0) {
temp <<= 1;
}
result = ~temp & ~num;
}

return result;
}
}``````

### 初雪之音

Leetcode 476. Number Complement

SnailTyan
09/12
0
0
Calcc

Calcc 是一个基于命令行的32/64位数值计算工具。支持十六进制、十进制、八进制、二进制、BASE4、字符串、IPv4/6等输入输出格式。能进行的操作包括：complement, not, shift, rotate, xor, an...

2008/10/30
286
1

2017/08/22
0
0

2017/08/22
0
0
MIT Data Science Machine Becomes As Intuitive As Humans: Rise Of The Machines?

Researchers at MIT have developed a new artificial intelligence system that is able to outperform even the smartest human intuition in a number of different areas. The new big d......

Betty__
2016/10/12
2
0

OSChina 周六乱弹 —— 你一口我一口多咬一口是小狗

Osc乱弹歌单（2018）请戳（这里） 【今日歌曲】 @达尔文 ：分享Roy Orbison的单曲《She's a Mystery to Me》 《She's a Mystery to Me》- Roy Orbison 手机党少年们想听歌，请使劲儿戳（这里...

141
4
Spring源码学习笔记-1-Resource

zypy333

13
0
RestClientUtil和ConfigRestClientUtil区别说明

RestClientUtil directly executes the DSL defined in the code. ConfigRestClientUtil gets the DSL defined in the configuration file by the DSL name and executes it. RestClientUtil......

bboss

21
0

4
0
Linux系统设置全局的默认网络代理

14
0