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(int $mask = 0)
$this->setMask($mask);
}
* @return $this
public static function create(int $mask = 0)
return new self($mask);
* @param int $bit
public function setBit(int $bit)
return $this->addMask(1 << $this->checkBit($bit));
public function addMask(int $mask)
$this->mask |= $mask;
return $this;
* @return int
* @throws \InvalidArgumentException
private function checkBit(int $bit): int
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(int $bit)
return $this->deleteMask(1 << $this->checkBit($bit));
public function deleteMask(int $mask)
$this->mask &= ~$mask;
* @return bool
public function issetBit(int $bit): bool
return (bool)($this->getMask() & (1 << $this->checkBit($bit)));
public function getMask(): int
return $this->mask;
public function setMask(int $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(): int
return substr_count(decbin($this->mask), '1');