the difference among these ways of swapping two ints

Discussion in 'C++' started by storyGerald@gmail.com, Nov 19, 2005.

  1. Guest

    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 19, 2005
    #1
    1. Advertising

  2. technator Guest

    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
     
    technator, Nov 19, 2005
    #2
    1. Advertising

  3. peter koch Guest

    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
     
    peter koch, Nov 19, 2005
    #3
  4. Fraser Ross Guest

    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.
     
    Fraser Ross, Nov 19, 2005
    #4
  5. technator Guest

    Hello Pete

    Sorry for sounding naive, but could u please elaborate?

    Regards
    Techantor
     
    technator, Nov 19, 2005
    #5
  6. 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
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Nov 19, 2005
    #6
  7. peter koch Guest

    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
     
    peter koch, Nov 20, 2005
    #7
  8. technator Guest

    Thanks Erik/ Pete.

    I understand.

    ~Technator
     
    technator, Nov 20, 2005
    #8
  9. Jim Langston Guest

    <> wrote in message
    news:...
    >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.
     
    Jim Langston, Nov 20, 2005
    #9
  10. 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
     
    Karl Heinz Buchegger, Nov 21, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    606
    Mark P
    Apr 3, 2005
  2. Skybuck Flying

    ints ints ints and ints

    Skybuck Flying, Jul 8, 2004, in forum: C Programming
    Replies:
    24
    Views:
    870
    Jack Klein
    Jul 10, 2004
  3. thinktwice
    Replies:
    2
    Views:
    803
    thinktwice
    Aug 25, 2006
  4. Moe Sisko
    Replies:
    0
    Views:
    341
    Moe Sisko
    Mar 31, 2008
  5. Replies:
    13
    Views:
    454
    Antoninus Twink
    Apr 18, 2009
Loading...

Share This Page