1.
Introduction
2.
What is a 2 's Complement?
3.
1’s and 2’s Complement
4.
Binary Subtraction Using 2's Complement
4.1.
Sample Example
4.2.
Implementation
4.3.
C++
4.3.1.
Time Complexity
4.3.2.
Space Complexity
5.
Subtraction Using r's Complement:
6.
Solved Examples
7.
7.1.
Why use 2's complement subtraction?
7.2.
What range of values can be represented using 2's complement?
7.3.
Can you perform 2's complement subtraction with negative numbers?
7.4.
Is 2's complement subtraction the same as signed integer subtraction?
7.5.
What is the rule of subtraction using 2's complement?
7.6.
What is the 2's complement of 10010?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

# 2's Complement Subtraction

Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems

## Introduction

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!

Also see: C Static Function

## What is a 2 's Complement?

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:

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

3. 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:

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

3. Add 1 to the resulting number.

4. 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:

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

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

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

4. 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``````

1. 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.

• 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.

Also see, Short int in C Programming

## Subtraction Using r's Complement:

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
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.

3  ->  0000 0011
-8  ->  1111 1000
+1  ->  0000 0001
--------------------
-5  ->  1111 1011 (Result in 2's complement form)

Therefore, 3 - 8 = -5.

Example 3: Subtract 6 from -4 using 2's complement.

-4  ->  1111 1100
-6   ->  1111 1010
+1   ->  0000 0001
---------------------
-10  ->  1111 0111 (Result in 2's complement form)

Therefore, -4 - 6 = -10.

Example 4: Subtract 15 from 10 using 2's complement.

10  ->  0000 1010
-15  ->  1111 0001
+1  ->  0000 0001
---------------------
-5  ->  1111 1011 (Result in 2's complement form)

Therefore, 10 - 15 = -5.

Example 5: Subtract -8 from -12 using 2's complement.

-12  ->  1111 0100
-(-8)  ->  0000 1000 (Negate the subtrahend)
---------------------
-4  ->  1111 1100 (Result in 2's complement form)

Therefore, -12 - (-8) = -4.

### Why use 2's complement subtraction?

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:

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsJavaScriptCompetitive ProgrammingAWS and many more! If you wish to test your competency in coding, check out the mock test series and take part in the contests hosted on Coding Ninjas Studio!

Happy Learning!

Guided path
Free
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems