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.
The following are some basic operations performed while subtracting two binary numbers -
There are multiple methods to subtract binary numbers. Some of them are as follows -
Take two numbers like 1010 and 101.
1 0 1 0
- 1 0 1
1 0 1 0
- 1 0 1
1
1 0 1 0
- 1 0 1
01
1 0 1 0
- 1 0 1
1 0 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
We can subtract two numbers using the 1’s complement. Below steps are to be followed -
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
0 1 0 0 0 0
The result is 010000 which is 16 in decimal.
To perform binary subtraction using two’s complement, the below steps are to be followed -
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).
#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)
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**.**