in reply to Re: Re: Swapping two values
in thread Swapping two values
In languages like C (and not Perl) where it sometimes makes sense to use this method for swapping values, it will work just fine even if the values are the same. I'm unsure what you mean by "overlap." To see that it will work, try it with all four combinations of 2 bits:
See? It works fine even when the values are equal.A | B | A = A^B | B = A^B | A = A^B | A | B ===|===|=========|=========|=========|===|=== 0 | 0 | 0 = 0^0 | 0 = 0^0 | 0 = 0^0 | 0 | 0 ---+---+---------+---------+---------+---+--- 0 | 1 | 1 = 0^1 | 0 = 1^1 | 1 = 1^0 | 1 | 0 ---+---+---------+---------+---------+---+--- 1 | 0 | 1 = 1^0 | 1 = 1^0 | 0 = 1^1 | 0 | 1 ---+---+---------+---------+---------+---+--- 1 | 1 | 0 = 1^1 | 1 = 0^1 | 1 = 0^1 | 1 | 1
But, don't do it in perl!
-sauoq "My two cents aren't worth a dime.";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Swapping two values
by adrianh (Chancellor) on Feb 15, 2003 at 23:36 UTC | |
by sauoq (Abbot) on Feb 16, 2003 at 00:34 UTC | |
by Anonymous Monk on Feb 16, 2003 at 03:30 UTC |