Re: [algogeeks] Swapping two variables without using a temporary variable
@sunny... yeah sorry, m stupid..:) thanx On Tue, Jun 14, 2011 at 10:52 AM, sunny agrawal sunny816.i...@gmail.comwrote: @tech rascal 10 = 01010 20 = 10100 -- xor = 0 = 30 why r u getting 1 ?? On Tue, Jun 14, 2011 at 2:28 AM, Supraja Jayakumar suprajasank...@gmail.com wrote: @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 wladimir...@gmail.comwrote: 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. -- U -- 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. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- 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. -- 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.
Re: [algogeeks] Swapping two variables without using a temporary variable
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.
RE: [algogeeks] Swapping two variables without using a temporary variable
Adding two huge no will cross the integer or what's its range will fall under overflow categories same case goes to negative which fall under underflow .. In both the case numbers will change.. For example no range is -10 to 10 so if x =5 and y=7 then addition will produce the result -9 so u can see we r loosing number this is a overflow same for negative number will produce underflow Thanks Umesh Sent from my Windows Phone -- From: Supraja Jayakumar Sent: Tuesday, June 14, 2011 2:28 AM To: algogeeks@googlegroups.com Subject: Re: [algogeeks] Swapping two variables without using a temporary variable @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 wladimir...@gmail.comwrote: 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. -- U -- 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. -- 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.
Re: [algogeeks] Swapping two variables without using a temporary variable
@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 wladimir...@gmail.comwrote: 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. -- U -- 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.
Re: [algogeeks] Swapping two variables without using a temporary variable
@tech rascal 10 = 01010 20 = 10100 -- xor = 0 = 30 why r u getting 1 ?? On Tue, Jun 14, 2011 at 2:28 AM, Supraja Jayakumar suprajasank...@gmail.com wrote: @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 wladimir...@gmail.comwrote: 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. -- U -- 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. -- Sunny Aggrawal B-Tech IV year,CSI Indian Institute Of Technology,Roorkee -- 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.