Skip to content

Complement of Base 10 Integer

EasyBit Manipulation & Math
Decode it first. Clues, constraints, pattern and plan before any code.

The complement of a number flips every bit of its binary form (without leading zeros): 1s become 0s and 0s become 1s. For example 5 is 101, whose complement is 010, which is 2. Given n, return its complement.

n = 5   →  2    (101  → 010)
n = 7   →  0    (111  → 000)
n = 10  →  5    (1010 → 0101)
n = 0   →  1    (0    → 1)

Constraints

  • 0 <= n < 10^9