Obfuscated SWAP

Ken Johnson ken at aiai.ed.ac.uk
Wed Aug 23 20:14:50 AEST 1989


Well, I tried it and it works, at least in the example I chose.  This
code

main( )
{
	int x, y;

	x = 12;
	y = 3;

	x ^= y ^= x ^= y;	/* Swap X and Y over */

	printf("x = %d\ny = %d\n",x,y);
}

produces this output:

x = 3
y = 12

I don't understand how it works, but I do now understand why C
programmers have a reputation for going around showing each other bits
of paper and saying `I bet you can't guess what this does!'

-- Ken

-- 
Ken Johnson, AI Applications Institute, 80 South Bridge, Edinburgh EH1 1HN
E-mail ken at aiai.ed.ac.uk, phone 031-225 4464 extension 212
`I have read your article, Mr.  Johnson, and I am no wiser than when I
started.' -- `Possibly not, sir, but far better informed.'



More information about the Comp.lang.c mailing list