function Calculator::twosComplement
Parameters
string $number A positive, binary number.:
1 call to Calculator::twosComplement()
- Calculator::bitwise in vendor/
brick/ math/ src/ Internal/ Calculator.php - Performs a bitwise operation on a decimal number.
File
-
vendor/
brick/ math/ src/ Internal/ Calculator.php, line 600
Class
- Calculator
- Performs basic operations on arbitrary size integers.
Namespace
Brick\Math\InternalCode
private function twosComplement(string $number) : string {
$xor = \str_repeat("\xff", \strlen($number));
$number ^= $xor;
for ($i = \strlen($number) - 1; $i >= 0; $i--) {
$byte = \ord($number[$i]);
if (++$byte !== 256) {
$number[$i] = \chr($byte);
break;
}
$number[$i] = "\x00";
if ($i === 0) {
$number = "\x01" . $number;
}
}
return $number;
}