Skip to content

Commit

Permalink
Fix bug in Calculator::twosComplement()
Browse files Browse the repository at this point in the history
The code forgot to prepend the carry bit when all bytes have been zeroed.
This bug affected bitwise operations AND, OR, XOR for all but the GMP calculator.
  • Loading branch information
BenMorel committed Aug 18, 2020
1 parent fadf080 commit e6f8e7d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Internal/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,11 @@ private function twosComplement(string $number) : string
break;
}

$number[$i] = \chr(0);
$number[$i] = "\x00";

if ($i === 0) {
$number = "\x01" . $number;
}
}

return $number;
Expand Down
2 changes: 2 additions & 0 deletions tests/BigIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2389,6 +2389,8 @@ public function providerXor() : array
['1', '-2', '-1'],
['-1', '2', '-3'],
['0', '1', '1'],
['-83', '173', '-256'],
['-18771', '46765', '-65536'],
['123456789', '123456787', '6'],
['123456789012345678901234567890', '123456789012345678901234567880', '26'],

Expand Down

0 comments on commit e6f8e7d

Please sign in to comment.