note 69771 added to language.operators.bitwise
| From: | keuleu at hotmail dot com | Date: | Wed, 20 Sep 2006 10:10:25 +0000 |
| Subject: | note 69771 added to language.operators.bitwise | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117343@lists.php.net to get a copy of this message | ||
<?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
----
Server IP: 216.235.15.211
Probable Submitter: 206.126.85.79
----
Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php
Edit -- https://master.php.net/note/edit/69771
Del: integrated -- https://master.php.net/note/delete/69771/integrated
Del: useless -- https://master.php.net/note/delete/69771/useless
Del: bad code -- https://master.php.net/note/delete/69771/bad+code
Del: spam -- https://master.php.net/note/delete/69771/spam
Del: non-english -- https://master.php.net/note/delete/69771/non-english
Del: in docs -- https://master.php.net/note/delete/69771/in+docs
Del: other reasons-- https://master.php.net/note/delete/69771
Reject -- https://master.php.net/note/reject/69771
Search -- https://master.php.net/manage/user-notes.php