Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wasatch!cs.utexas.edu!uunet!mcvax!piring.cwi.nl!tromp From: tromp@piring.cwi.nl (John Tromp) Newsgroups: comp.lang.c Subject: Re: swap(x,y) Message-ID: <8350@boring.cwi.nl> Date: 22 Aug 89 08:21:03 GMT References: Sender: news@cwi.nl Lines: 25 tg1e+@andrew.cmu.edu (Timothy R. Gottschalk) writes: > To swap two variables x,y in C without using a temporary variable: > /* begin swap */ > x += y; > y = x - y; > x -= y; > /* end swap */ > Just curious...(this looks pretty valid) does anyone know an even >simpler method? I would never program this way -- it's more of a theory >question. I've been told that it can't be done (???). >Tim Gottschalk >Pgh, PA How about x^=y^=x^=y; Avoids possible overflow problems and looks great in an Obfuscated C Code entry too! John Tromp (tromp@cwi.nl)