Re: swap two integers without using a tmp variable?

puppet_sock_at_hotmail.com
Date: 06/22/04


Date: 22 Jun 2004 14:48:38 -0700

Richard Herring <junk@[127.0.0.1]> wrote in message news:<1myFlguI4C2AFwv2@baesystems.com>...
> 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!

Well... You *could* add a self-assignment guard.

Sometimes coders find themselves working on extremely limited hardware.
Like a smoke detector or something. And every variable is taking up
the very limited RAM that was only allowed by having an arm-wrestling
match with the comptroller and the accountant.
Socks



Relevant Pages