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
精彩评论