note 69771 deleted from language.operators.bitwise by danielc
| From: | danielc@php.net | Date: | Mon, 13 Apr 2009 16:14:54 +0000 |
| Subject: | note 69771 deleted from language.operators.bitwise by danielc | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153175@lists.php.net to get a copy of this message | ||
Note Submitter: keuleu at hotmail dot com
Reason: 2
----
<?php
function xor_swap(&$a, &$b) {
if($a==$b) {
return;
}
$c = $a ^ $b;
$b = $c ^ $b;
$a = $c ^ $a;
}
?>
Explanation:
$c=$a*^$b*;
$b=$c^$b*; => $b=($a*^$b*)^$b*; => $b=$a*^0; => $b=$a*;
$a=$c^$a*; => $a=($a*^$b*)^$a; => $a=$b*^0; =>$a=$b*;
*original value of the variable
Now a version of xor_swap() with no buffer ($c):
<?php
function xor_swap(&$a,&$b){
if($a==$b) {
return;
}
$a=$a^$b;
$b=$a^$b;
$a=$a^$b;
}
?>
Explanation:
$a=$a*^$b*;
$b=$a^$b*; => $b=($a*^$b*)^$b*; => $b=$a*;
$a=$a^$b; => $a=($a*^$b*)^$a*; =>$a=$b*;
* original value of the variable