Search code examples
phpbitwise-operatorsbitwise-and

Bitwise Operators (And) in PHP. Getting weird answers


So I'm making this script that needs to get the integer from the & operator of two numers in PHP. So far I have managed it to work but with small numbers (<512). When I try to do do for example: echo 65535 $ 2048; I get a result of 0, when it's supposed to be 2048. Any ideas why I am getting this result or how can I get the desired result?. Thank you In advance I'm using PHP 5.5 and Ubuntu.


Solution

  • $ is not the and operator, & is.

    If you try:

    echo 65535 & 2048;
    

    you should get 2048 just fine, as I just did on http://sandbox.onlinephpfunctions.com/.