EmbLogic's Blog

Write a Program to Add two number using bitwise operators?

CODE:

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,sum,carry;
printf(“Enter Values\n”);
scanf(“%d%d”,&a,&b);
carry=a&b;
printf(“carry:%d\n”,carry);
sum=a^b;
printf(“Sum:%d\n”,sum);
while(carry)
{
carry<<=1;
printf(“carry:…%d\n”,carry);
a=sum;
printf(“a:%d\n”,a);
b=carry;
printf(“b:%d\n”,b);
sum=a^b;
printf(“sum:%d\n”,sum);
carry=a&b;
printf(“carry:…%d\n”,carry);
}
printf(“…..Sum=%d…..\n”,sum);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>