Solution: Complement of Base 10 Number
Let's solve the Complement of Base 10 Number problem using the Bitwise Manipulation pattern.
Statement#
For any positive number in base 10, return the complement of its binary representation as an integer in base 10.
Constraints
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:
-
Convert the integer to its binary value.
-
Once we have the binary value, we can use a loop to incrementally convert each to and each to .
-
Now that we have the complemented binary number, we can convert it to its respective base 10 value.
The conversion of binary values from to and to can take up to iterations, where is the length of binary value of the base 10 number. So, the time complexity of this approach is . Since the number of bits needed to hold the binary value is , the space complexity of this approach is also .
Optimized approach using bitwise manipulation#
This problem is a classic example of when to use the bitwise manipulation pattern. We will use the XOR () properties to get the complement of any 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:
1 of 5
2 of 5
3 of 5
4 of 5
5 of 5
The primary goal here is to flip all the bits of a number. Here’s how we’ll implement this algorithm:
-
Calculate the number of bits required to store a certain integer. We can get this by rounding down and adding .
-
Create an all 1-bit bitmask against the number of bits of the input value. For example, if we need bits to represent an integer, all its set bits will be
1111. The decimal value of1111, which is equal to . So, we can use this formula to get the required number. -
Flip all occurrences of s to s and s to s by computing the XOR operation between the two numbers.
Let’s look at the code for this solution below:
Solution summary#
To recap, the solution to this problem can be divided into the following parts:
-
We calculate the number of bits required to store any number.
-
We create an all 1-bit bitmask against it and flip all occurrences of s to s and s to s by computing the XOR operation.
-
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 .
Space complexity#
The space complexity of this solution is because at any time we have a decimal number to have it converted into its complement.
Complement of Base 10 Number
Two Single Numbers