Saturday, April 24, 2010

Additon without Arithmetic Operators:

Here is the program which use bit wise operator and perform the addition operation. There is no arithmetic operator is used in this program.

int main()
{
int a,b,sum,carry;
printf("Enter the values");
scanf("%d %d",&a,&b);
sum=a^b;
carry=a&b;
while(carry!=0)
{

a=sum;
b=carry<<1;
sum=a^b;
carry=a&b;
}
printf("addition:%d",sum);
return 0;
}

It is adder circuit in c program. If you read about adder circuit,it is easy to understand.
in half adder circuit " a ex-or b" is sum and "a and b" is carry. if you get carry ,then you should add it with the sum. That's what this program doing.

0 comments:

Post a Comment