Path: utzoo!mnetor!uunet!husc6!bloom-beacon!gatech!purdue!i.cc.purdue.edu!j.cc.purdue.edu!k.cc.purdue.edu!l.cc.purdue.edu!cik From: cik@l.cc.purdue.edu (Herman Rubin) Newsgroups: comp.lang.c Subject: Swap Message-ID: <661@l.cc.purdue.edu> Date: 20 Jan 88 12:24:16 GMT References: <3819@sigi.Colorado.EDU> <5080013@hpfcdc.HP.COM> <7120@brl-smoke.ARPA> <3203@bunker.UUCP> Organization: Purdue University Statistics Department Lines: 17 Summary: It always works if the compiler does not mess it up If the hardware does not have a swap instruction, the following (which I believe is easier to read than what was originally posted) is likely to be as fast as any other way to exchange two entities of the same size, assuming that the exclusive or equal operation is in hardware, and that the operation does not introduce access conflicts: a ^= b; b ^= a; a ^= b; I have used it on various machines. Of course, a particular implementation of C might not let you use it on pointers or floats; this is a defect in C, as there is no way the hardware can give the wrong answer(s). -- Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907 Phone: (317)494-6054 hrubin@l.cc.purdue.edu (ARPA or UUCP) or hrubin@purccvm.bitnet