swap(x,y)

Walter Bright bright at Data-IO.COM
Thu Aug 24 06:49:50 AEST 1989


In article <MYwDEFy00WB842JEUx at andrew.cmu.edu> tg1e+ at andrew.cmu.edu (Timothy R. Gottschalk) writes:
<To swap two variables x,y in C without using a temporary variable:
<       x += y;
<       y = x - y;
<       x -= y;
<does anyone know an even simpler method?
Known as the 'XOR trick':
	x ^= y;
	y ^= x;
	x ^= y;
or even:
	#asm	XCHG x,y	/* :-) */



More information about the Comp.lang.c mailing list