c programming of binary addition of two binary numbers

Answers were Sorted based on User's Feedback



c programming of binary addition of two binary numbers ..

Answer / vignesh1988i

the below program is for getting two numbers as input
(decimal format) and then the below program will convert in
binary and add WITHOUT ANY ARITHMETIC OPERATORS.....



#include<stdio.h>
#include<conio.h>
#define HIGH 1
#define LOW 0
void main()
{
long c[32],i,n,a,b,k,m,A,CARRY=0;
clrscr();
n=31;
printf("enter the value of a&b:");
scanf("%ld%ld",&a,&b);
for(i=0;i<32;i++)
{
k=((a>>i)&1);
m=((b>>i)&1);
if(!(CARRY^HIGH))
{
c[n]=((CARRY^k)^m);
if(!(k^HIGH)||!(m^HIGH))
CARRY=1;
else
CARRY=0;
}
else if(!(k^HIGH) && !(m^HIGH))
{
CARRY=1;
c[n]=k^m;
}
else if(!(k^LOW)||!(m^LOW))
{
if(!(CARRY^HIGH))
{
c[n]=((CARRY^k)^m);
CARRY=0;
}
else
c[n]=k^m;
}
n--;
}
for(i=0;i<32;i++)
printf("%d",c[i]);
getch();
}


thank u

Is This Answer Correct ?    30 Yes 12 No

c programming of binary addition of two binary numbers ..

Answer / leon

#include<stdio.h>
#include<conio.h>
int main()
{
long int n1,n2,r=0,sum[50];
int n,i=0;
clrscr();
printf("\n\n Enter First Binary Number: ");
scanf("%ld",&n1);
printf("\n\n Enter Second Binary Number: ");
scanf("%ld",&n2);

while (n1!=0 || n2!=0)
{
sum[i++]=(n1%10+n2%10+r)%2;
r=(n1%10+n2%10+r)/2;
n1=n1/10;
n2=n2/10;
}
if(r!=0)
sum[i++]=r;

printf("\n\n Sum of two binary numbers: ");

for(i=i-1;i>=0;i--)
printf("%d",sum[i]);

getch();
return 0;
}

Is This Answer Correct ?    17 Yes 4 No

c programming of binary addition of two binary numbers ..

Answer / kapil singhal

#include<stdio.h>
main()
{
int n1,n2,i=0,kap=0,k=0,j,temp;
int sum[100];

printf("Enter first binary no. : ");
scanf("%d",&n1);

printf("Enter second binary no. : ");
scanf("%d",&n2);

while(n1 !=0 || n2 !=0){
sum[i]=n1%10 +n2%10 + k;
if(sum[i]>1){
sum[i]=sum[i]%2;
k=1;
}
else{
k=0;
}
n1=n1/10;
n2=n2/10;

i++;

}
if(n1==0 && n2==0){
sum[i]=k;

}
for(j=0;j<=i/2;j++){
temp=sum[j];
sum[j]=sum[i-j];
sum[i-j]=temp;
}
printf("Sum is : ");
for(j=0;j<=i;j++){
printf("%d",sum[j]);
}
}

Is This Answer Correct ?    3 Yes 0 No

c programming of binary addition of two binary numbers ..

Answer / 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

More C Interview Questions

How can you print HELLO WORLD without using "semicolon"?

7 Answers   HCL, Infosys,


what are advantages of U D F?

1 Answers   Google,


can any one tell that i have a variable which is declared as static but i want this variable to be visible to the other files? how?

2 Answers  


What is nested structure in c?

0 Answers  


what is the difference between call by value and call by reference?

5 Answers   Genpact, Global Logic, Infosys,






if the area was hit by a virus and so the decrease in the population because of death was x/3 and the migration from other places increased a population by 2x then annually it had so many ppl. find our the population in the starting.

1 Answers   TCS,


Using which language Test cases are added in .ptu file of RTRT unit testing???

0 Answers  


how to make a scientific calculater ?

0 Answers  


Why do we use null pointer?

0 Answers  


Why we not create function inside function.

0 Answers  


difference between string and array?

6 Answers  


Do string constants represent numerical values?

0 Answers  


Categories