can u pls explain the overflow and underflow in +- approach On Jun 11, 8:58 am, 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 = (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 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.