• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlocati / ip-lib / 17940910502

23 Sep 2025 09:00AM UTC coverage: 98.542% (-0.1%) from 98.642%
17940910502

Pull #105

github

web-flow
Merge 92f92d364 into 178d74d76
Pull Request #105: Add getExactSize() to ranges

14 of 15 new or added lines in 4 files covered. (93.33%)

5 existing lines in 3 files now uncovered.

946 of 960 relevant lines covered (98.54%)

236.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/src/Service/BinaryMath.php
1
<?php
2

3
namespace IPLib\Service;
4

5
/**
6
 * Helper class to work with unsigned binary integers.
7
 *
8
 * @internal
9
 */
10
class BinaryMath
11
{
12
    private static $instance;
13

14
    /**
15
     * @return \IPLib\Service\BinaryMath
16
     */
17
    public static function getInstance()
18
    {
19
        if (self::$instance === null) {
27✔
NEW
20
            self::$instance = new self();
×
21
        }
22

23
        return self::$instance;
27✔
24
    }
25

26
    /**
27
     * Trim the leading zeroes from a non-negative integer represented in binary form.
28
     *
29
     * @param string $value
30
     *
31
     * @return string
32
     */
33
    public function reduce($value)
34
    {
35
        $value = ltrim($value, '0');
1,027✔
36

37
        return $value === '' ? '0' : $value;
1,027✔
38
    }
39

40
    /**
41
     * Compare two non-negative integers represented in binary form.
42
     *
43
     * @param string $a
44
     * @param string $b
45
     *
46
     * @return int 1 if $a is greater than $b, -1 if $b is greater than $b, 0 if they are the same
47
     */
48
    public function compare($a, $b)
49
    {
50
        list($a, $b) = $this->toSameLength($a, $b);
16✔
51

52
        return $a < $b ? -1 : ($a > $b ? 1 : 0);
16✔
53
    }
54

55
    /**
56
     * Add 1 to a non-negative integer represented in binary form.
57
     *
58
     * @param string $value
59
     *
60
     * @return string
61
     */
62
    public function increment($value)
63
    {
64
        $lastZeroIndex = strrpos($value, '0');
155✔
65
        if ($lastZeroIndex === false) {
155✔
66
            return '1' . str_repeat('0', strlen($value));
13✔
67
        }
68

69
        return ltrim(substr($value, 0, $lastZeroIndex), '0') . '1' . str_repeat('0', strlen($value) - $lastZeroIndex - 1);
143✔
70
    }
71

72
    /**
73
     * Calculate the bitwise AND of two non-negative integers represented in binary form.
74
     *
75
     * @param string $operand1
76
     * @param string $operand2
77
     *
78
     * @return string
79
     */
80
    public function andX($operand1, $operand2)
81
    {
82
        $operand1 = $this->reduce($operand1);
516✔
83
        $operand2 = $this->reduce($operand2);
516✔
84
        $numBits = min(strlen($operand1), strlen($operand2));
516✔
85
        $operand1 = substr(str_pad($operand1, $numBits, '0', STR_PAD_LEFT), -$numBits);
516✔
86
        $operand2 = substr(str_pad($operand2, $numBits, '0', STR_PAD_LEFT), -$numBits);
516✔
87
        $result = '';
516✔
88
        for ($index = 0; $index < $numBits; $index++) {
516✔
89
            $result .= $operand1[$index] === '1' && $operand2[$index] === '1' ? '1' : '0';
516✔
90
        }
91

92
        return $this->reduce($result);
516✔
93
    }
94

95
    /**
96
     * Calculate the bitwise OR of two non-negative integers represented in binary form.
97
     *
98
     * @param string $operand1
99
     * @param string $operand2
100
     *
101
     * @return string
102
     */
103
    public function orX($operand1, $operand2)
104
    {
105
        list($operand1, $operand2, $numBits) = $this->toSameLength($operand1, $operand2);
501✔
106
        $result = '';
501✔
107
        for ($index = 0; $index < $numBits; $index++) {
501✔
108
            $result .= $operand1[$index] === '1' || $operand2[$index] === '1' ? '1' : '0';
501✔
109
        }
110

111
        return $result;
501✔
112
    }
113

114
    /**
115
     * Zero-padding of two non-negative integers represented in binary form, so that they have the same length.
116
     *
117
     * @param string $num1
118
     * @param string $num2
119
     *
120
     * @return string[],int[] The first array element is $num1 (padded), the first array element is $num2 (padded), the third array element is the number of bits
121
     */
122
    private function toSameLength($num1, $num2)
123
    {
124
        $num1 = $this->reduce($num1);
517✔
125
        $num2 = $this->reduce($num2);
517✔
126
        $numBits = max(strlen($num1), strlen($num2));
517✔
127

128
        return array(
129
            str_pad($num1, $numBits, '0', STR_PAD_LEFT),
517✔
130
            str_pad($num2, $numBits, '0', STR_PAD_LEFT),
517✔
131
            $numBits,
517✔
132
        );
133
    }
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc