Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP按位操作未返回正确的值_Php_Bit Manipulation_Bitmask_Comparison Operators - Fatal编程技术网

PHP按位操作未返回正确的值

PHP按位操作未返回正确的值,php,bit-manipulation,bitmask,comparison-operators,Php,Bit Manipulation,Bitmask,Comparison Operators,我有一个函数,它接受一个数字并返回一个与天数对应的数组(该数字将在一周的每一天被位屏蔽)。但是,数组返回某些值的所有天数,并返回另一个值的空数组。 下面是函数 function get_days($days) { $days_arr = array(); echo "days: " . decbin($days) . " - type: " . gettype($days) . "<br/>"; echo "type1: " . gettype($days & 0

我有一个函数,它接受一个数字并返回一个与天数对应的数组(该数字将在一周的每一天被位屏蔽)。但是,数组返回某些值的所有天数,并返回另一个值的空数组。 下面是函数

function get_days($days) {
    $days_arr = array();


echo "days: " . decbin($days) . " - type: " . gettype($days) . "<br/>";
echo "type1: " . gettype($days & 0x01) . " - type2: " . gettype(0x01) . "<br/>";
echo "days & 0x01 = " . dechex($days & 0x01) . " = " . ($days & 0x01 == 0x01) . "<br/>";
echo "days & 0x02 = " . dechex($days & 0x02) . " = " . ($days & 0x02 == 0x02) . "<br/>";
echo "days & 0x04 = " . dechex($days & 0x04) . " = " . ($days & 0x04 == 0x04) . "<br/>";
echo "days & 0x08 = " . dechex($days & 0x08) . " = " . ($days & 0x08 == 0x08) . "<br/>";
echo "days & 0x10 = " . dechex($days & 0x10) . " = " . ($days & 0x10 == 0x10) . "<br/>";


    if($days & 0x01 == 0x01)
        $days_arr[] = 'M';

    if($days & 0x02 == 0x02)
        $days_arr[] = 'T';

    if($days & 0x04 == 0x04)
        $days_arr[] = 'W';

    if($days & 0x08 == 0x08)
        $days_arr[] = 'H';

    if($days & 0x10 == 0x10)
        $days_arr[] = 'F';

    return $days_arr;
}

我似乎无法找出问题背后的原因,在我看来这应该是合理的。

这是一个运算符优先级问题。见:

所以
=
&
之上。你不应该做:

$days & 0x02 == 0x02
但是:


这是一个运算符优先级问题。应将按位表达式括起来,因为比较具有更高的优先级,所以它发生在按位操作之前。在if语句中,给出所需结果的语法是
if($days&0x08)==0x08)
。看到和
$days & 0x02 == 0x02
($days & 0x02) == 0x02