Re: swap two integers without using a tmp variable?

From: Steve (ngsteve_at_my-deja.com)
Date: 06/22/04


Date: 22 Jun 2004 05:44:36 -0700

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)
Thanks for the help.



Relevant Pages

  • Re: puzzle
    ... >> Creating the base-two representation for the XOR requires either the ... A grown up programmer would avoid the XOR solution UNLESS it was needed ...
    (comp.programming)
  • Re: puzzle
    ... >> One real problem, however, with the XOR algorithm is that it assumes ... Using XOR, you just happen to get lucky, in the sense that XOR just ... It didn't state that the language, in which the algorithm was written ... I had to beat the XOR solution out of you. ...
    (comp.programming)
  • Re: puzzle
    ... one can do the xor without ... >I conclude that the XOR solution is junk, ... It's a puzzle; it was stated as a puzzle. ... Richard Harter, cri@xxxxxxxx ...
    (comp.programming)
  • Re: Need help with an algorithm
    ... number: DWORD; ... if (number xor c)<number then ... Very elegant. ...
    (comp.lang.pascal.delphi.misc)
  • Re: puzzle
    ... > CBFalconer wrote: ... snip ... ... >>> I still don't see the actual XOR solution. ...
    (comp.programming)