[net.lang.c] swapping variables

cottrell@nbs-vms.ARPA (02/05/85)

/*
> >	 int ptr[]   <=>   int *ptr
> 
> Oo, oo!  This brings to mind a useful language extension:
> 
> 	a <=> b;
> 
> for exchanging the contents of a and b.  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.
*/

guy@rlgvax.UUCP (Guy Harris) (02/05/85)

> > 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