25

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Young
  • 7,006
  • 7
  • 38
  • 62

6 Answers6

20

^ 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 ^ 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.

Yacoby
  • 51,022
  • 12
  • 106
  • 116
11

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.

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

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

means:

$x = $x ^ $y
$y = $y ^ ($x ^ $y)                // = $x
$x = ($x ^ $y) ^ ($y ^ ($x ^ $y))  // = $y
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
tanascius
  • 50,043
  • 21
  • 106
  • 130
7

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"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Denis Bazhenov
  • 8,695
  • 6
  • 41
  • 62
6

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
SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
  • In the example code for the question the operands are *strings*. What actually happens? For instance, are the ASCII values used? What if strings are longer than one? – Peter Mortensen Dec 28 '15 at 16:03
1

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

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.

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

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

Justin
  • 4,015
  • 1
  • 20
  • 31
0

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

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).

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.

Runcorn
  • 4,894
  • 4
  • 31
  • 50
ALJ
  • 1