/* C Program For Implementation Of Binary Operations on Two Numbers
Operations Included
Binary Addition
Binary Subtraction
Binary Subtraction Is Done Making Use Of Two's Complement */
#include< stdio.h>
#include< conio.h>
#include< math.h>
int exor(int n1, int n2)
{
if(n1==n2)
return 0;
else
return 1;
}
int min(int n1, int n2)
{
if(n1< n2)
return n1;
else
return n2;
}
int max(int n1, int n2)
{
if(n1>=n2)
return n1;
else
return n2;
}
void main()
{
int a1[10],a2[10],a3[10],a4[10],a5[10],a7[10],a8[10],a11[10];
int j=0,i=0,p=0,n1=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0;
int a,b,c,h,v,o, n,q,r,x,y,S,Z,c1,c2,c3,c4,s1,carry;
int g[8],p1[10],p2,p3[10],p4[10],s[8],w[8],z[8];
clrscr();
do
{ printf("\t\tCALCULATIONS OF BINARY ARITHMETIC OPERATIONS\n");
printf("\t --------------------------------------------\n");
printf("1: BINARY ADDITION \n");
printf("2: BINARY SUBTRACTION\n");
L:
printf("\nENTER THE OPERATION YOU WOULD LIKE TO CARRY OUT:");
scanf("%d",&n);
switch(n)
{case 1:
{M:
printf("\nEnter The First Number :");
scanf("%d",&x);
if(x>0)
printf("x = %d\n",x);
else
{ printf("The Entered Number Is Invalid\n");
printf("Please Enter the valid value\n");
goto M;
}
for(i=0;i< =7;i++)
{ z[i]=0; s[i]=0; }
i=7;
do
{ z[i]=x%2;
x=x/2;
i--;
}while(x!=0);
printf("The binary equivalent of x is: ");
for(j=0;j< =7;j++)
{ printf("%d",z[j]); }
N:
printf("\n\nEnter the second number : ");
scanf("%d",&y);
if(y>=0)
printf("y = %d\n",y);
else
{ printf("The entered number is invalid\n");
printf("Please enter the valid value\n");
goto N;
}
i=7;
do
{ s[i]=y%2;
y=y/2;
i--;
}while(y!=0);
printf("The binary equivalent of y is: ");
for(p=0;p< =7;p++)
{ printf("%d",s[p]); }
printf("\n\nBinary of x: ");
for(i=0;i< =7;i++)
{printf("%d",z[i]); }
printf("\nBinary of y: ");
for(i=0;i< =7;i++)
{printf("%d",s[i]); }
for(h=7;h>=0;h--)
{s1=exor(carry,exor(z[h],s[h]));
c1=min(z[h],s[h]);
c2=min(s[h],carry);
c3=min(z[h],carry);
c4=max(c3,max(c1,c2));
carry=c4;
a1[h]=s1; }
printf("\nSum : ");
for(b=0;b< =7;b++)
{ printf("%d",a1[b]); }
for(v=0;v< =7;v++)
{ o=pow(2,7-v);
n1=n1+(a1[v]*o); }
printf("\n\nDecimal Equivalent of Sum : ");
printf("%d",n1);
break;
}
case 2:
{ printf("\nEnter the first number : ");
scanf("%d",&x); Z=x;
printf("x = %d",x);
for(i=0;i< =7;i++)
{ z[i]=0; s[i]=0; }
if(Z>0)
{i=7;
do
{ z[i]=x%2;
x=x/2;
i--;
}while(x!=0);
printf("\nThe binary equivalent of x is: ");
for(j=0;j< =7;j++)
{ printf("%d",z[j]); }
}
if(Z< 0)
{i=7;
do
{ z[i]=x%2;
x=x/2;
if(z[i]< 0)
z[i]=-z[i];
i--;
}while(x!=0);
printf("\nThe binary equivalent of x is: ");
for(j=0;j< =7;j++)
{ printf("%d",z[j]); }
for(h=0;h< =7;h++)
{ w[h]=z[h];p3[h]=1-w[h]; }
printf("\n\nThe 1'S Complement of x is: ");
for(b=0;b< =7;b++)
{ printf("%d",p3[b]); }
if(p3[7]==1)
{ p3[7]=0;carry=1;}
else
if(p3[7]==0)
{p3[7]=1;carry=0;}
for(h=6;h>=0;h--)
{ s1=exor(carry,p3[h]);
c1=min(p3[h],carry);
carry=c1;
p3[h]=s1;
}
printf("\nThe 2'S Complement of x is: ");
for(b=0;b< =7;b++)
{ printf("%d",p3[b]); }
}
printf("\n\nEnter the second number : ");
scanf("%d",&y);S=y;
printf("y = %d",y);
if(S< 0)
{p=7;
do
{ s[p]=y%2;
y=y/2;
if(s[p]< 0)
s[p]=-s[p];
p--;
}while(y!=0);
printf("\nThe binary equivalent of y is: ");
for(q=0;q< =7;q++)
{ printf("%d",s[q]); }
printf("\n________________________________________________");
}
if(S>0)
{p=7;
do
{ s[p]=y%2;
y=y/2;
p--;
}while(y!=0);
printf("\nThe binary equivalent of y is: ");
for(q=0;q< =7;q++)
{ printf("%d",s[q]); }
for(h=0;h< =7;h++)
{ w[h]=s[h];
p4[h]=1-w[h];
}
printf("\nThe 1'S Complement of y is: ");
for(b=0;b< =7;b++)
{ printf("%d",p4[b]); }
if(p4[7]==1)
{ p4[7]=0;carry=0;}
else
if(p4[7]==0)
{ p4[7]=1;carry=0; }
for(h=6;h>=0;h--)
{ s1=exor(carry,p4[h]);
c1=min(p4[h],carry);
carry=c1;
p4[h]=s1; }
printf("\nThe 2'S Complement of y is: ");
for(b=0;b< =7;b++)
{ printf("%d",p4[b]); }
printf("\n_______________________________________________");
}
if(Z< 0 && S< 0)
{ printf("\n2'S Complement of x: ");
for(h=0;h< =7;h++)
{ printf("%d",p3[h]); }
printf("\nBinary of y : ");
for(h=0;h< =7;h++)
{ printf("%d",s[h]); }
for(h=7;h>=0;h--)
{ s1=exor(carry,exor(p3[h],s[h]));
c1=min(p3[h],s[h]);
c2=min(s[h],carry);
c3=min(p3[h],carry);
c4=max(c3,max(c1,c2));
carry=c4;
a2[h]=s1;
}
if(carry==0)
{ printf("\nDifference : ");
for(y=0;y< =7;y++)
{ printf("%d",a2[y]);
a2[y]=1-a2[y]; }
printf("\nAnswer is Negative and is in 2'S Complement form");
printf("\n1'S Complement : ");
for(y=0;y< =7;y++)
{ printf("%d",a2[y]); }
if(a2[7]==1)
{ a2[7]=0; carry=1;}
else
{ a2[7]=1; carry=0; }
for(i=6;i>=0;i--)
{ c1=exor(carry,a2[i]);
c2=min(a2[i],carry);
carry=c2;
a2[i]=c1;
}
printf("\n2'S Complement : ");
for(y=0;y< =7;y++)
{
printf("%d",a2[y]);
n3=n3+(a2[y] *pow(2,7-y));
}
printf("\nDecimal Equivalent of Difference : ");
printf("%d",n3);
}
else
{
if(carry==1)
{
printf("\nDifference : ");
for(y=0;y< =7;y++)
{
printf("%d",a2[y]);
}
for(y=0;y< =7;y++)
{
n4=n4+(a2[y] *pow(2,7-y));
}
printf("\nDecimal Equivalent of Difference : ");
printf("%d",n4);
}
}
}
if(Z>0 && S>0)
{
printf("\nBinary of x : ");
for(h=0;h< =7;h++)
{
printf("%d",z[h]);
}
printf("\n2'S Complement of y : ");
for(h=0;h< =7;h++)
{
printf("%d",p4[h]);
}
for(h=7;h>=0;h--)
{
s1=exor(carry,exor(z[h],p4[h]));
c1=min(z[h],p4[h]);
c2=min(p4[h],carry);
c3=min(z[h],carry);
c4=max(c3,max(c1,c2));
carry=c4;
a5[h]=s1;
}
if(carry==1)
{
printf("\nDifference : ");
for(y=0;y< =7;y++)
{
printf("%d",a5[y]);
}
for(y=0;y< =7;y++)
{
n5=n5+(a5[y] *pow(2,7-y));
}
printf("\nDecimal Equivalent of difference : ");
printf("%d",n5);
}
else
if(carry==0)
{
printf("\nDifference : ");
for(y=0;y< =7;y++)
{
printf("%d",a5[y]);
a5[y]=1-a5[y];
}
printf("\nAnswer is Negative and is in 2'S Complement form");
printf("\n1'S Complement is : ");
for(y=0;y< =7;y++)
printf("%d",a5[y]);
if(a5[7]==1)
{
a5[7]=0; carry=1;
}
else
{
a5[7]=1; carry=0;
}
for(i=6;i>=0;i--)
{
c1=exor(carry,a5[i]);
c2=min(a5[i],carry);
carry=c2;
a5[i]=c1;
}
printf("\n2'S Complement is : ");
for(y=0;y< =7;y++)
{
printf("%d",a5[y]);
n6=n6+(a5[y] *pow(2,7-y));
}
printf("\nDecimal Equivalent of difference : ");
printf("%d",n6);
}
}
if(Z< 0 && S>0)
{
printf("\n2'S Complement of x : ");
for(h=0;h< =7;h++)
{
printf("%d",p3[h]);
}
printf("\n2'S Complement of y : ");
for(h=0;h< =7;h++)
{
printf("%d",p4[h]);
}
for(h=7;h>=0;h--)
{
s1=exor(carry,exor(p3[h],p4[h]));
c1=min(p3[h],p4[h]);
c2=min(p4[h],carry);
c3=min(p3[h],carry);
c4=max(c3,max(c1,c2));
carry=c4;
a8[h]=s1;
}
printf("\nDifference : ");
for(y=0;y< =7;y++)
{
printf("%d",a8[y]);
a8[y]=1-a8[y];
}
printf("\nAnswer is Negative and is in 2'S Complement form");
printf("\n1'S Complement is : ");
for(y=0;y< =7;y++)
printf("%d",a8[y]);
if(a8[7]==1)
{
a8[7]=0; carry=1;
}
else
{
a8[7]=1; carry=0;
}
for(i=6;i>=0;i--)
{
c1=exor(carry,a8[i]);
c2=min(a8[i],carry);
carry=c2;
a8[i]=c1;
}
printf("\n2'S Complement is : ");
for(y=0;y< =7;y++)
{
printf("%d",a8[y]);
n7=n7+(a8[y] *pow(2,7-y));
}
printf("\nDecimal Equivalent of difference : ");
printf("%d",n7);
}
if(Z>0 && S< 0)
{
printf("\nBinary of x : ");
for(h=0;h< =7;h++)
{ printf("%d",z[h]); }
printf("\nBinary of y : ");
for(h=0;h< =7;h++)
{ printf("%d",s[h]); }
for(h=7;h>=0;h--)
{
s1=exor(carry,exor(z[h],s[h]));
c1=min(z[h],s[h]);
c2=min(s[h],carry);
c3=min(z[h],carry);
c4=max(c3,max(c1,c2));
carry=c4;
a11[h]=s1;
}
printf("\nDifference : ");
for(y=0;y< =7;y++)
{ printf("%d",a11[y]); }
for(y=0;y< =7;y++)
{ n8=n8+(a11[y] *pow(2,7-y)); }
printf("\nDecimal Equivalent of difference : ");
printf("%d",n8);
}
break;
default:
clrscr();
printf("ERROR!!!!!!!\nINVALID ENTRY\n");
}
}
printf("\n-------------------------------------------------");
printf("\nWould You Like To Continue\n");
printf("Please Enter '1' For 'YES' And '0' For 'NO': ");
scanf("%d",&r);
if(r==0)
{ printf("\n************** THANK YOU ***************\n") }
}while(r==1);
getch();
}
/******************** OUTPUT **********************
CALCULATIONS OF BINARY ARITHMETIC OPERATIONS
--------------------------------------------
1: BINARY ADDITION
2: BINARY SUBTRACTION
ENTER THE OPERATION YOU WOULD LIKE TO CARRY OUT:1
Enter The First Number :5
x = 5
The binary equivalent of x is: 00000101
Enter the second number : 2
y = 2
The binary equivalent of y is: 00000010
Binary of x: 00000101
Binary of y: 00000010
Sum : 00001001
Decimal Equivalent of Sum : 9
-------------------------------------------------
Would You Like To Continue
Please Enter '1' For 'YES' And '0' For 'NO': 1
CALCULATIONS OF BINARY ARITHMETIC OPERATIONS
--------------------------------------------
1: BINARY ADDITION
2: BINARY SUBTRACTION
ENTER THE OPERATION YOU WOULD LIKE TO CARRY OUT: 2
Enter the first number : 4
x = 4
The binary equivalent of x is: 00000100
Enter the second number : 3
y = 3
The binary equivalent of y is: 00000011
The 1'S Complement of y is: 11111100
The 2'S Complement of y is: 11111101
_______________________________________________
Binary of x : 00000100
2'S Complement of y : 11111101
Difference : 00000001
Decimal Equivalent of difference : 1
-------------------------------------------------
Would You Like To Continue
Please Enter '1' For 'YES' And '0' For 'NO': 1
CALCULATIONS OF BINARY ARITHMETIC OPERATIONS
--------------------------------------------
1: BINARY ADDITION
2: BINARY SUBTRACTION
ENTER THE OPERATION YOU WOULD LIKE TO CARRY OUT:2
Enter the first number : 4
x = 4
The binary equivalent of x is: 00000100
Enter the second number : 9
y = 9
The binary equivalent of y is: 00001001
The 1'S Complement of y is: 11110110
The 2'S Complement of y is: 11110111
_______________________________________________
Binary of x : 00000100
2'S Complement of y : 11110111
Difference : 11111011
Answer is Negative and is in 2'S Complement form
1'S Complement is : 00000100
2'S Complement is : 00000101
Decimal Equivalent of difference : 5
-------------------------------------------------
Would You Like To Continue
Please Enter '1' For 'YES' And '0' For 'NO': 1
CALCULATIONS OF BINARY ARITHMETIC OPERATIONS
--------------------------------------------
1: BINARY ADDITION
2: BINARY SUBTRACTION
ENTER THE OPERATION YOU WOULD LIKE TO CARRY OUT:2
Enter the first number : 6
x = -6
The binary equivalent of x is: 00000110
The 1'S Complement of x is: 11111001
The 2'S Complement of x is: 11111010
Enter the second number : 5
y = 5
The binary equivalent of y is: 00000101
The 1'S Complement of y is: 11111010
The 2'S Complement of y is: 11111011
_______________________________________________
2'S Complement of x : 11111010
2'S Complement of y : 11111011
Difference : 11110101
Answer is Negative and is in 2'S Complement form
1'S Complement is : 00001010
2'S Complement is : 00001011
Decimal Equivalent of difference : 11
-------------------------------------------------
Would You Like To Continue
Please Enter '1' For 'YES' And '0' For 'NO': 0
************** THANK YOU ***************** */
Subscribe to:
Post Comments (Atom)
6 comments:
very nice and awesome program. i just saw it. when i run it on turbo c i got 25 errors and when i look at the errors they were all the problem of extra space in for loop but thats a syntax error and easily removed. and program runs good . awesome.
wow...awsome...pgm code..thank you...it is so use ful......
hi frnds this is sweet barathi frm ramnad...i need a c++ program using inheritance for NSS camp....pls help me...
gr8 coding bro
the assignment which would take that much time has been reduced by u :)
thanx alot
if it is possible 2 provide me coding of binary multiplication according 2 booth's algorithm
my email id is zeeshan.110@live.com
thanx
thanx buddy for reducing my work
your this effort is really outstanding
I need a 1 more favor from u that is that can u provide me the coding for multiplication of binary numbers according to booth's algorithm ???
my email id is zeeshan.110@live.com
thanx and regards,
Zeeshan Haider
Hi theгe Dear, are you гeally νisiting thiѕ web ѕite гegularly, if so afterward you wіll without dоubt gеt pleasant knoωledge.
Loоk at mу webpаge ... Sixpack ()
Post a Comment