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

前端 未结 6 496
粉色の甜心
粉色の甜心 2020-12-30 23:46

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
         


        
6条回答
  •  没有蜡笔的小新
    2020-12-31 00:10

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

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

    For example:

       100110110
     ^ 010001100   
     = 110111010
    

提交回复
热议问题