PHP Bitmask says true when it should be false

Artemis

I am comparing two variables with a bitwise AND, and it shouldn't return true but it does. Here is my code,

if($d & $w){
    return true;
} else {
    return false;
}

where $d is 15 and $w is 31.

Why does it return true when the bitmasks are different?

hherger

You are not comparing the two variables, you are anding them instead. Whith a bitwise AND you cannot compare anything.

($d & $w) means $d AND $w, where AND is the boolean AND operator. You are anding two integer variables here which will give an integer too. And an integer is interpreted as TRUE in a comparison if it is not null.

$d is binary 01111
$w is binary 11111
($d & $w) obviously is binary 01111. If you do a var_dump($d & $w) you see that the result is an integer, not a boolean;

So, if you mean to compare the ANDed value, you should choose a comparison construct, like this:

if ( ($d & $w) == $d ) ...

which means: if the ANDed value of $d and $w equals $d.

Code example

<?php

$d = 15;
$w = 31;
$res = ($d & $w);
echo '$d=' . decbin($d) . '<br />';
echo '$w=' . decbin($w) . '<br />';
echo '($d & $w)=' . decbin($res) . '<br />';

// Boolean AND only
if($d & $w){
    echo '($d & $w) is true' . '<br />';
} else {
    echo '($d & $w) is false' . '<br />';
}

// Comparison with boolean AND
if ( ($d & $w) == $d ) {
    echo '(($d & $w) == $d) is true' . '<br />';
} else {
    echo '(($d & $w) == $d) is false' . '<br />';
}

// Simple comparison
if ($d == $w) {
    echo '($d == $w) is true' . '<br />';
} else {
    echo '($d == $w) is false' . '<br />';
}

Result

$d=1111
$w=11111
($d & $w)=1111
($d & $w) is true
(($d & $w) == $d) is true
($d == $w) is false

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Prolog help | how come 'tony' is true when it should be false?

Firebase realtime database .info/connected False when it should be True

Java - !String.equals("abc") returning TRUE when it should be FALSE

Java matcher.matches() returning false when it should be true

When should I var_export(..., TRUE), when FALSE?

HttpRequest.Content.IsMimeMultipartContent() is returning false when it should return true

Passport.js `isAuthenticated()` inconsistent behavior; false when it should be true

Should a boolean value be truncated to either true or false when assigned?

Javascript function returns true when it should return false

empty function return false when it should return true codeigniter

momentjs isValid is returning false when it should be true and visa versa

have_posts() outputting false on 'single.php' when should be true

Chinese character comparison returning false when it should return true

Why does this code snippet output "true" when it should return "false"?

Why is if statement returning false when it should be true?

LINQ Query returns false when it should be true

Bool = true when it should = false in C++

Split String Array[index] > constant returning True when it should be false

Javascript function seems to be returning true when it should be returning false

Getting a false Boolean value when I should be getting true

regex returns false when should return true

Isset() returning false when it should be true - Symfony PHP

Null-conditional operator always true in if statement when should be false

Javascript if statement come back false when it should be true

Why is this function returning true when it should be returning false?

Variable says it is False when run in the function, but then it reverts back to True

Python "==" returning False when it should return True

When should I return TRUE and when FALSE on DialogProc

An and statement returns True when it should return False