Variable interchange in PHP Tutorial

Source: Internet
Author: User
AttributedtoSolomonW. Golomb; values (youcantellthisdatesfromtheElderDays, whenvariableswereexpensive !). ThankstoPHPssyntaxitsalsoaone-l Attributed to Solomon W. golomb; a method for swapping the values of two integer variables without using an intermediate variable (you can tell this dates from the Elder Days, when variables were expensive !). Thanks to PHP's syntax it's also a one-liner.

$ A ^ = $ B ^ = $ a ^ = $ B;

Okay, here's how it goes (yeah, like I need to make content-free posts just for the sake of an increment ...).

First, simplify the line; noting that ^ = is right-associative, which means that in that line the rightmost operator is evaluated first, that the assignment operators also return the value that they assign to their lvalue, and that foo ^ = bar is shorthand for foo = foo ^ bar:
Code:
$a^=$b^=$a^=$b;$a^=($b^=($a^=$b));$a=$a^b;$a^=($b^=$a);$a=$a^$b;$b=$b^$a;$a=$a^$b;


Recall what ^ does. takes each pair of corresponding bits from its arguments (the internal binary representation of its arguments, that is ), and xors them together to produce the corresponding bit of the result ("corresponding" means that the first bits of both arguments produce the first bit of the result, the second bits of both arguments produce the second bit of the result, and so on. this is why, as BuzzLY noted, it's important that both variables are the same size-demons probably start flying out of your nose if one of them runs out of bits to xor before the other. so to figure out what ^ does to a pair of variables, we only need to recap what it does to single bits

Code:
x  y  | x^y------+----0  0  | 00  1  | 11  0  | 11  1  | 0
Basically, x ^ y is true if x and y are different, and is false otherwise. In other words, x is true or y is true, but they're not BothTrue. Hence ," ExClusive-or ".

Now, taking those three lines, we see what happens when $ a and $ B start out with initial values $ s and $ t.

Code:
$a = $s;$b = $t;$a=$a^$b;$b=$b^$a;$a=$a^$b;// Since $b==$t, we substitute $t for $b for as long as $b doesn't change$a = $s;$a=$a^$t;$b=$t^$a;$a=$a^$b;// And likewise for $a$a=$s^$t;$b=$t^$a;$a=$a^$b;// But don't stop there!$b=$t^($s^$t);$a=($s^$t)^$b;$b=$t^($s^$t);$a=($s^$t)^($t^($s^$t));

From the table above, it's obvious that $ a ^ $ B = $ B ^ $ a (if $ a is different from $ B, then $ B must be different from $ a), and that $ a ^ $ a = 0 ($ a is not different from itself ). using those two facts, that the fact that $ a ^ 0 = $ a and $ a ^ ($ B ^ $ c) = ($ a ^ $ B) ^ $ c (which I leave as exercises for the reader), we can simplify those two rather long expressions.
Code:
$b=$t^($s^$t);$a=($s^$t)^($t^($s^$t));$b=$t^($t^$s);$a=($t^$s)^($t^($t^$s));$b=($t^$t)^$s;$a=($t^$s)^(($t^$t)^$s);$b=0^$s;$a=($t^$s)^(0^$s);$b=$s;$a=($t^$s)^$s;$b=$s;$a=$t^($s^$s);$b=$s;$a=$t^0;$b=$s;$a=$t;


So after starting out with $ a = $ s and $ B = $ t, we end up with $ a = $ t and $ B = $ s. in other words, ^ swapped the each pair of bits of $ a and $ B with each other. do that for all the bits and the result is one of $ a and $ B being swapped.


And after all that is it any mystery why (a) XOR is such a useful operation in cryptography, and (B) learning a bit of maths (boolean algebra in this case) can be useful in programming?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.