Re: swap two integers without using a tmp variable?

From: Richard Herring (junk_at_[127.0.0.1)
Date: 06/22/04


Date: Tue, 22 Jun 2004 14:03:04 +0100

In message <976e0586.0406220444.4ed62241@posting.google.com>, Steve
<ngsteve@my-deja.com> writes
>The xor solution was really elegant. I would not have thought about it
>and never knew it could be done this way.
> X XOR Y XOR Y = X (assign in y variable, cancel out both Y)
> X XOR Y XOR X = Y (assign in x variable, cancel out both X)

And it DOESN'T WORK (no apologies for shouting) if X and Y are both
references to the same object. Don't do it!

-- 
Richard Herring


Relevant Pages

  • Re: swap two integers without using a tmp variable?
    ... Richard Herring wrote: ... >> The xor solution was really elegant. ... Mike Smith ...
    (comp.lang.cpp)
  • Re: Stickler 30/10/06 *Spoilers*
    ... Steve B. wrote: ... 15-down A part of Australia - one loyal to the queen was particularly ... elegant for a simple 3-letter word. ... (Solution by request.) ...
    (rec.puzzles.crosswords)
  • Re: Maybe a Mac version from the new company
    ... Msangali kirjoitti: ... them to something more elegant in the past, I don't think he would do this ... And Steve should understand that not all developers love the C-languages. ...
    (borland.public.delphi.non-technical)
  • Re: swap two integers without using a tmp variable?
    ... >>The xor solution was really elegant. ... Sometimes coders find themselves working on extremely limited hardware. ... the very limited RAM that was only allowed by having an arm-wrestling ...
    (comp.lang.cpp)
  • Re: copper-graphite connections
    ... aluminium foil gasket, but I doubt we could get a decent clamping force. ... Your method is elegant! ... Steve ...
    (sci.materials)