Problem 673
Number Complement
Easy · 10 pts · Bit Manipulation
Problem
The complement of a positive integer is obtained by flipping every bit of its binary representation, written without leading zeros: each 0 becomes 1 and each 1 becomes 0. For example, 5 is 101 in binary and its complement is 010, which is 2. Given a positive integer, print its complement. Build a mask of ones as long as the number's binary form and XOR the number with it.
Input
A single integer X.
Output
Print the complement of X.
Constraints
1 <= X < 2^31
Example input
5
Example output
2
Explanation
5 is 101 in binary. Flipping each bit gives 010, which is 2 in decimal, so the answer is 2.