Thread: Swaping the variables

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    Red face Swaping the variables

    Hello Everyone,

    I found the code for swaping two variables without using a temproray variable. this says if a and b are two variables to swap then we should do something like this--

    a^=b^= ^=a ^=b;

    can anyone tell me what does this statment mean? How does this statment work????

    I would appreciate any help.......

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look here.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When reading the article that grumpy linked to, take special note of the last line of the "code example" section, and also carefully read the "reasons" sections.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Thanks for reply Gud site

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Except the correct expression is

    a^=b^=a^=b;

    I wonder if this technique works for floats/doubles (well it does, at the bit-level). That is, the compiler may whine that boolean operations are a no-no. This is left as an exercise for the reader.
    Last edited by nonoob; 03-09-2009 at 04:39 PM.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Except the correct expression is
    ...not what you posted. The "correct" version of this useless and error-prone trick is:
    Code:
    a ^= b, b ^= a, a ^= b;
    Note the introduction of a sequence point with each comma operator. Semicolons work as well.
    My best code is written with the delete key.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nonoob View Post
    Except the correct expression is

    a^=b^=a^=b;

    I wonder if this technique works for floats/doubles (well it does, at the bit-level). That is, the compiler may whine that boolean operations are a no-no. This is left as an exercise for the reader.
    The compiler almost certainly will not do XOR on floating point data types, or for that matter any other non-integer type [which is not, in my nomenclature, a boolean, but a bitwise operation]. And even if you manage to convince the compiler to perform the operation itself, it is LIKELY that any modern processor will do it fairly slowly, because you would be switching between using the floating point unit and the integer unit, which means stalls on both of those units, and most likely more extra operations than the traditional temp variable swap [and modern compilers can quite often turn the temp variable into a register anyways].

    [Yes, before anyone says so: SSE has a XORP/SD/S instruction that will do bitwise XOR on floating point values - but convincing the compiler to generate that instruction without half a dozen other instructions that take away the entire benefit is can be quite hard to achieve, and it is unlikely that it will actually provide better code than the most obvious temp based solution anyways].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I fear you may be right. I was experimenting with calculating an approximate square-root using masks and shifts by casting doubles to __int64 (pick you own flavour depending on compiler), and I got hideous blow-ups assigning the result. Probably because the numbers were still in the floating point unit and intermediate results were NAN or worse. Thanks for the explanation. I'll need to be much more careful in future. It seems this illustrates how the semantics of C are at odds with the underlying hardware.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  5. Replies: 6
    Last Post: 01-02-2004, 01:01 PM