c programming of binary addition of two binary numbers
Answer Posted / pramod yadav
#include<iostream.h>
#include<conio.h>
#define HIGH 1
#define LOW 0
class binary
{
public:
long c[8];
long CARRY;
binary()
{
CARRY=0;
for(int i=0;i<8;i++)
c[i]=0;
}
};
int main()
{
binary bin;
long i,n,a,b,k,m,A;
clrscr();
n=7;
cout<<"\nEnter the value of a&b in Decimal :";
cin>>a>>b;
for(i=0;i<8;i++)
{
k=((a>>i)&1);
m=((b>>i)&1);
if(!(bin.CARRY^HIGH))
{
bin.c[n]=((bin.CARRY^k)^m);
if(!(k^HIGH)||!(m^HIGH))
bin.CARRY=1;
else
bin.CARRY=0;
}
else if(!(k^HIGH) && !(m^HIGH))
{
bin.CARRY=1;
bin.c[n]=k^m;
}
else if(!(k^LOW)||!(m^LOW))
{
if(!(bin.CARRY^HIGH))
{
bin.c[n]=((bin.CARRY^k)^m);
bin.CARRY=0;
}
else
bin.c[n]=k^m;
}
n--;
}
cout<<"Addition of Two Binary No. is";
for(i=0;i<8;i++)
cout<<bin.c[i];
getch();
return 0;
}
| Is This Answer Correct ? | 7 Yes | 14 No |
Post New Answer View All Answers
When should a type cast not be used?
Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.]
How do we declare variables in c?
how to write a c program to print list of fruits in alpabetical order?
If I have a char * variable pointing to the name of a function ..
How can you tell whether two strings are the same?
List the difference between a While & Do While loops?
What are the types of pointers?
If you know then define #pragma?
Why ca not I do something like this?
What is the condition that is applied with ?: Operator?
What is s in c?
Explain what header files do I need in order to define the standard library functions I use?
Why should I use standard library functions instead of writing my own?
What do you mean by keywords in c?