program in C and C++ for full subtractor

program in C and C++ for full subtractor

Full Subtractor


Full Subtractor is a combinational logic circuit that perform the arithmetic subtraction among three bits. There are three input and two output in Full adder.

Truth Table:

Circuit Diagram:

Diff = A XOR B XOR Bin
Bout = A'.B + B. Bin + A'.Bin

Program in C:




 #include <stdio.h>
 
 typedef char bit;
   bit Bout=0;
  
bit fullsub(bit A ,bit B,bit Bin){
Bout=(!A&Bin)||(!A&B)||(B&Bin);
return (A^B)^Bin;
 }

int main()
{
printf("A   B   Bin  |  Diff  Bout\n");
 for(int i=0;i<2;i++)
 {
  for(int j=0;j<2;j++)
  {
   for(int k=0;k<2;k++)
   {
    int result=fullsub(i,j,k);
    printf("%d   %d   %d    |   ",i,j,k);
    printf("%d     %d\n",result,Bout);
      }
  
       }
     }
 return 0;
}
   

Output:



 A   B   Bin  |  Diff  Bout
 0   0   0    |   0     0
 0   0   1    |   1     1
 0   1   0    |   1     1
 0   1   1    |   0     1
 1   0   0    |   1     0
 1   0   1    |   0     0
 1   1   0    |   0     0
 1   1   1    |   1     1

--------------------------------
Process exited after 0.1636 seconds with return value 0
Press any key to continue . . .


Program in C++:




#include <iostream>
using namespace std;
 
 typedef char bit;
bit fullsub(bit A ,bit B,bit Bin,bit D){
if(D==0)    
  return (!A&Bin)||(!A&B)||(B&Bin);
else  
  return (A^B)^Bin;
 }

int main()
{
printf("A   B   Bin  |  Diff  Bout\n");
for(int i=0;i<2;i++)
{
 for(int j=0;j<2;j++)
 {
  for(int k=0;k<2;k++)
  {
  int Bout=fullsub(i,j,k,0);   
  int result=fullsub(i,j,k,1);
  cout<<i<<"   "<<j<<"   "<<k<<"    |   ";
  cout<<result<<"     "<<Bout<<"\n";
    
   }
  
  }
 }
return 0;
}

Output:



A   B   Cin  |  S  Cout
0   0   0    |  0    0
0   0   1    |  1    0
0   1   0    |  1    0
0   1   1    |  0    1
1   0   0    |  1    0
1   0   1    |  0    1
1   1   0    |  0    1
1   1   1    |  1    1

--------------------------------
Process exited after 0.1661 seconds with return value 0
Press any key to continue . . .



Algorithm:

STEP 1: Start

STEP 2: Define 'fullsub' function

STEP 3: Perform the operation (A XOR B XOR Bin) on binary numbers and store it as Bout

STEP 4: Perform the operation (A'.B + B.Bin + A'.Bin) on binary numbers and store it as diff

STEP 5: Invoke main function

STEP 6: Initialize variables

STEP 7: Check for condition (i<2), if Condition is true, go to step 8 otherwise go to step 12

STEP 8: Check for condition (j<2), if Condition is true, go to further step otherwise go to step 7

STEP 9: Check for condition (k<2), if Condition is true, go to further steps otherwise go to step 8

STEP 10: Call 'fullsum' function for value of Diff and Borrow

STEP 11: Print all values

STEP 12: Stop

Comments :

Post a Comment