ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
BitWise.php
Go to the documentation of this file.
1<?php
2
4
7
8class BitWise
9{
10 const SPLIT_DIVISOR = 2 ** 24;
11
17 private static function splitNumber($number): array
18 {
19 return [floor($number / self::SPLIT_DIVISOR), fmod($number, self::SPLIT_DIVISOR)];
20 }
21
35 public static function BITAND($number1, $number2)
36 {
37 try {
38 $number1 = self::validateBitwiseArgument($number1);
39 $number2 = self::validateBitwiseArgument($number2);
40 } catch (Exception $e) {
41 return $e->getMessage();
42 }
43 $split1 = self::splitNumber($number1);
44 $split2 = self::splitNumber($number2);
45
46 return self::SPLIT_DIVISOR * ($split1[0] & $split2[0]) + ($split1[1] & $split2[1]);
47 }
48
62 public static function BITOR($number1, $number2)
63 {
64 try {
65 $number1 = self::validateBitwiseArgument($number1);
66 $number2 = self::validateBitwiseArgument($number2);
67 } catch (Exception $e) {
68 return $e->getMessage();
69 }
70
71 $split1 = self::splitNumber($number1);
72 $split2 = self::splitNumber($number2);
73
74 return self::SPLIT_DIVISOR * ($split1[0] | $split2[0]) + ($split1[1] | $split2[1]);
75 }
76
90 public static function BITXOR($number1, $number2)
91 {
92 try {
93 $number1 = self::validateBitwiseArgument($number1);
94 $number2 = self::validateBitwiseArgument($number2);
95 } catch (Exception $e) {
96 return $e->getMessage();
97 }
98
99 $split1 = self::splitNumber($number1);
100 $split2 = self::splitNumber($number2);
101
102 return self::SPLIT_DIVISOR * ($split1[0] ^ $split2[0]) + ($split1[1] ^ $split2[1]);
103 }
104
118 public static function BITLSHIFT($number, $shiftAmount)
119 {
120 try {
121 $number = self::validateBitwiseArgument($number);
122 $shiftAmount = self::validateShiftAmount($shiftAmount);
123 } catch (Exception $e) {
124 return $e->getMessage();
125 }
126
127 $result = floor($number * (2 ** $shiftAmount));
128 if ($result > 2 ** 48 - 1) {
129 return Functions::NAN();
130 }
131
132 return $result;
133 }
134
148 public static function BITRSHIFT($number, $shiftAmount)
149 {
150 try {
151 $number = self::validateBitwiseArgument($number);
152 $shiftAmount = self::validateShiftAmount($shiftAmount);
153 } catch (Exception $e) {
154 return $e->getMessage();
155 }
156
157 $result = floor($number / (2 ** $shiftAmount));
158 if ($result > 2 ** 48 - 1) { // possible because shiftAmount can be negative
159 return Functions::NAN();
160 }
161
162 return $result;
163 }
164
172 private static function validateBitwiseArgument($value)
173 {
175
176 if (is_numeric($value)) {
177 if ($value == floor($value)) {
178 if (($value > 2 ** 48 - 1) || ($value < 0)) {
179 throw new Exception(Functions::NAN());
180 }
181
182 return floor($value);
183 }
184
185 throw new Exception(Functions::NAN());
186 }
187
188 throw new Exception(Functions::VALUE());
189 }
190
198 private static function validateShiftAmount($value)
199 {
201
202 if (is_numeric($value)) {
203 if (abs($value) > 53) {
204 throw new Exception(Functions::NAN());
205 }
206
207 return (int) $value;
208 }
209
210 throw new Exception(Functions::VALUE());
211 }
212
218 public static function nullFalseTrueToNumber(&$number): void
219 {
220 $number = Functions::flattenSingleValue($number);
221 if ($number === null) {
222 $number = 0;
223 } elseif (is_bool($number)) {
224 $number = (int) $number;
225 }
226 }
227}
$result
An exception for terminatinating execution or to throw for unit testing.
static validateBitwiseArgument($value)
Validate arguments passed to the bitwise functions.
Definition: BitWise.php:172
static BITXOR($number1, $number2)
BITXOR.
Definition: BitWise.php:90
static splitNumber($number)
Split a number into upper and lower portions for full 32-bit support.
Definition: BitWise.php:17
static BITRSHIFT($number, $shiftAmount)
BITRSHIFT.
Definition: BitWise.php:148
static BITAND($number1, $number2)
BITAND.
Definition: BitWise.php:35
static validateShiftAmount($value)
Validate arguments passed to the bitwise functions.
Definition: BitWise.php:198
static BITLSHIFT($number, $shiftAmount)
BITLSHIFT.
Definition: BitWise.php:118
static nullFalseTrueToNumber(&$number)
Many functions accept null/false/true argument treated as 0/0/1.
Definition: BitWise.php:218
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
Definition: Functions.php:649