Completed
Push — master ( 217fbd...78d8b4 )
by Lesnykh
02:13
created

Bitmask::getSetBitsCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 1
    public function __construct($mask = 0)
24
    {
25 1
        $this->setMask($mask);
26 1
    }
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)
42
    {
43 1
        return $this->addMask(1 << $this->checkBit($bit));
44
    }
45
46
    /**
47
     * @param int $mask
48
     * @return $this
49
     */
50 1
    public function addMask($mask)
51
    {
52 1
        $this->mask |= $mask;
53 1
        return $this;
54
    }
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)
78
    {
79 1
        return $this->deleteMask(1 << $this->checkBit($bit));
80
    }
81
82
    /**
83
     * @param int $mask
84
     * @return $this
85
     */
86 1
    public function deleteMask($mask)
87
    {
88 1
        $this->mask &= ~$mask;
89 1
        return $this;
90
    }
91
92
    /**
93
     * @param int $bit
94
     * @return bool
95
     */
96 3
    public function issetBit($bit)
97
    {
98 3
        return (bool)($this->getMask() & (1 << $this->checkBit($bit)));
99
    }
100
101
    /**
102
     * @return int
103
     */
104 1
    public function getMask()
105
    {
106 1
        return $this->mask;
107
    }
108
109
    /**
110
     * @param int $mask
111
     * @return $this
112
     */
113 1
    public function setMask($mask)
114
    {
115 1
        $this->mask = (int)$mask;
116 1
        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 1
    public function getSetBitsCount()
125
    {
126 1
        return substr_count(decbin($this->mask), '1');
127
    }
128
}
129