another interesting way from Ivor Horton's book:
The ^ operator has a surprising property.
Suppose that you have two char variables, "first" with
the value âAâ, and "last" with the value âZâ,
corresponding to binary values "0100 0001" and "0101 1010". If
you write the statements
first ^= last; // Result first is 0001 1011
last ^= first; // Result last is 0100 0001
first ^= last; // Result first is 0101 1010
the result of these is that "first" and "last" have exchanged
values without using any intermediate memory
location.This works with any integer values.
|