Bug #70255 [Opn->Nab]: Bitwise AND behaviour in if statement
| From: | requinix@php.net | Date: | Thu, 13 Aug 2015 00:34:01 +0000 |
| Subject: | Bug #70255 [Opn->Nab]: Bitwise AND behaviour in if statement | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-195174@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70255&edit=1
ID: 70255
Updated by: requinix@php.net
Reported by: cattyv10 at gmail dot com
Summary: Bitwise AND behaviour in if statement
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Linux, (Windows maybe?)
PHP Version: 5.4.44
Block user comment: N
Private report: N
New Comment:
== has higher precedence than & so you're effectively writing
if( (1) & (1 == 1) & (1) ) { // 1&true&1 -> 1&1&1 -> 1&1 -> 1
-> true
if( (1) & (3 == 1) & (1) ) { // 1&false&1 -> 1&0&1 -> 0&1 ->
0 -> false
if( (2) & (2 == 2) & (2) ) { // 2&true&2 -> 2&1&2 -> 0&2 -> 0
-> false
http://php.net/manual/en/language.operators.precedence.php
Use parentheses.
Previous Comments:
------------------------------------------------------------------------
[2015-08-13 00:17:36] cattyv10 at gmail dot com
Description:
------------
Strange behaviour with bitwise AND when used in if statements.
Test script:
---------------
<?php
if(1&1 == 1&1) { echo "equal\n"; }else{ echo "not equal\n"; } //equal
0001 & 0001
if(1&3 == 1&3) { echo "equal\n"; }else{ echo "not equal\n"; } //not
equal???? 0001 & 0011
if(2&2 == 2&2) { echo "equal\n"; }else{ echo "not equal\n"; } //not
equal???? 0010 & 0010
?>
Expected result:
----------------
equal
equal
equal
Actual result:
--------------
equal
not equal
not equal
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70255&edit=1