Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!quasi-eli!cs.yale.edu!horne-scott From: horne-scott@cs.yale.edu (Scott Horne) Newsgroups: comp.lang.c Subject: Re: Swapping two variables in place Message-ID: <26310@cs.yale.edu> Date: 23 Sep 90 02:23:46 GMT References: <26101@shamash.cdc.com> Sender: news@cs.yale.edu Organization: Yale University Computer Science Dept., New Haven, CT 06520-2158 Lines: 23 Nntp-Posting-Host: jaguar.zoo.cs.yale.edu Originator: horne@jaguar.CS.Yale.Edu In article <26101@shamash.cdc.com> awm@shamash.UUCP (Allan Magnuson) writes: >There was a message a while back about not being able to create a good >#define function to swap two variables. > >How about this one: #define swap(a,b) a^=b^=a^=b I prefer #define ___(_,__)(_^=__^=_^=__) It's delightfully difficult to read, isn't it? :-) "Oh, that? It swaps variables. Can't you tell?" :-) But it won't work on `float's and `double's, as `^' is defined only on integral types. --Scott -- Scott Horne ...!{harvard,cmcl2,decvax}!yale!horne horne@cs.Yale.edu SnailMail: Box 7196 Yale Station, New Haven, CT 06520 203 436-1817 Residence: Rm 1817 Silliman College, Yale Univ Uneasy lies the head that wears the _gao1 mao4zi_.