476. Number Complement
- Use XOR 11111… to flip bit.
- Use Mask to decide which bits are 1s.
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.
Example 1:
1 | Input: 5 |
Example 2:
1 | Input: 1 |
Soulution:
1 | class Solution: |
1 | class Solution: |