检查位掩码值的总和中是否包含位掩码值

时间:2016-07-28 22:45:38

标签: php bitmask

我有以下bitmask值规范:

// Structure of Database:
// SC_NAME, flag
//
// flag    1  - SC cannot be removed by death.
//         2  - SC cannot be saved.
//         4  - SC cannot be reset by dispell.
//         8  - SC cannot be reset by clearance.
//         16 - SC considered as buff and be removed by Hermode and etc.
//         32 - SC considered as debuff and be removed by Gospel and etc.
//         64 - SC cannot be reset when MADO Gear is taken off.
//        128 - SC cannot be reset by 'sc_end SC_ALL' and status change clear.
//        256 - SC can be visible for all players

这是位掩码的示例用法:

  

SC_ENDURE,21

以上意思是:

SC_ENDURE: cannot be removed by death and dispel and considered as buff. (16 + 4 + 1 = 21)

我有一个要检查的CSV列表(例如修剪),如下所示:

SC_PROVOKE, 32
SC_ENDURE, 21
SC_HIDING, 4
SC_CLOAKING, 6
SC_TWOHANDQUICKEN, 24
SC_CONCENTRATION, 16
SC_ENCHANTPOISON, 16
SC_ORCISH, 2

我想要做的是通过列表选择所有被视为buff 16的效果到一个列表中,将其他效果选择到一个单独的列表中。

使用上面的例子;如果16存在于位掩码总和21中,如何检查?

这是我到目前为止所尝试的(由于我对位掩码缺乏了解而且没有运气):

<pre>
<?php

$buff_list = [];
$not_buffs = [];

if (($handle = fopen("data.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        list ($effect_code, $bitmask_value) = $data;
        $effect_code = trim($effect_code);
        $bitmask_value = (int)trim($bitmask_value);
        if (16 | $bitmask_value) {
            $buff_list[] = $effect_code;
        } else {
            $not_buffs[] = $effect_code;
        }
    }
    fclose($handle);
}

print_r($buff_list);
echo "<hr>";
print_r($not_buffs);

我尝试的代码是将所有效果都放入$buff_list,我不确定我是否正确行事。

1 个答案:

答案 0 :(得分:1)

替换

(16 | $bitmask_value)

(16 & $bitmask_value)

修改以帮助澄清:

(16 | $bitmask_value) = &bitmask_value 中的所有标记以及 16。 示例:(1 | 16) = 17((4 | 16) | 16) = (4 | 16) = 20

(16 & $bitmask_value) = &bitmask_value 中的所有标记也在中。 示例:(1 & 16) = 0((4 | 16) & 16) = 16((1 | 2 | 4) & (2 | 4 | 8)) = (2 | 4) = 6