#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();
}