#1009

Complement of Base 10 Integer

pupil · 370 · lc easy +23 · verified · 60.6% accepted · 2,596 likes · top 59%

Description

Flipping all bits in an integer's binary representation produces its complement.

- For example, 5 is "101" in binary, and its complement is "010" which equals 2.

Given an integer n, return its complement.

Example 1:

Input: n = 5
Output: 2
Explanation: 5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.

Example 2:

Input: n = 7
Output: 0
Explanation: 7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.

Example 3:

Input: n = 10
Output: 5
Explanation: 10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.

Code

1
2
3