On 9/16/07, sejal patel <[EMAIL PROTECTED]> wrote:
> Here is the program to add two numbers without using the '+' operator.
> #include <iostream>
> using namespace std;
>
> unsigned int add(unsigned int a, unsigned int b)
> {
> unsigned int c= 0;
> unsigned int r= 0;
> unsigned int t= ~0;
> for (t= ~0; t; t>>= 1)
> {
> r<<= 1;
> r|= (a^b^c)&1;
> c= ((a|b)&c|a&b)&1;
> a>>= 1;
> b>>= 1;
> }
> for (t= ~0, c= ~t; t; t>>= 1)
> {
> c<<= 1;
> c|= r&1;
> r>>= 1;
> }
> return c;
> }
Can you give an explanation of how this works?
-- Brett
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
If I were to divulge it, it would overturn the world."
-- Jelaleddin Rumi