@Wladimir:
Can you kindly explain the overflow and underflow you mentioned.

Thanks
Supraja J

On Fri, Jun 10, 2011 at 9:58 PM, Wladimir Tavares <[email protected]>wrote:

> Swapping two variables without using a temporary variable using the + and
> -:
>
> x = a;
> y = b;
>
> x = x + y / / x = a + b;
> y = x - y / / y = a + b-b = a;
> x = x - y / / x = a + b-a = b;
>
> y = b;
> x = a;
>
> Problems with this approach:
> 1) It can cause overflow in the operation (+)
> 2) It can cause underflow on operation (-)
>
> Swapping two variables without using variables
> Temporary using XOR:
>
> x = a;
> y = b;
>
> x = x ^ y;
> y = x ^ y / / y = (x xor y) xor y = x xor (y xor y) xor x = 0 = x
> x = x ^ y / / x = (x xor y) xor x = (x xor y) xor y xor x = (x xor x) = y
> xor y = 0
>
> Note that we use some properties of XOR:
>
> 1) Associativity
> 2) Commutativity
> 3) X = X 0 XOR
>
> We have no problems neither underflow nor overflow!
>
> Wladimir Araujo Tavares
> *Federal University of CearĂ¡
>
> *
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
U

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

Reply via email to