Search topics...

Number Complement

easy
bitsTime: O(log n)Space: O(1)Frequency: 4

Given a positive integer, return its complement by flipping all bits in its binary representation.

Example 1:

Input: num = 5
Output: 2
Explanation: 5 in binary is 101, its complement is 010 = 2.

Example 2:

Input: num = 1
Output: 0
Explanation: 1 in binary is 1, its complement is 0.
bit-manipulation