Swaping the variables

This is a discussion on Swaping the variables within the C Programming forums, part of the General Programming Boards category; Hello Everyone, I found the code for swaping two variables without using a temproray variable. this says if a and ...

  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,301
    Look here.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,832
    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,796
    >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,832
    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, 10:07 PM
  4. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  5. Replies: 6
    Last Post: 01-02-2004, 12:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21