note 105142 added to language.variables.basics
| From: | gene dot garber at gmail dot NOSPAM | Date: | Fri, 29 Jul 2011 23:05:32 +0000 |
| Subject: | note 105142 added to language.variables.basics | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182010@lists.php.net to get a copy of this message | ||
The XOR note above stated the content was switching but I don't think switching or swapping the
variable data is a detailed way to explain the code there.
<?php
$a = 3;
$b = 5;
$a^=$b;
/**
~binary (unary operator)~
$b 0000 0101 a/k/a 5
$a 0000 0011 a/k/a 3
=either or but not both=
$a 0000 0110 a/k/a 6
~~
*/
$b^=$a;
/**
~~
$a 0000 0110 a/k/a 6
$b 0000 0101 a/k/a 5
=either or but not both=
$b 0000 0011 a/k/a 3
~~
*/
$a^=$b;
/**
~~
$b 0000 0011 a/k/a 3
$a 0000 0110 a/k/a 6
=either or but not both=
$a 0000 0101 a/k/a 5
~~
*/
?>
so now $a=5, $b=3
I think it would be less confusing to just do a regular swap (see below) but does XOR make it
possible to always swap variable data without the extra variable ($temp)?
<?php
$a=3;
$b=5;
$temp = $a;
// $temp = 3;
$a = $b;
// $a = 5;
$b = $temp;
// $b = 3;
// $a = 5, $b = 3, ($temp cries itself to sleep)
?>
----
Server IP: 69.147.83.197
Probable Submitter: 72.44.160.82
----
Manual Page -- http://www.php.net/manual/en/language.variables.basics.php
Edit -- https://master.php.net/note/edit/105142
Del: integrated -- https://master.php.net/note/delete/105142/integrated
Del: useless -- https://master.php.net/note/delete/105142/useless
Del: bad code -- https://master.php.net/note/delete/105142/bad+code
Del: spam -- https://master.php.net/note/delete/105142/spam
Del: non-english -- https://master.php.net/note/delete/105142/non-english
Del: in docs -- https://master.php.net/note/delete/105142/in+docs
Del: other reasons-- https://master.php.net/note/delete/105142
Reject -- https://master.php.net/note/reject/105142
Search -- https://master.php.net/manage/user-notes.php