Search code examples
phpbit-manipulationxor

php - chart for xor bitwise operator ^?


Is there a char with all the outputs? Or is the only way to figure them out is with trial and error?

For example

    <?php

    echo "A" ^ "{"; //outputs :

    ?>

Thanks.


Solution

  • There is no need for such a chart; the operation is perfectly predictable without one. Bitwise operations on PHP strings are performed on the individual bytes making up the string; for instance, in your example:

    ord("A") = 0x41
    ord("{") = 0x7b
               0x41 ^ 0x7b = 0x3a
                         chr(0x3a) = ":"