php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70255 Bitwise AND behaviour in if statement
Submitted: 2015-08-13 00:17 UTC Modified: 2015-08-13 00:34 UTC
From: cattyv10 at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.44 OS: Linux, (Windows maybe?)
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: cattyv10 at gmail dot com
New email:
PHP Version: OS:

 

 [2015-08-13 00:17 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-13 00:34 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-08-13 00:34 UTC] requinix@php.net
== 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.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 03:00:02 2026 UTC