>  기사  >  백엔드 개발  >  PHP教程之变量互换_PHP

PHP教程之变量互换_PHP

WBOY
WBOY원래의
2016-06-01 12:34:51944검색

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:
<div dir="ltr" style="TEXT-ALIGN: left">
$a^=$b^=$a^=$b;

$a^=($b^=($a^=$b));

$a=$a^b;
$a^=($b^=$a);

$a=$a^$b;
$b=$b^$a;
$a=$a^$b;
<br><br>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 <br><br>
<div style="MARGIN: 5px 20px 20px">
<div class="smallfont" style="MARGIN-BOTTOM: 2px">Code:<pre class="alt2" style="BORDER-RIGHT: 1px inset; PADDING-RIGHT: 6px; BORDER-TOP: 1px inset; PADDING-LEFT: 6px; PADDING-BOTTOM: 6px; MARGIN: 0px; OVERFLOW: auto; BORDER-LEFT: 1px inset; WIDTH: 740px; PADDING-TOP: 6px; BORDER-BOTTOM: 1px inset; HEIGHT: 130px"><div dir="ltr" style="TEXT-ALIGN: left">
x  y  | x^y
------+----
0  0  | 0
0  1  | 1
1  0  | 1
1  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 <i>both</i> true. Hence, "<i>ex</i>clusive-or". <br><br>Now, taking those three lines, we see what happens when $a and $b start out with initial values $s and $t. <br><br>
<div style="MARGIN: 5px 20px 20px">
<div class="smallfont" style="MARGIN-BOTTOM: 2px">Code:<pre class="alt2" style="BORDER-RIGHT: 1px inset; PADDING-RIGHT: 6px; BORDER-TOP: 1px inset; PADDING-LEFT: 6px; PADDING-BOTTOM: 6px; MARGIN: 0px; OVERFLOW: auto; BORDER-LEFT: 1px inset; WIDTH: 740px; PADDING-TOP: 6px; BORDER-BOTTOM: 1px inset; HEIGHT: 402px"><div dir="ltr" style="TEXT-ALIGN: left">
$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));
<br>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. <br>
<div style="MARGIN: 5px 20px 20px">
<div class="smallfont" style="MARGIN-BOTTOM: 2px">Code:<pre class="alt2" style="BORDER-RIGHT: 1px inset; PADDING-RIGHT: 6px; BORDER-TOP: 1px inset; PADDING-LEFT: 6px; PADDING-BOTTOM: 6px; MARGIN: 0px; OVERFLOW: auto; BORDER-LEFT: 1px inset; WIDTH: 740px; PADDING-TOP: 6px; BORDER-BOTTOM: 1px inset; HEIGHT: 402px"><div dir="ltr" style="TEXT-ALIGN: left">
$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;

<p><br>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. <br></p>
<p><br>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? </p>
					
		
		
		
    </div>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:php里 的 四舍五入_PHP다음 기사:用php修改ftp密码_PHP

관련 기사

더보기