Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!usc!ginosko!uunet!dgis!generous From: generous@dgis.daitc.mil (Curtis Generous) Newsgroups: comp.lang.c Subject: Re: swap(x,y) Message-ID: <40@dgis.daitc.mil> Date: 22 Aug 89 12:46:51 GMT References: Organization: DTIC Special Projects Office (DTIC-SPO), Alexandria VA Lines: 32 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 Saw this come across the net a few months back. Don't know where it came from. swap( a , b ) unsigned *a, *b; { *a ^= *b ; *b ^= *a ; *a ^= *b ; } I'm not sure it is any simpler, just another original approach. --curtis -- Curtis C. Generous DTIC Special Projects Office (DTIC-SPO) ARPA: generous@daitc.mil UUCP: {uunet,vrdxhq}!dgis!generous