About Me

My photo

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS/E/B d-- s:++>: a-- C++(++++) UL++ P! L++ E--- W+++ N+++ w PS+ PE- !tv
b+++ G++ e++ h
------END GEEK CODE BLOCK------

Wednesday, April 21, 2004

Swapping variables without dummy

This seems to be Old Programming School Practise. Came to know from Ariya.

a ^=b;
b ^=a;
a ^=b;

In C Programming, ^ stands for Exclusive OR.

Lets expand this expression and denote an and bn for new values of a and b then,

bn = b xor ( a xor b) = b xor a xor b = a
an = a xor ( b xor a) = a xor b xor a = b

No comments: