Solution: Complement of Base 10 Number

Let's solve the Complement of Base 10 Number problem using the Bitwise Manipulation pattern.

Statement#

For any nn positive number in base 10, return the complement of its binary representation as an integer in base 10.

Constraints

  • 0n1090 \leq n \leq 10^9

Solution#

So far, you’ve probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.

Naive approach#

To calculate the complement of any integer, we need to perform the following steps:

  1. Convert the integer to its binary value.

  2. Once we have the binary value, we can use a loop to incrementally convert each 11 to 00 and each 00 to 11.

  3. Now that we have the complemented binary number, we can convert it to its respective base 10 value.

The conversion of binary values from 11 to 00 and 00 to 11 can take up to logn+1\lfloor \log{n} \rfloor + 1 iterations, where logn+1\lfloor \log{n} \rfloor + 1 is the length of binary value of the base 10 number. So, the time complexity of this approach is O(logn)O(\log{n}). Since the number of bits needed to hold the binary value is logn+1\lfloor \log{n} \rfloor + 1, the space complexity of this approach is also O(logn)O(\log{n}).

Optimized approach using bitwise manipulation#

This problem is a classic example of when to use the bitwise manipulation pattern. We will use the XOR (\land) properties to get the complement of any nn number. The XOR (exclusive OR) operation is a binary operation that results in true (or 1) only when the inputs are different and false (or 0) if the inputs are the same. By taking the bitwise XOR of the binary value of the number with an all 1-bit bitmask, each of the bits in the binary value will be flipped, resulting in the complement of the number.

The following illustration demonstrates the algorithm:

canvasAnimation-image

1 of 5

canvasAnimation-image

2 of 5

canvasAnimation-image

3 of 5

canvasAnimation-image

4 of 5

canvasAnimation-image

5 of 5

The primary goal here is to flip all the bits of a number. Here’s how we’ll implement this algorithm:

  1. Calculate the number of bits required to store a certain integer. We can get this by rounding down logn\log{n} and adding 11.

  2. Create an all 1-bit bitmask against the number of bits of the input value. For example, if we need 44 bits to represent an integer, all its set bits will be 1111. The decimal value of 1111 =23+22+21+20=15= 2^3 + 2^2 + 2^1 + 2^0 = 15, which is equal to 2bits1=2412^{bits} - 1 = 2^4 - 1. So, we can use this formula to get the required number.

  3. Flip all occurrences of 11s to 00s and 00s to 11s by computing the XOR operation between the two numbers.

Let’s look at the code for this solution below:

Complement of Base 10 Number

Solution summary#

To recap, the solution to this problem can be divided into the following parts:

  1. We calculate the number of bits required to store any number.

  2. We create an all 1-bit bitmask against it and flip all occurrences of 11s to 00s and 00s to 11s by computing the XOR operation.

  3. By converting the binary value back to base 10, we got our final answer.

Time complexity#

To take the complement, we first calculate the number of bits required for the binary representation of the given integer and then take XOR, which are constant time operations. So, the time complexity of this solution is O(1)O(1).

Space complexity#

The space complexity of this solution is O(1)O(1) because at any time we have a decimal number to have it converted into its complement.

Complement of Base 10 Number

Two Single Numbers