swap(x,y)

Richard O'Keefe ok at cs.mu.oz.au
Fri Sep 1 11:46:15 AEST 1989


In article <1545 at l.cc.purdue.edu>, cik at l.cc.purdue.edu (Herman Rubin) writes:
> In article <14479 at haddock.ima.isc.com>, karl at haddock.ima.isc.com (Karl Heuer) writes:
> > In article <230 at crdos1.crd.ge.COM> davidsen at crdos1.UUCP (bill davidsen) writes:
> > >However, there are reasons for doing tricks such as the XOR thing.  They
> > >enable me to write a macro which does the swap for any type.
> > Only for integral types.  It won't work on float, pointer, struct, etc.
...

> This is another example of keeping the tools from the programmer.

On the 18th of April, 1962, Christopher Strachey said:
	Writing programs needs genuis to save the last order
	or the last millisecond.  It is great fun, but it is
	a young man's game.  You start it with great
	enthusiasm when you first start programming, but after
	ten years you get a bit bored with it, and then you
	turn to automatic-programming languages and use them
	because they enable you to get to the heart of the
	problem that you want to do, instead of having to
	concentrate on the mechanics of getting the program
	going as fast as you possibly can, which is really
	nothing more than doing a sort of crossword puzzle.

I shouldn't need to point out that "the XOR thing" has the extremely
nasty "feature" that swap(x[i], x[j]) will set x[i] to 0 when i = j.

I shouldn't need to point out that some programming languages
(Burroughs Extended Algol, Pop2, Algol 68, Dijkstra's notation)
can do exchanges without XOR hacks, but that these languages remain
in a minority because exchanges are rare and easily avoidable at
low cost.



More information about the Comp.lang.c mailing list