php 按位运算符 XOR ('^') 如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2674920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:18:48  来源:igfitidea点击:

How does the bitwise operator XOR ('^') work?

phpbitwise-operators

提问by Young

I'm a little confused when I see the output of following code:

当我看到以下代码的输出时,我有点困惑:

$x = "a";
$y = "b";
$x ^= $y;
$y ^= $x;
$x ^= $y;
echo $x; //Got b
echo $y; //Got a

How does the operator ^work here?

^这里的操作员是如何工作的?

采纳答案by tanascius

This looks like swapping a value using XOR. Though I am not sure about the strings in PHP (normally you use it for ints or something). For a truth table of XOR you can look here.

这看起来像使用 XOR 交换值。虽然我不确定 PHP 中的字符串(通常你将它用于整数或其他东西)。对于 XOR 的真值表,您可以查看这里

The interesting thing about XORis that it is reversable: A XOR B XOR B == A ... that is not working with ANDor OR. Because of this fact, it can be used as in your example to swap two values:

有趣的XOR是它是可逆的: A XOR B XOR B == A ... 与ANDor不起作用OR。由于这个事实,它可以在您的示例中用于交换两个值:

$x ^= $y;
$y ^= $x;
$x ^= $y;

means:

方法:

$x = $x ^ $y
$y = $y ^ ($x ^ $y)                // = $x
$x = ($x ^ $y) ^ ($y ^ ($x ^ $y))  // = $y

回答by Yacoby

^ is the "exclusive or" bitwise operator. It reads in English as "either or". The result is 1 if and only if both bits differ:

^ 是“异或”按位运算符。它在英语中读作“或者”。当且仅当两个位不同时,结果为 1:

1 ^ 0 = 1
1 ^ 1 = 0
0 ^ 0 = 0

Simplifying the example a bit so (and using Pseudo code):

稍微简化一下示例(并使用伪代码):

$x = 0011 //binary
$y = 0010

$x = $x xor $y
//Result: x = 0001

//x = 0001
//y = 0010
$y = $y xor $x
//Result: y = 0011

//x = 0001
//y = 0011
$x = $x xor $y
//Result: x = 0010

All that PHP has done is treat the string "a" and "b" as their integer equivalents.

PHP所做的只是将字符串“a”和“b”视为它们的整数等价物。

回答by Denis Bazhenov

In this example, when you're using ^ characters, they are casted to integers. So

在此示例中,当您使用 ^ 字符时,它们将被转换为整数。所以

"a" ^ "b"

is the same as:

是相同的:

ord("a") ^ ord ("b")

with one exception. In the first example, the result was casted back to a string. For example:

除了一个例外。在第一个示例中,结果被强制转换回字符串。例如:

"a" ^ "6" == "W"

because of:

因为:

ord("a") ^ ord("6") == 87

and

chr(87) == "W"

回答by SLaks

Th ^operator is a bitwise operator, meaning that it operates on every bit of its operands.

Th^运算符是按位运算符,这意味着它对其操作数的每一位进行运算。

It returns a value in which each bit is 1if the two corresponding bits in the operands are unequal, and 0if they're equal.

1如果操作数中的两个相应位不相等,并且0如果它们相等,则它返回一个值,其中每个位是。

For example:

例如:

   100110110
 ^ 010001100   
 = 110111010

回答by Justin

The ^ operator performs an XOR on the bit values of each variable. XOR does the following:

^ 运算符对每个变量的位值执行 XOR。XOR 执行以下操作:

a   = 1100
b   = 1010
xor = 0110

x is the result of the XOR operation. If the bits are equal the result is 0 if they are different the result is 1.

x 是异或运算的结果。如果位相等,则结果为 0,如果位不同,则结果为 1。

In your example the ^= performs XOR and assignment, and you swap the bits around between the two variables $x and $y.

在您的示例中,^= 执行 XOR 和赋值,并在两个变量 $x 和 $y 之间交换位。

Read more here http://en.wikipedia.org/wiki/Xor_swap_algorithm

在这里阅读更多http://en.wikipedia.org/wiki/Xor_swap_algorithm

回答by ALJ

XORor the exclusive or is based on logic and circuits. It indicates that, for example, A ^= Bwhere A is 0111 and B is 0101 can be either 1 or 0 at each corresponding bit but not both. Therefore

XOR或独占或基于逻辑和电路。它表示,例如,A ^= B当 A 为 0111 且 B 为 0101 时,每个对应位可以为 1 或 0,但不能同时为 1 或 0。所以

A = 0111
B = 0101
    _____
^=  0010 

To understand this better the rules of binary math apply except that there are no carry overs. So in binary math 1 + 0 = 1, 0 + 0 = 0, 0 + 1 = 1 and 1 + 1 = 0 (where a 1 is carried over to the next more significant position in binary math, but the XOR rules bypass this).

为了更好地理解这一点,二进制数学规则适用,除了没有结转。所以在二进制数学中 1 + 0 = 1, 0 + 0 = 0, 0 + 1 = 1 和 1 + 1 = 0(其中 1 被结转到二进制数学中的下一个更重要的位置,但 XOR 规则绕过了这个)。

Note: That the XOR rules, therefore, allow you to take the result of A ^= B in the example above and add A to it to get B or add B to it to get A (referencing the swap ability mentioned above.

注意:因此,XOR 规则允许您取上例中 A ^= B 的结果并将 A 添加到其中得到 B 或将 B 添加到其中得到 A(参考上面提到的交换能力。