1. Introduction

One’s complement is a method for representing negative numbers in binary form by inverting all the bits of the number. It’s often used in networking protocols for error detection and checksum calculation. In this tutorial, we’ll learn how to calculate the one’s complement of a number in Java.

2. Using Bitwise NOT Operation

We can calculate the one’s complement of a number by performing a bitwise NOT operation on the number. The bitwise NOT operation flips all the bits in the binary representation of the number, effectively changing 0s to 1s and 1s to 0s.

Here’s an example code snippet that uses the bitwise NOT operation to calculate the one’s complement of a number:

int calculateOnesComplementUsingBitwiseNot(int num) {
    return ~num;
}

Let’s verify the results of using the bitwise NOT operation:

int onesComplement = calculateOnesComplementUsingBitwiseNot(10);

assertEquals(-11, onesComplement);
assertEquals("11111111111111111111111111110101", Integer.toBinaryString(onesComplement));

The number 10 is represented in binary as “00000000 00000000 00000000 00001010”. The bitwise NOT operator (~) inverts each bit in the binary representation. After flipping, the binary becomes “11111111 11111111 11111111 11110101“.

Java interprets this binary representation using two’s complement for signed integers. In two’s complement, the leftmost bit signifies the sign (0 for positive, 1 for negative). Since the leftmost bit is now 1, the number is interpreted as negative. Therefore the actual value is -11.

Notably, the one’s complement of a negative number is a positive number, and vice versa. This is because the bitwise NOT operation flips all the bits in the binary representation of the number, including the sign bit.

For a negative number -10, the one’s complement is 9:

int onesComplement = calculateOnesComplementUsingBitwiseNot(-10);

assertEquals(9, onesComplement);
assertEquals("1001", Integer.toBinaryString(onesComplement));

The binary representation of -10 in two’s complement form is “11111111 11111111 11111111 11110110”. When we apply the bitwise NOT operator, it flips all the bits of this binary number. Flipping the bits of “11111111 11111111 11111111 11110110” results in “00000000 00000000 00000000 00001001”. Converting this binary number to decimal gives us 9.

Therefore, when calculating the one’s complement of a number using bitwise NOT operation, it’s important to consider the sign of the number and the resulting sign of the one’s complement.

3. Using the BigInteger Class

For very large numbers, using the int data type might not be sufficient. In such cases, we can leverage the BigInteger class. The not() method of BigInteger performs a bitwise NOT operation, providing the one’s complement.

Here’s the code snippet for using BigInteger.not():

BigInteger calculateOnesComplementUsingBigIntegerNot(BigInteger num) {
    return num.not();
}

Let’s test this implementation:

BigInteger onesComplement = calculateOnesComplementUsingBigInteger(BigInteger.valueOf(10));

assertEquals(BigInteger.valueOf(-11), onesComplement);
assertEquals("11111111111111111111111111110101", Integer.toBinaryString(onesComplement.intValue()));

4. Using XOR With All 1’s

Alternatively, we can use the bitwise XOR (^) operation with a mask of all 1‘s. This method is particularly useful when we want to control the bit length explicitly:

int calculateOnesComplementUsingXOROperator(int num, int bitLength) {
    int mask = (1 << bitLength) - 1;

    return num ^ mask;
}

In this example, we first determine the bit length and mask all 1’s for that bit length using (1 << bitLength) – 1. This can be achieved by left-shifting (<<) a 1 by the number of bits required. Then we subtract 1 for positive numbers to ensure the leftmost bit (sign bit) is 0.

Next, we perform a bitwise XOR operation between the input number and the mask of 1s. This flips all the bits in the input number, resulting in the one’s complement.

For example, for the number 10 with an 8-bit representation, the mask is 11111111 (binary for 255). XORing 0000**1010 with 11111111 results in 11110101, which gives us 245:

int onesComplement = calculateOnesComplementUsingXOROperator(10, 8);

assertEquals(245, onesComplement);
assertEquals("11110101", Integer.toBinaryString(onesComplement));

When using XOR for one’s complement, it flips all bits, including the sign bit for positive numbers. In two’s complement, a flipped sign bit changes the interpretation from positive to negative. However, the value part (11110101) is indeed the one’s complement of 10.

For negative numbers, we can first convert them to their corresponding positive values in a way that ensures the bit length is maintained before applying the bitwise XOR:

int calculateOnesComplementUsingXOROperator(int num, int bitLength) {
    int mask = (1 << bitLength) - 1;

    // To handle negative value
    int extendedNum = num < 0 ? (1 << bitLength) + num : num;

    return extendedNum ^ mask;
}

Let’s check the output for one’s complement of the number -10:

int onesComplement = calculateOnesComplementUsingXOROperator(-10, 8);

assertEquals(9, onesComplement);
assertEquals("1001", Integer.toBinaryString(onesComplement));

5. Conclusion

In this article, we’ve learned about one’s complement and how to calculate it in Java using three approaches.

The bitwise NOT operator is more efficient and straightforward, allowing us to calculate the one’s complement with a single operation. Moreover, we can use the XOR operator with a mask that allows for precise control over the bit length.

As always, the source code for the examples is available over on GitHub.