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 |