swapping variables

Guy Harris guy at rlgvax.UUCP
Tue Feb 5 14:56:05 AEST 1985


> > How often have you written:
> > 
> > 	{	register type	t;
> > 
> > 		t = a;
> > 		a = b;
> > 		b = t;
> > 	}
> Rarely. I use:	`a ^= b; b ^= a; a ^= b;' Only worx for integer types.

Also takez more cycles on most machines.

An example: on the 68000, case 1 is

	move.l	a,t		# 32-bit ints
	move.l	b,a
	move.l	t,a

case 2 is

	move.l	b,d0
	eor.l	d0,a
	move.l	a,d0
	eor.l	d0,b
	move.l	b,d0
	eor.l	d0,a

Cute, but not worth it.

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy



More information about the Comp.lang.c mailing list