Completed
Pull Request — master (#4)
by Lesnykh
02:27
created

Bitmask::getMask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace Aliance\Bitmask;
3
4
/**
5
 * Simple bitmask implementation.
6
 * Supports only 63 bits (from 0 to 62) on x64 platforms.
7
 */
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)
33
    {
34
        return new self($mask);
35
    }
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)
51
    {
52
        $this->mask |= $mask;
53
        return $this;
54
    }
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)
87
    {
88
        $this->mask &= ~$mask;
89
        return $this;
90
    }
91
92
    /**
93
     * @param int $bit
94
     * @return bool
95
     */
96
    public function issetBit(int $bit): bool
97
    {
98
        return (bool)($this->getMask() & (1 << $this->checkBit($bit)));
99
    }
100
101
    /**
102
     * @return int
103
     */
104
    public function getMask(): int
105
    {
106
        return $this->mask;
107
    }
108
109
    /**
110
     * @param int $mask
111
     * @return $this
112
     */
113
    public function setMask(int $mask)
114
    {
115
        $this->mask = (int)$mask;
116
        return $this;
117
    }
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
125
    {
126
        return substr_count(decbin($this->mask), '1');
127
    }
128
}
129