 New update is available. Click here to update.
Last Updated: May 30, 2023
Easy

# 2's Complement Subtraction Author Alankrit Srivastava 0 upvotes

## 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, Tribonacci Series

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

## Subtracting Binary Numbers 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.

## Implementation

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

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

## 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 CodeStudio 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 CodeStudio!

Happy Learning!

Codekaze-June23 India's Biggest Tech Hiring Challenge is LIVE! Go on top