Thread: Why ^ will swap values?

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Why ^ will swap values?

    Hi,

    I really don't understand why a^=b^=a^=b will swap the values of a and b. How does it work?

    thnx

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    a ^= b ^= a ^= b

    is the same as

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

    which can be written as

    a = a xor b
    b = b xor a = b xor (a xor b) = a
    a = a xor b = (a xor b) xor a = b

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I konw the outcome. But how come it'll swap two values?

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    A= 1101
    B= 0011
    a = a ^ b = 1101 ^ 0011 = 1110
    b = b ^ a = 0011 ^ 1110 = 1101
    a = a ^ b = 1110 ^ 1101 = 0011

    a is b's value
    b is a's value

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >I konw the outcome. But how come it'll swap two values?

    Just follow the calculations and you'll see why, it's the result of the definition of the xor operation. If I am correct it only applies to integer values and not to floating point values.

  7. #7
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    XOR

    the ^ means XOR or exclusive OR

    or usually abbreviated XOR. will turn a bit on only
    if the bits being compared are diff. for e.g.

    127^127 is

    0 1 1 1 1 1 1 1
    ^
    0 1 1 1 1 0 0 0
    -------------------------
    0 0 0 0 0 1 1 1

    in general, bitwise ANDs ORs and XORs apply their operations directly to each bit in the variable individually. for this conditions
    bitwise operators are not usually used in conditional statements
    the way relational and logical operators are.



    i hope i did well...


    but i think prelude has a good point of view in here..


    thnx
    " programming is 1% syntax and 99% logic "

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Did it wrong.

    127^127=0

  9. #9
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    oh really?

    i did not did it wrong...


    the book did it wrong...

    if you think your better than the book, then publish your own
    i just copied it from th book
    " programming is 1% syntax and 99% logic "

  10. #10
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    I was taught swaping values with xor is a favorite trick in assembly code, but I had never thought of using it in C. Nice.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Written out in long format

    1. a = a ^ b
    2. b = a ^ b
    3. a = a ^ b

    If you expand 2 (by substitution of a), you end up with the expression
    b = ( a ^ b ) ^ b;

    Now anything xor'ed with itself is zero, and anything xor'ed with 0 is itself...

    we have
    b = a ^ 0 // the b's cancel out giving us 0
    Hence
    b = a // half the swap completed

    Now do the same thing with 3
    a = ( a ^ b ) ^ a // remember that b is now a from the previous result
    This reduces to
    a = b ^ 0
    therefore
    a = b // the swap is complete

    Voila!

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    To save myself some typing, here is a link.

    http://www.cscene.org/CS9/CS9-02.html

    -Prelude
    My best code is written with the delete key.

  13. #13
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Thnx, understood it now everyone...

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Thnx, understood it now everyone...
    Good, now do us all a favor and don't adopt it as your favorite swap routine.

    -Prelude
    My best code is written with the delete key.

  15. #15
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Why? Any side effects?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM