The 2's complement is a system used to represent negative numbers in binary form. The need for 2's complement arises from the fact that computers operate with binary digits, which can only represent non-negative values. To represent negative numbers in binary form, a method is needed to represent their negative sign and magnitude.

This article will discuss 2's complement subtraction in detail. We will start our discussion with an intro of 1's and 2's complement. Afterwards, we will see how to do 2's complement subtraction, along with an example. At last, we will see a C++ implementation to do 2's complement subtraction. So without further ado, letâ€™s get started!

The 2's complement is a way of representing signed integers in binary form. It is generally used in computer systems and digital arithmetic operations. In the 2's complement representation, the MSB (most significant bit) is used to indicate the sign of the number. 0 represents a positive number, and 1 illustrates a negative number.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

1â€™s and 2â€™s Complement

Taking the 1s and 2s complement of a binary number involves flipping the bits of the original number in a specific way.

To take the 1s complement of a binary number:

Start with the binary number.

Flip (or invert) each bit of the binary number so that 0 becomes 1 and 1 becomes 0.

The resulting number is the 1s complement of the original binary number.

For example, the 1s complement of the binary number 0101 1010 is 1010 0101.

To take the 2s complement of a binary number:

Start with the binary number.

Flip (or invert) each bit of the binary number, as with the 1s complement.

Add 1 to the resulting number.

The resulting number is the 2s complement of the original binary number.

For example, the 2s complement of the binary number 0101 1010 is 1010 0110.

Note that the 2s complement is used to represent negative numbers in two's complement arithmetic. You can refer to article 2â€™s complement for further reading.

Binary Subtraction Using 2's Complement

Subtracting binary numbers using 2's complement involves converting the subtrahend (the number being subtracted) into its 2's complement form. Then add it to the minuend (the number from which the subtrahend is being subtracted). If there is a carry-out of the most significant bit, it indicates that the result is negative.

Here are the steps to subtract binary numbers using 2's complement:

Convert the subtrahend into its 2's complement form by inverting all the bits and adding 1.

Add the minuend to the 2's complement form of the subtrahend, ignoring any carry from the previous addition.

If there is a carry-out of the most significant bit (leftmost bit), discard it. This indicates that the result is negative.

The resulting binary number is the difference between the minuend and subtrahend in binary form.

Sample Example

Here's an example of subtracting two binary numbers using 2's complement:

Minuend: 1101
Subtrahend: 1010

Convert the subtrahend into its 2's complement form by inverting all the bits and adding 1:

1010 -> 0101 + 1 = 0110

2. Add the minuend to the 2's complement form of the subtrahend:

3. Ignore any carry from the previous addition:

0011

4. The resulting binary number is the difference between the minuend and subtrahend in binary form:

0011 = 3 in decimal

Therefore, 1101 - 1010 = 3 in decimal using 2's complement subtraction.

Implementation

C++

C++

#include <iostream> #include <bitset>

using namespace std;

int main() { // Define the minuend and subtrahend as binary strings string minuend_str = "1101"; string subtrahend_str = "1010";

// Convert the binary strings to unsigned integers unsigned int minuend = bitset < 4 > (minuend_str).to_ulong(); unsigned int subtrahend = bitset < 4 > (subtrahend_str).to_ulong();

// Convert the subtrahend to its 2's complement form by inverting all the bits and adding 1 subtrahend = ~subtrahend + 1;

// Subtract the subtrahend from the minuend using unsigned integer subtraction unsigned int result = minuend + subtrahend;

// Convert the result back to a binary string string result_str = bitset < 5 > (result).to_string();

// Print the result cout << "The difference between " << minuend_str << " and " << subtrahend_str << " is " << result_str << endl;

return 0; }

Output:

The difference between 1101 and 1010 is 00011

Explanation:

This implementation first converts the minuend and subtrahend from binary strings to unsigned integers. It then converts the subtrahend to its 2's complement form. It is done by inverting all the bits and adding 1. Finally, subtract the subtrahend from the minuend using unsigned integer subtraction. At last, the result is converted to a binary string.

Time Complexity

The time complexity of the implementation of 2's complement subtraction is O(n). Here n is the number of bits in the binary numbers.

Space Complexity

The space complexity of the implementation of 2's complement subtraction is also O(n). As the binary numbers are stored as strings and converted to unsigned integers.

To find M-N in base r, we add M + râ€™s complement of N Result is M + (rn â€“ N)

1) If M > N then result is M â€“ N + rn (rn is an end carry and can be neglected. 2) If M < N then result is rn â€“(N-M) which is the râ€™s complement of the result.

Example: Subtract (76425 â€“ 28321) using 10â€™s complements.

10â€™s complement of 28321 is 71679

Then add - 7 6 4 2 5 + 7 1 6 7 9

---------------

1 4 8 1 0 4

Therefore the difference is 48104 after discarding the end carry.

Example: subtract (28531 â€“ 345920) Answer - It is obvious that the difference is negative. We also have to work with the same number of digits, when dealing with complements.

10â€™s complement of 345920 is 654080 Then add - 0 2 8 5 3 1 + 6 5 4 0 8 0

---------------- 6 8 2 6 1 1

Therefore the difference is negative and is equal to the 10â€™s complement of the answer. Difference is - 317389

Solved Examples

Example 1: Subtract 7 from 12 using 2's complement.

12 -> 0000 1100 -7 -> 1111 1001 +1 -> 0000 0001 (Add 1 to the least significant bit of the subtrahend) --------------------- 5 -> 0000 0101 (Result in 2's complement form)

Therefore, 12 - 7 = 5.

Example 2: Subtract 8 from 3 using 2's complement.

2's complement subtraction allows us to use the same hardware for addition and subtraction operations in a computer's arithmetic logic unit (ALU). It also simplifies the process of performing subtraction in software, as it eliminates the need for a separate subtraction circuit.

What range of values can be represented using 2's complement?

The range of values that can be represented using n bits in 2's complement form is -2^(n-1) to 2^(n-1)-1. For example, with 8 bits, the range is -128 to 127.

Can you perform 2's complement subtraction with negative numbers?

Yes, you can perform 2's complement subtraction with negative numbers by representing the negative numbers in 2's complement form. For example, to subtract -3 from -5, you can first represent -3 as 11111101 and -5 as 11111011 and then perform 2's complement subtraction as usual.

Is 2's complement subtraction the same as signed integer subtraction?

Yes, 2's complement subtraction is the same as signed integer subtraction. In most programming languages, signed integers are represented in 2's complement form, so performing signed integer subtraction is equivalent to performing 2's complement subtraction.

What is the rule of subtraction using 2's complement?

To subtract in 2's complement, negate the subtrahend and add it to the minuend, including a carry if necessary, to obtain the result in 2's complement form.

What is the 2's complement of 10010?

To find the 2's complement of 10010, invert all bits to get 01101, then add 1 to get 01110. Therefore, the 2's complement of 10010 is 01110.

Conclusion

This article briefly discussed the topic of the 2's complement subtraction. We started our discussion with 1's and 2's complement. Afterwards, we discussed how to do 2's complement subtraction along with a c++ implementation. We hope that this article helped you understand the 2's complement subtraction topic.

For further reading, refer to our articles on similar topics: