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 | 1 | public function __construct($mask = 0) |
|
27 | |||
28 | /** |
||
29 | * @param int $mask |
||
30 | * @return $this |
||
31 | */ |
||
32 | 1 | public static function create($mask = 0) |
|
33 | { |
||
34 | 1 | return new self($mask); |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * @param int $bit |
||
39 | * @return $this |
||
40 | */ |
||
41 | 1 | public function setBit($bit) |
|
45 | |||
46 | /** |
||
47 | * @param int $mask |
||
48 | * @return $this |
||
49 | */ |
||
50 | 1 | public function addMask($mask) |
|
55 | |||
56 | /** |
||
57 | * @param int $bit |
||
58 | * @return int |
||
59 | * @throws \InvalidArgumentException |
||
60 | */ |
||
61 | 1 | private function checkBit($bit) |
|
62 | { |
||
63 | 1 | if ($bit > self::MAX_BIT) { |
|
64 | 1 | throw new \InvalidArgumentException(sprintf( |
|
65 | 1 | 'Bit number %d is greater than possible limit %d.', |
|
66 | 1 | $bit, |
|
67 | self::MAX_BIT |
||
68 | 1 | )); |
|
69 | } |
||
70 | return $bit; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param int $bit |
||
75 | * @return $this |
||
76 | */ |
||
77 | 1 | public function unsetBit($bit) |
|
81 | |||
82 | /** |
||
83 | * @param int $mask |
||
84 | * @return $this |
||
85 | */ |
||
86 | 1 | public function deleteMask($mask) |
|
91 | |||
92 | /** |
||
93 | * @param int $bit |
||
94 | * @return bool |
||
95 | */ |
||
96 | 3 | public function issetBit($bit) |
|
100 | |||
101 | /** |
||
102 | * @return int |
||
103 | */ |
||
104 | 1 | public function getMask() |
|
108 | |||
109 | /** |
||
110 | * @param int $mask |
||
111 | * @return $this |
||
112 | */ |
||
113 | 1 | public function setMask($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 | 1 | public function getSetBitsCount() |
|
128 | } |
||
129 |