1 | <?php |
||
8 | class Bitmask |
||
9 | { |
||
10 | /** |
||
11 | * @var int |
||
12 | */ |
||
13 | const MAX_BIT = 62; |
||
14 | |||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | private $mask; |
||
19 | |||
20 | /** |
||
21 | * @param int $mask |
||
22 | */ |
||
23 | public function __construct(int $mask = 0) |
||
24 | { |
||
25 | $this->setMask($mask); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @param int $mask |
||
30 | * @return $this |
||
31 | */ |
||
32 | public static function create(int $mask = 0) |
||
36 | |||
37 | /** |
||
38 | * @param int $bit |
||
39 | * @return $this |
||
40 | */ |
||
41 | public function setBit(int $bit) |
||
42 | { |
||
43 | return $this->addMask(1 << $this->checkBit($bit)); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param int $mask |
||
48 | * @return $this |
||
49 | */ |
||
50 | public function addMask(int $mask) |
||
55 | |||
56 | /** |
||
57 | * @param int $bit |
||
58 | * @return int |
||
59 | * @throws \InvalidArgumentException |
||
60 | */ |
||
61 | private function checkBit(int $bit): int |
||
62 | { |
||
63 | if ($bit > self::MAX_BIT) { |
||
64 | throw new \InvalidArgumentException(sprintf( |
||
65 | 'Bit number %d is greater than possible limit %d.', |
||
66 | $bit, |
||
67 | self::MAX_BIT |
||
68 | )); |
||
69 | } |
||
70 | return $bit; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param int $bit |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function unsetBit(int $bit) |
||
78 | { |
||
79 | return $this->deleteMask(1 << $this->checkBit($bit)); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param int $mask |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function deleteMask(int $mask) |
||
91 | |||
92 | /** |
||
93 | * @param int $bit |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function issetBit(int $bit): bool |
||
100 | |||
101 | /** |
||
102 | * @return int |
||
103 | */ |
||
104 | public function getMask(): int |
||
108 | |||
109 | /** |
||
110 | * @param int $mask |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function setMask(int $mask) |
||
118 | |||
119 | /** |
||
120 | * Return set bits count. |
||
121 | * Actually, counts the number of 1 in binary representation of the decimal mask integer. |
||
122 | * @return int |
||
123 | */ |
||
124 | public function getSetBitsCount(): int |
||
128 | } |
||
129 |