Number Complement Problem in Java

The Number Complement problem is an interesting challenge that involves binary operations and bit manipulation. In this section, we will explore this problem in detail, delve into the theory behind it, and provide a comprehensive solution in Java. This problem is commonly found in coding interviews and is an excellent way to test your understanding of binary numbers and bitwise operations.

Problem Statement

Given a positive integer, the task is to find its complement. The complement of an integer is defined as flipping all the bits in its binary representation (i.e., converting 0s to 1s and 1s to 0s).

For example:

  • The binary representation of 5 is 101. The complement of 101 is 010, which is 2 in decimal.
  • The binary representation of 1 is 1. The complement of 1 is 0, which is 0 in decimal.

Approach to Solve the Problem

To solve the Number Complement problem, follow these steps:

  • Convert the given number to its binary representation.
  • Flip all the bits in the binary representation.
  • Convert the resulting binary number back to its decimal form.

Step-by-Step Breakdown

Let's break down the steps further using an example.

Example:

Given number: 5

1. Convert to Binary:

2. Flip the Bits:

3. Convert Back to Decimal:

Implementation in Java

Now that we understand the problem and the steps to solve it, let's implement the solution in Java.

Step 1: Convert to Binary

We can use the built-in Integer.toBinaryString() method to convert an integer to its binary string representation.

Step 2: Flip the Bits

To flip the bits, we will iterate through the binary string and replace 0s with 1s and 1s with 0s.

Step 3: Convert Back to Decimal

Finally, we will use Integer.parseInt(binaryString, 2) to convert the flipped binary string back to a decimal integer.

Here is the complete Java code for the solution.

File Name: NumberComplement.java

Output:

 
The complement of 5 is 2   

Explanation of the Code

1. Convert the Number to Binary:

The above syntax converts the given number to its binary string representation.

2. Flip the Bits:

Here, we iterate through each character in the binary string. If the character is 0, we append 1 to the StringBuilder. If the character is 1, we append 0.

3. Convert Back to Decimal:

Finally, we convert the flipped binary string back to a decimal integer using Integer.parseInt() with a radix of 2.

Optimized Solution Using Bit Manipulation

While the above solution works perfectly, it can be optimized further using bit manipulation. Instead of converting to a binary string and flipping bits manually, we can use bitwise operations.

Her is the optimized solution:

  • Find the highest bit set in the given number.
  • Create a bitmask that has all bits set to 1 up to the highest bit of the given number.
  • XOR the given number with the bitmask to get the complement.

Example:

Given number: 5 (binary: 101)

  • Highest bit set is the third bit.
  • Bitmask with all bits set up to the highest bit: 111 (binary)
  • XOR 101 with 111 results in 010 (binary), which is 2 in decimal.

Here is the optimized Java code.

File Name: NumberComplement.java

Output:

 
The complement of 5 is 2   

Explanation of the Optimized Code

Find the Bit Length:

The above line of code calculates the number of bits required to represent the given number in binary.

Create a Bitmask:

This line creates a bitmask with all bits set to 1 up to the bit length of the given number. The expression (1 << bitLength) shifts 1 to the left by bitLength positions, creating a number with a single 1 followed by bitLength zeros. Subtracting 1 from this number gives us a bitmask with all bits set to 1 up to the highest bit.

XOR with Bitmask:

Finally, we use the XOR operation to flip the bits of the given number by XORing it with the bitmask.

Conclusion

The Number Complement problem is a great way to practice working with binary numbers and bitwise operations. We explored two solutions: a straightforward approach using string manipulation and an optimized solution using bit manipulation. Both solutions are valid, but the optimized solution is more efficient and demonstrates the power of bitwise operations in solving such problems.

By understanding and implementing these solutions, we can improve your problem-solving skills and be better prepared for coding interviews that involve binary operations and bit manipulation.