It is sum and carry addition that we do by hand.
Take two numbers a , b
c = a XOR b gives the sum with out carry for all binary digits
x = a AND b gives the carry at all the places for all binary digits

We shift all carries of binary digits in the bit string to one place right.
ie. do right shift once on x.

If x is not zero that There is till some carry forward. So we have to
continue the process.

The whole idea is to parallelly do sum and carry for all bit positions, till
the carry for all places becomes zero.


EX: a=3 and b=5 (011 , 101 in binary)

c=a^b=110
x=a&b= 001
x=x<<1=010
a=c
Next Iteration

The example that will clearly  explain it is
adding all 1's with a single 1.
Here the parallel addition reduces to the simple arithmetic that we learn in
school.

ex:
a= 1111
b= 0001



On 29 June 2010 15:21, shrinivas <shri.nit...@gmail.com> wrote:

> hi friends i m new to this group, i found very interesting and useful
> discussion here....
> this is code for adding two number without arithmetic operator
>
> int add(int a, int b)
> {
>           do
>           {
>                      a=a^b;
>                      b=(a^b)&b;
>                      b=b<<1;
>           } while(b);
>
>           return(a);
> }
>
>
> it is working well for negative numbers also . i tried lot but can not
> understand logic behind it can any one explain it in easy manner ...
> thanks in advance
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algoge...@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to