swapping two numbers

Discussion in 'C' started by abhisek dash, Sep 29, 2009.

  1. abhisek dash

    abhisek dash New Member

    Joined:
    Sep 27, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    you should know, how to swap two numbers using a third variable!

    you may also know how to swap two numbers without using a third variable!

    but you may not know how to swap two numbers without using any of the arithimetic operators and a third variable!!!!!!

    if anyone does,please post it!:nice:
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Welcome to G4EF :)

    I know what you are trying to ask, and I am answering what you want to hear ::
    Here is how you can do it without using any arithmetic op.s and any temp. var. :
    Code:
    a = a^b;
    b = b^a;
    a = a^b;
    
    Or in a single line :
    Code:
    a^=b^=a^=b;
    
    But this is **NOT** the best way, because suppose you define this :
    Code:
    void swap(int &x, int &y)
    {    x^=y^=x^=y;    }
    This would work fine for calls like swap(A,B), where A = 2 and B = 3 say.
    But for calls like swap(A,A) or swap(A,B) where B=A, it would lead to entire loss of data (variable's contents).
    So, be careful while using it.


    Again, that won't work for data-types like float, double, structs, classes etc ...
    So, you should mention in your question that the target vars are of the type int.

    A generic swap function can be :
    Code:
    template <class TYPE>
    void swamp(TYPE &var1, TYPE &var2) {
        TYPE var_temp = var1;
        var1 = var2;
        var2 = var_temp;
    }
    
    That one uses a temp. var., 'coz there's no way out.

    You can do it this way too :
    Code:
    #include <algorithm>
    . . .
    . . .
    . . .
    swap(x,y);
    . . .
    . . .
    . . .
    
    This one doesn't seem to use any arithmetic op.s or temp var.s, but works with a temp var. in the background ;)
     
    Last edited: Sep 30, 2009
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    This code has undefined behavior, since it modifies the lvalue a twice without an intervening sequence point.

    See http://en.wikipedia.org/wiki/XOR_swap_algorithm

    The "Reasons for avoidance in practice" part is also worth a read.
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Yeah, I know abt that.
    That's why I added :
    The guy wanted that XOR form.

    But I still don't understand why it has "undefined" behavior.
    I read the theories, but am unable to find an example.
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Because both "a^=" parts modify a, and there is no "sequence point" between the two. You will see a definition of C++ sequence points in the linked Wiki article, which does not list ^= as containing an implied sequence point.

    > I read the theories, but am unable to find an example.
    An example is not necessary, only the language specification. The language does not define the behaviour, and that has no implications for the behaviour of any specific compiler; many compilers may implement it according to the common understanding, but that does not mean the behaviour is defined.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice