function Calculator::bitwise
Performs a bitwise operation on a decimal number.
Parameters
'and'|'or'|'xor' $operator The operator to use.:
string $a The left operand.:
string $b The right operand.:
3 calls to Calculator::bitwise()
- Calculator::and in vendor/
brick/ math/ src/ Internal/ Calculator.php - Calculates bitwise AND of two numbers.
- Calculator::or in vendor/
brick/ math/ src/ Internal/ Calculator.php - Calculates bitwise OR of two numbers.
- Calculator::xor in vendor/
brick/ math/ src/ Internal/ Calculator.php - Calculates bitwise XOR of two numbers.
File
-
vendor/
brick/ math/ src/ Internal/ Calculator.php, line 553
Class
- Calculator
- Performs basic operations on arbitrary size integers.
Namespace
Brick\Math\InternalCode
private function bitwise(string $operator, string $a, string $b) : string {
[
$aNeg,
$bNeg,
$aDig,
$bDig,
] = $this->init($a, $b);
$aBin = $this->toBinary($aDig);
$bBin = $this->toBinary($bDig);
$aLen = \strlen($aBin);
$bLen = \strlen($bBin);
if ($aLen > $bLen) {
$bBin = \str_repeat("\x00", $aLen - $bLen) . $bBin;
}
elseif ($bLen > $aLen) {
$aBin = \str_repeat("\x00", $bLen - $aLen) . $aBin;
}
if ($aNeg) {
$aBin = $this->twosComplement($aBin);
}
if ($bNeg) {
$bBin = $this->twosComplement($bBin);
}
$value = match ($operator) { 'and' => $aBin & $bBin,
'or' => $aBin | $bBin,
'xor' => $aBin ^ $bBin,
};
$negative = match ($operator) { 'and' => $aNeg and $bNeg,
'or' => $aNeg or $bNeg,
'xor' => $aNeg xor $bNeg,
};
if ($negative) {
$value = $this->twosComplement($value);
}
$result = $this->toDecimal($value);
return $negative ? $this->neg($result) : $result;
}