I'll try to explain:
PHP does all calculations in binary, but we code it in decimal... that's normal. The numbers we type in (e.g., 12, 32, 110, 120 etc..) are all assumed to be decimal unless we say otherwise. PHP outputs all numbers in decimal too.
Binary doesn't neccessarily have to be one's and zero's, it's just on and off states (the accepted is to represnt this with ones and zeros however).
Using the bitwise operators in PHP works as expected it's just that you need to know what the decimal values are AND if you don't know that there's the function decbin() and bindec() to convert between the two
So
Code: Select all
echo 10 ^ 5; //Binary equivalent is: 1010 XOR 101 (15)
Why?
Code: Select all
16 | 8 | 4 | 2 | 1 | Decimal
-------------------------------------------------------
1 0 1 0 10
1 0 1 5
Bits in 10 that are NOT in 5 =>
16 | 8 | 4 | 2 | 1
---------------------------------------
1 0 1 0
Bits in 5 that are NOT in 10 =>
16 | 8 | 4 | 2 | 1
-----------------------------------
1 0 1
Which gives =>
16 | 8 | 4 | 2 | 1 | Decimal
----------------------------------------------------
1 1 1 1 15
If you wanted to do that in a more clearly binary sense:
Code: Select all
echo bindec(1010) ^ bindec(101); //15
Hope that helps
