The complement of an integer is the integer you get by flipping every 0 to 1 and every 1 to 0 in its binary representation, without leading zeros. For example, 5 is "101" in binary and its complement is "010", which is 2. Given a positive integer num, return its complement.
The complement of an integer is the integer you get by flipping every 0 to 1 and every 1 to 0 in its binary representation, without leading zeros. For example, 5 is "101" in binary and its complement is "010", which is 2. Given a positive integer num, return its complement.
Example 1:
Input: {"num":5}
Output: 2
Constraints:
1≤num<231
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"num":5}
num = 5 is 101 in binary (3 bits). Flipping must stop at its highest 1: the leading zeros are not part of the number.