Obfuscated SWAP: not portable!

Richard H. Gumpertz rhg at cpsolv.UUCP
Fri Sep 1 18:43:30 AEST 1989


The exchange of x and y that has been under discussion:
    x ^= y ^= x ^= y
is non-portable because it depends on right-to-left evaluation which is NOT
specified in the C standard.  To be portable, use
    (x ^= y, x ^= y ^= x)    or    (y ^= x ^= y, x ^= y)
either of which forces the two assignments to x to be done in a well-defined
order.

		Richard H. Gumpertz

I am the company, so no disclaimer is needed! :-)



More information about the Comp.lang.c mailing list