1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:37:45 +00:00

LibJS+LibCrypto: Fix SignedBitInteger::bitwise_not and use it in LibJS

Bitwise operators are defined on two's complement, but SignedBitInteger
uses sign-magnitude. Correctly convert between the two.

Let LibJS delegate to SignedBitInteger for bitwise_not, like it does
for all other bitwise_ operations on bigints.

No behavior change (LibJS is now the only client of
SignedBitInteger::bitwise_not()).
This commit is contained in:
Nico Weber 2022-01-17 12:22:51 -05:00 committed by Ali Mohammad Pur
parent ec37eadb39
commit 945d962322
4 changed files with 15 additions and 5 deletions

View file

@ -160,7 +160,12 @@ FLATTEN SignedBigInteger SignedBigInteger::bitwise_xor(const UnsignedBigInteger&
FLATTEN SignedBigInteger SignedBigInteger::bitwise_not() const
{
return { unsigned_value().bitwise_not(), !m_sign };
// Bitwise operators assume two's complement, while SignedBigInteger uses sign-magnitude.
// In two's complement, -x := ~x + 1.
// Hence, ~x == -x -1 == -(x + 1).
SignedBigInteger result = plus(SignedBigInteger { 1 });
result.negate();
return result;
}
FLATTEN SignedBigInteger SignedBigInteger::multiplied_by(UnsignedBigInteger const& other) const