0

I'm converting some VB code to PHP, and in one line there is a logic operator AND.

The PHP and VB are giving me different results, so breaking down each step, it boils down to the logic operator AND, or && as it is in PHP.

In PHP I have:

($intAscii && $a)

with values of $intAscii = 87 and $a = 128.

The above logic should return 0, but PHP returns 1.

Converting these to binary:

    87 = ‭01010111‬
    128 = 10000000‬

so it seems obvious this should return 0, so why does PHP insist on returning 1?

Full function here:

    Function IsBitSet($intAscii , $intBit) {

        //'change the value of the integer passed by
        //'turning on the bit passed
        $a=(int)(pow(2 ,(int)$intBit));
        $b=($intAscii && $a);
        echo '</br>Manual AND  87 && 128 '.(87 && 128) .'</br>';
        echo 'is bit set $intAscii='.$intAscii .' $a='.$a.' And = '.( $b).'</br>';

        Return(($intAscii && $a) <> 0);

    }

and the output:

Manual AND 87 && 128 1
is bit set $intAscii=84 $a=128 And = 1
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Tym
  • 89
  • 2
  • 14
  • 2
    `&&` and `&` are different operators. – u_mulder Jun 19 '20 at 09:57
  • I'm not using &... – Tym Jun 19 '20 at 10:01
  • 1
    And in your case you __should__ use `&` and not `&&`. – u_mulder Jun 19 '20 at 10:05
  • Why? https://www.php.net/manual/en/language.operators.logical.php The PHP AND operator is &&, and that's what I'm trying to acheive - so why is it & and not &&? Take the manual 87 && 128 for example - that doesn't return the correct result using the correct operator. – Tym Jun 19 '20 at 10:16
  • Read the manual - "TRUE if both $a and $b are TRUE." Both 87 and 128 are TRUE. And now open this: https://www.php.net/manual/en/language.operators.bitwise.php and read about `&`. – u_mulder Jun 19 '20 at 10:17
  • Thanks u-mulder. I wasn't aware of the & | and ^ bitwise operators. – Tym Jun 19 '20 at 10:28

0 Answers0