Thread: swapping chars/numbers

  1. #1
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46

    swapping chars/numbers

    Hi All,

    To swap chars/numbers between two variables, I've used the below code successfully on some implementations.

    char a='a', b='b';
    a ^= b ^= a ^= b;

    I recently tried it on a different machine and it doesn't seem to work.
    Could someone explain as to what might be the issue with this code.

    regards
    maverix

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I had the impression that the XOR swap is pretty much guaranteed to work for integer types. Does it work if you change it to:
    Code:
    char a = 'a', b = 'b';
    a ^= b;
    b ^= a;
    a ^= b;
    One possibility why your original code did not work is that is it in the realm of undefined behaviour since you are reading and modifying a twice within consecutive sequence points.
    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

  3. #3
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    Code:
    char a = 'a', b = 'b';
    a ^= b;
    b ^= a;
    a ^= b;
    i'll check this and let you know once i reach home (coz this behavior is in my home PC)

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    There are several threads about this code on this forum,
    Code:
    a ^= b ^= a ^= b;
    and the consensus is, the behavior is undefined.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also bear in mind that modern processors probably do better on creating a temporary variable and shuffling the data back and forth, rather than "confusing" the processor with an XOR operation.

    On older processors (particularly small embedded processors), XOR is perhaps faster because it saves a temporary location.

    --
    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.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Could someone give me some examples of what you would use that for?
    I'm guessing it has something to do with encryption...?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, it's just doing "char t = a; a = b; b = t;", so swapping a's with b's content, in a way that if you are not familiar with the concept, you won't see what it does. That in itself is a good reason NOT to do this in "professional code".

    --
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Could someone give me some examples of what you would use that for?
    These days, for educational purposes. In the past, or maybe when doing embedded programming, it would be to avoid a temporary when performing a swap. I note that matsp pretty much pointed this out in the post before your own
    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

  9. #9
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    Thank you all for your valuable inputs,

    laserlight...I checked it in my home PC and it works when the statements are separated.

    I was of the impression that this code would be efficient in all processors (old or new). Thanks to matsp for pointing out the differences.

    regards
    maverix

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    These days, for educational purposes. In the past, or maybe when doing embedded programming, it would be to avoid a temporary when performing a swap. I note that matsp pretty much pointed this out in the post before your own
    OK thanks.
    I just tried it on paper with a=3, b=5 and now I see how it works. I guess you could swap CPU registers like that if you don't have a 3rd register available?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    OK thanks.
    I just tried it on paper with a=3, b=5 and now I see how it works. I guess you could swap CPU registers like that if you don't have a 3rd register available?
    Yes, on a processor with 2 or 3 registers, it would make sense to do this.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Monitoring Page Swapping?
    By Cell in forum Linux Programming
    Replies: 10
    Last Post: 06-13-2010, 12:16 PM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. C bubble sort?
    By fredanthony in forum C Programming
    Replies: 11
    Last Post: 02-13-2006, 09:54 PM
  4. swapping within a char pointer array
    By Mario in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 01:31 AM