PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.14.6
Independent Analytics – WordPress Analytics Plugin v2.14.6
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / mlocati / ip-lib / src / Service / BinaryMath.php
independent-analytics / vendor / mlocati / ip-lib / src / Service Last commit date
BinaryMath.php 1 year ago RangesFromBoundaryCalculator.php 1 year ago UnsignedIntegerMath.php 1 year ago
BinaryMath.php
106 lines
1 <?php
2
3 namespace IAWPSCOPED\IPLib\Service;
4
5 /**
6 * Helper class to work with unsigned binary integers.
7 *
8 * @internal
9 */
10 class BinaryMath
11 {
12 /**
13 * Trim the leading zeroes from a non-negative integer represented in binary form.
14 *
15 * @param string $value
16 *
17 * @return string
18 */
19 public function reduce($value)
20 {
21 $value = \ltrim($value, '0');
22 return $value === '' ? '0' : $value;
23 }
24 /**
25 * Compare two non-negative integers represented in binary form.
26 *
27 * @param string $a
28 * @param string $b
29 *
30 * @return int 1 if $a is greater than $b, -1 if $b is greater than $b, 0 if they are the same
31 */
32 public function compare($a, $b)
33 {
34 list($a, $b) = $this->toSameLength($a, $b);
35 return $a < $b ? -1 : ($a > $b ? 1 : 0);
36 }
37 /**
38 * Add 1 to a non-negative integer represented in binary form.
39 *
40 * @param string $value
41 *
42 * @return string
43 */
44 public function increment($value)
45 {
46 $lastZeroIndex = \strrpos($value, '0');
47 if ($lastZeroIndex === \false) {
48 return '1' . \str_repeat('0', \strlen($value));
49 }
50 return \ltrim(\substr($value, 0, $lastZeroIndex), '0') . '1' . \str_repeat('0', \strlen($value) - $lastZeroIndex - 1);
51 }
52 /**
53 * Calculate the bitwise AND of two non-negative integers represented in binary form.
54 *
55 * @param string $operand1
56 * @param string $operand2
57 *
58 * @return string
59 */
60 public function andX($operand1, $operand2)
61 {
62 $operand1 = $this->reduce($operand1);
63 $operand2 = $this->reduce($operand2);
64 $numBits = \min(\strlen($operand1), \strlen($operand2));
65 $operand1 = \substr(\str_pad($operand1, $numBits, '0', \STR_PAD_LEFT), -$numBits);
66 $operand2 = \substr(\str_pad($operand2, $numBits, '0', \STR_PAD_LEFT), -$numBits);
67 $result = '';
68 for ($index = 0; $index < $numBits; $index++) {
69 $result .= $operand1[$index] === '1' && $operand2[$index] === '1' ? '1' : '0';
70 }
71 return $this->reduce($result);
72 }
73 /**
74 * Calculate the bitwise OR of two non-negative integers represented in binary form.
75 *
76 * @param string $operand1
77 * @param string $operand2
78 *
79 * @return string
80 */
81 public function orX($operand1, $operand2)
82 {
83 list($operand1, $operand2, $numBits) = $this->toSameLength($operand1, $operand2);
84 $result = '';
85 for ($index = 0; $index < $numBits; $index++) {
86 $result .= $operand1[$index] === '1' || $operand2[$index] === '1' ? '1' : '0';
87 }
88 return $result;
89 }
90 /**
91 * Zero-padding of two non-negative integers represented in binary form, so that they have the same length.
92 *
93 * @param string $num1
94 * @param string $num2
95 *
96 * @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
97 */
98 private function toSameLength($num1, $num2)
99 {
100 $num1 = $this->reduce($num1);
101 $num2 = $this->reduce($num2);
102 $numBits = \max(\strlen($num1), \strlen($num2));
103 return array(\str_pad($num1, $numBits, '0', \STR_PAD_LEFT), \str_pad($num2, $numBits, '0', \STR_PAD_LEFT), $numBits);
104 }
105 }
106