454,507 Members | 1,758 Online
Need help? Post your question and get tips & solutions from a community of 454,507 IT Pros & Developers. It's quick & easy.

the difference among these ways of swapping two ints

 P: n/a I knew some ways of swapping two ints without using a temporary variable. Just like: // Method 1: void swap1(int &a, int &b) { int temp = a; a = b; b = a; } // Method 2: void swap2(int &a, int &b) { a = a + b; b = a - b; a = a - b; } // Method 3: void swap(int &a, int &b) { a = a ^ b; b = a ^ b; a = a ^ b; } But I don't know the difference among these ways. The third method is read from a book, and the author said that would be some cases where this method can't be used. I don't why, Would you please help me solve this puzzle? Thank you! Gerald Nov 22 '05 #1
9 Replies

 P: n/a Method 1 uses a temprorary variable. Method 2 is fairly simple. Method 3 uses bitwise xor operator. If initially a is 2 and b is 3. a - 0010 b - 0011 ------------ a - 0001 ------------ a - 0001 b - 0011 ------------ b - 0010 ------------ a - 0001 b - 0010 ------------ a - 0011 ------------ So in the end a = 3 and b is 2. Regards Technator Nov 22 '05 #2

 P: n/a st*********@gmail.com skrev: I knew some ways of swapping two ints without using a temporary variable. Just like: // Method 1: void swap1(int &a, int &b) { int temp = a; a = b; b = a; } // Method 2: void swap2(int &a, int &b) { a = a + b; b = a - b; a = a - b; } // Method 3: void swap(int &a, int &b) { a = a ^ b; b = a ^ b; a = a ^ b; } But I don't know the difference among these ways. The third method is read from a book, and the author said that would be some cases where this method can't be used. I don't why, Would you please help me solve this puzzle? The first method works and could easily be modified to swap other types of values such as strings. The other two methods are at best non-portable (integer overflow is undefined behaviour) and fail in swap(a,a); /Peter Thank you! Gerald Nov 22 '05 #3

 P: n/a Hello Pete Sorry for sounding naive, but could u please elaborate? Regards Techantor Nov 22 '05 #4

 P: n/a On 2005-11-19 14:37, technator wrote: Hello Pete Sorry for sounding naive, but could u please elaborate? Regards Techantor An example would be if in the second method a+b is greater than the largest number you can store in an integer. Since it is unspecified behaviour you never know what will happen, will depend on architecture and compiler. Erik Wikström -- "I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone" -- Bjarne Stroustrup Nov 22 '05 #5

 P: n/a technator skrev: Hello Pete Sorry for sounding naive, but could u please elaborate? Regards Techanto I'm not sure I understand, but you could try to test the algorithms with int i = ...; swap(i,i); /Peter Nov 22 '05 #6

 P: n/a Thanks Erik/ Pete. I understand. ~Technator Nov 22 '05 #7

 P: n/a Method 3 I found is not faster than 1. That would be the main reason for using it I expect but tests show it is slower. Method 2 would probably be similar in speed to 3. I don't see where any integer overflow is with method 3. Fraser. Nov 22 '05 #8

 P: n/a wrote in message news:11**********************@z14g2000cwz.googlegr oups.com...I knew some ways of swapping two ints without using a temporary variable. Just like: // Method 1: void swap1(int &a, int &b) { int temp = a; a = b; b = a; } I believe this is a mistake. I think you actually meant: void swap1(int &a, int &b) { int temp = a; a = b; b = temp; } because: a = b; b = a; makes both those variables the same no matter what came before them. Think about it. Say a = 5, b = 10. a = b; a is now 10, b is still 10. b = a; b is still 10, a is still 10. Nov 22 '05 #9

 P: n/a Fraser Ross wrote: Method 3 I found is not faster than 1. That would be the main reason for using it I expect but tests show it is slower. Method 2 would probably be similar in speed to 3. I don't see where any integer overflow is with method 3. There aren't any. It is Method 2 that suffers from overflow. The problem with Method 3 is that you can't call it with the same argument for a *and* b, as in swap( i, i ); The moral of the story: While every week all those methods (and probably more) are rediscovered, nothing beats the plain and simple 'use a temporary variable for swapping' method. It is simple, elegant and works in all cases. -- Karl Heinz Buchegger kb******@gascad.at Nov 22 '05 #10