Half Subtractor
Truth Table:
Circuit Diagram:
Diff = A XOR B
Borrow = A' AND B
Borrow = A' AND B
Program in C:
#include <stdio.h> typedef char bit; bit borrow=0; bit halfsub(bit A,bit B){ borrow=!A&B; return A^B; } int main() { int i,j,result; printf("A B | Diff Borrow\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { result=halfsub(i,j); printf("%d %d | ",i,j); printf("%d %d\n",result,borrow); } } return 0; }
Output:
A B | Diff Borrow
0 0 | 0 0
0 1 | 1 1
1 0 | 1 0
1 1 | 0 0
--------------------------------
Process exited after 0.1232 seconds with return value 0
Press any key to continue . . .
Program in C++:
#include <iostream> using namespace std; typedef char bit; bit halfsub(bit A,bit B,bit C){ if(C==0) return !A&B; else return A^B; } int main() { int i,j,result; int borrow; cout<<"A B | D Borrow\n"; for(i=0;i<2;i++) { for(j=0;j<2;j++) { result=halfsub(i,j,1); borrow=halfsub(i,j,0); cout<<i<<" "<<j<<" | "; cout<<result<<" "<<borrow<<"\n"; } } return 0; }
Output:
A B | S Borrow
0 0 | 0 0
0 1 | 1 0
1 0 | 1 0
1 1 | 0 1
--------------------------------
Process exited after 0.1519 seconds with return value 0
Press any key to continue . . .
Comments :
Post a Comment