Return the base-10 complement of a non-negative integer by flipping every bit in its binary representation up to the highest set bit.
Given a non-negative integer num, compute its bitwise complement in base 10.
The complement is formed by taking the binary representation of num, flipping every bit from the most significant 1 down to the least significant bit, and converting the result back to decimal.
For example, if num = 5, its binary form is 101, so the complement is 010, which equals 2.
num = 0, treat its complement as 1.num.num.0 <= num < $2^{31}$num.num to flip only the significant bits.num.num.0 <= num < $2^{31}$Example 1
Input
num = 5
Output
2
Explanation
5 is 101 in binary. Flipping the three significant bits gives 010, which is 2.
Example 2
Input
num = 1
Output
0
Explanation
1 is 1 in binary. Flipping that bit gives 0.
Example 3
Input
num = 0
Output
1
Explanation
By convention for this problem, the complement of 0 is 1.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.