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

the operator for exponencation is a.** b.^ c.% d.not available

5 Answers   TCS,


what is the difference between : func (int list[], ...) or func (int *list , ....) - what is the difference if list is an array and if also if list is a pointer

2 Answers  


What is a null pointer in c?

0 Answers  


How can you pass an array to a function by value?

0 Answers  


What is scanf_s in c?

0 Answers  






what is a non volatile key word in c language?

1 Answers  


here is a link to download Let_Us_C_-_Yashwant_Kanetkar

3 Answers   Microsoft,


Please write me a program to print the first 50 prime numbers (NOT between the range 1 -50)

5 Answers   IBM, KJH,


advantages of pointers?

3 Answers  


Why we use void main in c?

0 Answers  


Write a program that takes a 5 digit number and calculates 2 power that number and prints it(should not use big integers and exponential functions)

2 Answers   HCL, IBM, Satyam, Vimal, Vimukti Technologies,


what is the c.

3 Answers   IBM, TCS,


Categories