Swapping two variables in place

Scott Horne horne-scott at cs.yale.edu
Sun Sep 23 12:23:46 AEST 1990


In article <26101 at shamash.cdc.com> awm at 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 at 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_.



More information about the Comp.lang.c mailing list