Have you tested your claim regarding overflow/underflow and proved it
is indeed a problem, or you just thought that it won't work?  Using
two's complement, it should still work.

On Friday, June 10, 2011, Wladimir Tavares <wladimir...@gmail.com> 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 =

-- 
Fear of the LORD is the beginning of knowledge (Proverbs 1:7)

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@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