开发者

Any functions to work with bitmask?

开发者 https://www.devze.com 2023-02-05 10:05 出处:网络
I have int field representing bitmask. Is there any functions available t开发者_开发技巧o work with bitmask? issetBit, unsetBit, setBit?

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

精彩评论

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