Number Complement

Given a positive integer num, return its complement, where every bit in the binary representation is flipped and leading zeros are ignored. Pattern focus: Check kth Bit. Build an all-ones mask up to the highest set bit, then flip only the relevant positions.

Input Format

num = positive integer

Output Format

bitwise complement of num

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • num must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

num = 5

Output:

2

Explanation:

5 is 101 in binary, and its complement is 010, which is 2.

Example 2:

Input:

num = 1

Output:

0

Explanation:

1 becomes 0 after flipping its single bit.

Loading...
Number Complement - Bit Manipulation DSA Problem