开发者

Any functions to work with bitmask?

I have int field representing bitmask. Is there any functions available t开发者_开发技巧o work with bitmask? issetBit, unsetBit, setBit?

Thank you


Use bitwise operators. But if you want functions, here you have some.

function issetBit(& $mask, $bit) {
    return (bool)($mask & (1 << $bit));
}

function unsetBit(& $mask, $bit) {
    $mask &= ~(1 << $bit);
}

function setBit(& $mask, $bit) {
    $mask |= (1 << $bit);
}

Usage: first argument is your current bitmask; second argument is the number of the bit (zero-based). I.e. issetBit($mask, 2) equals (bool)($mask & 4) You cannot test/set/unset more than one bit at once with those functions though.


You want the Bitwise Operators: http://php.net/manual/en/language.operators.bitwise.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜