Arithmetic and Logic unit
This program is made for 8-bit Arithmetic and Logic unit in C. The logic to compute these operation is given below:
This program seems long but it is very simple first read it at least once.
Arithmetic Operation:
The program focuses on four arithmetic activities.
(Ⅰ) Addition:
For addtion simply we use (A+B).
(Ⅱ) Subtraction:
A - B = A + (complement of B) + 1
A - B = A + (complement of B) + 00000001 (used in program for 8-bit binary number)
(Ⅲ) Increment
A + 1 = A + = A + 00000001 (used in program for 8-bit binary number)
(Ⅳ) Decrement
A - 1 = A + (complement of 1) + 1
A - 1 = A + 11111110 + 00000001
A - 1 = A + 11111111(used in program for 8-bit binary number)
Logical Operation:
The program focuses on four Logical activities.
(Ⅰ) OR:
A OR B = (A + B)
A OR B = A|B (used in program)
(Ⅱ) AND:
A AND B = A.B
A AND B = A&B (used in program for)
(Ⅲ) NOT:
NOT A = ~A(used in program)
(Ⅳ) XOR:
A XOR B = A ^ B(used in program)
#include <stdio.h> #include <conio.h> #include <iostream> using namespace std; typedef char bit; long u1=00000001,u2=11111111; long bn1,bn2,ans; int res[100], rem=0,ch; int res1[8],res2[8]; bit notop(bit A) { return ~A; } bit xorop(bit A,bit B) { return A^B; } bit orop(bit A ,bit B) { return A|B; } bit andop(bit A, bit B) { return A&B; } int arth() { int i=0,k=0; printf("1. Addition\n2. Subtraction\n"); printf("3. Increment\n4. decrement\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter two binary numbers: "); scanf("%ld%ld",&bn1,&bn2); while(bn1!=0||bn2!=0) { res[i]=(bn1 %10 + bn2 %10+rem)%2; rem=(bn1 %10 + bn2 %10 + rem)/2; bn1=bn1/10; bn2=bn2/10; i++; } --i; printf("Sum of two binary numbers: "); while(i>=0) { printf("%d",res[i--]); } printf("\ncarry= %d",rem); break; case 3: printf("Enter a binary number"); scanf("%ld",&bn1); rem=0,i=0; while(bn1!=0||u1!=0) { res[i]=(bn1 %10 + u1 %10+rem)%2; rem=(bn1 %10 + u1 %10 + rem)/2; bn1=bn1/10; u1=u1/10; i++; } --i; printf("binary1+1= "); while(i>=0) { printf("%d",res[i--]); } break; case 4: printf("Enter a binary number"); scanf("%ld",&bn1); rem=0,i=0; while(bn1!=0||u2!=0) { res[i]=(bn1 %10 + u2 %10+rem)%2; rem=(bn1 %10 + u2 %10 + rem)/2; bn1=bn1/10; u2=u2/10; i++; } --i; printf("binary1-1= "); while(i>=0) { printf("%d",res[i--]); } break; case 2: k=7,i=7; printf("Enter two binary numbers: "); scanf("%ld%ld",&bn1,&bn2); while(k>=0) { res1[k]=not(bn2%10); bn2=bn2/10; k--; } k=7; while(k>=0||i>=0) { res2[i]=(bn1 %10 + res1[k]+rem)%2; rem=(bn1 %10 + res1[k] + rem)/2; bn1=bn1/10; k--; i--; } int j=7; i=7,rem=0; while(j>=0||i>=0) { res1[i]=(res2[j] + u1 %10+rem)%2; rem=(res2[j] + u1 %10 + rem)/2; u1=u1/10; j--; i--; } i=0; printf("binary1 - binary2= "); while(i<=7) { printf("%d",res1[i++]); } break; } return 0; } int logic() { int ch,i=0; printf("1. OR\n2. AND\n"); printf("3. NOT\n4. XOR\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("Enter two binary numbers: "); scanf("%ld%ld",&bn1,&bn2); while(bn1!=0|| bn2!=0) { res[i]= orop(bn1%10,bn2%10); bn1=bn1/10; bn2=bn2/10; i++; } --i; printf("bn1 OR bn2 = "); while(i>=0) { printf("%d",res[i--]); } break; case 2: printf("Enter two binary numbers: "); scanf("%ld%ld",&bn1,&bn2); while(bn1!=0|| bn2!=0) { res[i]= andop(bn1%10,bn2%10); bn1=bn1/10; bn2=bn2/10; i++; } --i; printf("bn1 AND bn2 = "); while(i>=0) { printf("%d",res[i--]); } break; case 3: printf("Enter a binary number: "); scanf("%ld",&bn1); while(i<=7) { res[i]=not(bn1%10); bn1=bn1/10; i++; } --i; printf("NOT bn1= "); while(i>=0) { printf("%d",res[i--]); } break; case 4: printf("Enter two binary numbers: "); scanf("%ld%ld",&bn1,&bn2); while(bn1!=0|| bn2!=0) { res[i]= xorop(bn1%10,bn2%10); bn1=bn1/10; bn2=bn2/10; i++; } --i; printf("bn1 XOR bn2 = "); while(i>=0) { printf("%d",res[i--]); } break; default : printf("wrong input"); break; } return 0; } int main() { int ch; char b; printf("1. Arithmetic Operation\n"); printf("2. Logical operation\n"); printf("Enter your choice: "); scanf("%d",&ch); switch(ch) { case 1: arth(); break; case 2: logic(); break; default: printf("wrong input"); break; } printf("\n"); printf("Do you want to continue(Y/N):"); cin>>b; if(b=='Y'||b=='y') { main(); } else{ return 0; } }
Output:
1. Arithmetic Operation 2. Logical operation Enter your choice: 1 1. Addition 2. Subtraction 3. Increment 4. decrement Enter your choice: 1 Enter two binary numbers: 11001010 10001001 Sum of two binary numbers: 01010011 carry= 1 Do you want to continue(Y/N): y 1. Arithmetic Operation 2. Logical operation Enter your choice: 1 1. Addition 2. Subtraction 3. Increment 4. decrement Enter your choice: 3 Enter a binary number10111000 binary1+1= 10111001 Do you want to continue(Y/N): y 1. Arithmetic Operation 2. Logical operation Enter your choice: 2 1. OR 2. AND 3. NOT 4. XOR Enter your choice: 1 Enter two binary numbers: 1 1110000 00001111 bn1 OR bn2 = 11111111 Do you want to continue(Y/N): y 1. Arithmetic Operation 2. Logical operation Enter your choice: 2 1. OR 2. AND 3. NOT 4. XOR Enter your choice: 3 Enter a binary number: 11110000 NOT bn1= 00001111 Do you want to continue(Y/N): n -------------------------------- Process exited after 271 seconds with return value 0 Press any key to continue . . .
very nice sir
ReplyDelete