paint-brush
Creating a C++ Program To Do Binary Subtractionby@sandeepmishratech
8,023 reads
8,023 reads

Creating a C++ Program To Do Binary Subtraction

by Sandeep MishraFebruary 22nd, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Binary subtraction is one of the four arithmetic operations where we perform the subtraction method of two binary numbers. The operation is much similar to arithmetic subtraction when we borrow 1 from the next higher order digit to reduce it by 1. There are multiple methods to subtract [binary numbers] using the one’s complement of the subtrahend. If the result contains a carry, add it to the least significant bit and the rest is the answer of the rest of the answer. Take two numbers 11010101(53) and 100101(37) and the result is 010000.
featured image - Creating a C++ Program To Do Binary Subtraction
Sandeep Mishra HackerNoon profile picture

Introduction

Binary numbers are those whose digits consist of 0 and 1. Binary subtraction is one of the four arithmetic operations where we perform the subtraction method of two binary numbers. The operation is much similar to arithmetic subtraction.


When we perform binary subtraction and subtract 1 from 0, we have to borrow 1 from the next higher order digit to reduce it by 1 and in this case, the remainder remains 1.

Binary Subtraction Operations

The following are some basic operations performed while subtracting two binary numbers -


  • 0 – 0 = 0
  • 1 – 0 = 1
  • 1 – 1 = 0
  • 0 – 1 = 1 (Borrow 1 from next high order digit)

Methods to Subtract Binary Numbers

There are multiple methods to subtract binary numbers. Some of them are as follows -

A. Normal subtraction by borrowing from higher digit


Take two numbers like 1010 and 101.

 1 0 1 0 

-   1 0 1



  1. Starting from the last digit, we subtract (0 - 1). We need to borrow 1 from the other column so after borrowing 1 it becomes (10 - 1) which results in 1.


 1 0 1 0 

-   1 0 1


    1


  1. In the 10’s place, we are left with (0 - 0) which results in 0.


 1 0 1 0 

-   1 0 1


01


  1. In the 100's place, we borrow 1 from the 1000’s place. After performing the operation (10-1), we get 1 in 1000’s place.


 1 0 1 0 

-   1 0 1


1  0  1


  1. In the 1000’s place we are only left with 0 so the output of this substaction comes out to 0101. We can cross-check it as 1010 in decimal is 10 and 101 is 5.  So, 10 - 5 = 5 ( 0101 in binary)


    1 0 1 0

-   1 0 1


0  1  0  1


B. Binary subtraction using 1’s complement

We can subtract two numbers using the 1’s complement. Below steps are to be followed -

  • Write the one’s complement of the subtrahend.
  • Add this complement to the minuend.
  • If the result contains a carry, add it to the least significant bit.
  • If there is no carry, take the 1’s complement of the result and its negative.


Take two numbers 110101(53) and 100101(37).

1 1 0 1 0 1

- 1 0 0 1 0 1



Here 37 is the subtrahend. The one’s complement of 37 is - 011010

Adding it with the minuend.

1 1 0 1 0 1

+ 0 1 1 0 1 0


10 0 1 1 1 1


Here 1 is the carry. Now we add this 1 to the result.

1 0 0 1 1 1 1

  • 1

0 1 0 0 0 0


The result is 010000 which is 16 in decimal.


C. Binary subtraction using two’s complement

To perform binary subtraction using two’s complement, the below steps are to be followed -

  • Find the two’s complement of the subtrahend.
  • Add these two’s complement with the minuend.
  • Discard the carry bit if any and the rest is the answer.
  • In case of no carry, take two’s complement of the result which will be negative.


Take two numbers 10101(21) and 00111(7). Calculating the two’s complement of the subtrahend.

0 0 1 1 1 = 1 1 0 0 1(In 2’s complement)

Adding it to the minuend,

1 0 1 0 1 

+ 1 1 0 0 1


1  0 1 1 1 0
We get to carry 1 so we discard it and the final answer comes to 01110(14).

C++ Program to Perform Binary Subtraction


#include<iostream>
#include<bits/stdc++.h>
using namespace std;

// Program to subtract two binary numbers 
void Subtract(int n, int a[], int b[])
{
	
	// 1's Complement of the subtrahend 
	for(int i = 0; i < n; i++)
	{
		
		//Replace 1 by 0
		if(b[i] == 1)
		{
			b[i] = 0;
		}
	
		//Replace 0 by 1
		else
		{
			b[i] = 1;
		}
	}

	//Add 1 at end to get 2's Complement of the subtrahend
	for(int i = n - 1; i >= 0; i--)
	{					
		if(b[i] == 0)
		{
			b[i] = 1;
			break;
		}
		else
		{
			b[i] = 0;
		}
	}

	// Represents carry
	int t = 0;						
	for(int i = n - 1; i >= 0; i--)
	{
		
		// Add a, b and carry
		a[i] = a[i] + b[i] + t;
	
		// If a[i] is 2
		if(a[i] == 2)
		{
			a[i] = 0;
			t = 1;

		}
	
		// If a[i] is 3
		else if(a[i] == 3)
		{
			a[i] = 1;
			t = 1;
		}
		else
			t = 0;
	}

	cout << endl;
	
	// If carry is generated
	// discard the carry
	if(t==1)
	{	
	
	// print the result
	for(int i = 0; i < n; i++)
	{
			
		// Print the result
		cout<<a[i];	
	}
	}

	// if carry is not generated
	else				
	{				
		
		// Calculate 2's Complement
		// of the obtained result
		for(int i = 0; i < n; i++)
		{				
			if(a[i] == 1)
				a[i] = 0;
			else
				a[i] = 1;
		}
		for(int i = n - 1; i >= 0; i--)
		{
			if(a[i] == 0)
			{
				a[i] = 1;
				break;
			}
		else
			a[i] = 0;
		}
	
		// Add -ve sign to represent
		cout << "-";		
	
		// -ve result
		// Print the resultant array
		for(int i = 0; i < n; i++)
		{
			cout << a[i];
		}
	}
}


int main() // Main function 
{
	int n;
	n = 5;		
	int a[] = {1, 0, 1, 0, 1},
		b[] = {0, 0, 1, 1, 1};
	
	Subtract(n,a,b);
	return 0;
}


Output:

01110


Time complexity - O(nlogn)

Space complexity - O(1)


Conclusion

We can subtract two binary numbers using three methods - simple subtraction, using 1’s complement, and using 2’s complement.


In simple subtraction, we borrow a bit from the next higher significant bit.


In 1’s complement, we perform the 1’s complement of the subtrahend and add it to the minuend. If there is a carry add it to the last bit else take 1’s complement of the result.


In the case of 2’s complement subtraction, take the two’s complement of the subtrahend and add it to the minuend. If there is a carry discard it, else take two’s complement of the result which will result in a negative answer**.**