for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Aliance\Bitmask;
/**
* Simple bitmask implementation.
* Supports only 63 bits (from 0 to 62) on x64 platforms.
*/
class Bitmask
{
* @var int
const MAX_BIT = 62;
private $mask;
* @param int $mask
public function __construct($mask = 0)
$this->setMask($mask);
}
* @return $this
public static function create($mask = 0)
return new self($mask);
* @param int $bit
public function setBit($bit)
return $this->addMask(1 << $this->checkBit($bit));
public function addMask($mask)
$this->mask |= $mask;
return $this;
* @return int
* @throws \InvalidArgumentException
private function checkBit($bit)
if ($bit > self::MAX_BIT) {
throw new \InvalidArgumentException(sprintf(
'Bit number %d is greater than possible limit %d.',
$bit,
self::MAX_BIT
));
return $bit;
public function unsetBit($bit)
return $this->deleteMask(1 << $this->checkBit($bit));
public function deleteMask($mask)
$this->mask &= ~$mask;
* @return bool
public function issetBit($bit)
return (bool)($this->getMask() & (1 << $this->checkBit($bit)));
public function getMask()
return $this->mask;
public function setMask($mask)
$this->mask = (int)$mask;
* Return set bits count.
* Actually, counts the number of 1 in binary representation of the decimal mask integer.
public function getSetBitsCount()
return substr_count(decbin($this->mask), '1');