leetcode-Number-Complement

2017-08-24
leetcode

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

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

1
2
3
4
5

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.
1
2
3
4
5
6
7
class Solution {
public int findComplement(int num) {
long i;
for(i=1;i<=num;i*=2) num^=i;
return num;
}
}