Thread: A small puzzle
View Single Post
  #6 (permalink)  
Old August 21st, 2006, 02:05 AM
iut.aristo iut.aristo is offline
Registered User
 
Join Date: Jul 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote