From 33d05a07de147e7243866207cee765110bf6d2e8 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 10 Nov 2019 20:06:04 +0000 Subject: [PATCH] Fix `big_int_or` and `big_int_xor` --- src/big_int.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/big_int.cpp b/src/big_int.cpp index 5b6b228a5..a24b2f28d 100644 --- a/src/big_int.cpp +++ b/src/big_int.cpp @@ -1248,15 +1248,15 @@ void big_int_xor(BigInt *dst, BigInt const *x, BigInt const *y) { x = y; y = tmp; } - - // x ^ (-y) == x ^ ~(y-1) == ~(x ^ (y-1)) == -((x ^ (y-1)) + 1) - dst->neg = false; - BigInt y1 = big_int_make_abs(y); - big_int_sub_eq(&y1, &BIG_INT_ONE); - big_int__xor_abs(dst, x, &y1); - big_int_add_eq(dst, &BIG_INT_ONE); - dst->neg = true; + if (y->neg) { + // x ^ (-y) == x ^ ~(y-1) == ~(x ^ (y-1)) == -((x ^ (y-1)) + 1) + BigInt y1 = big_int_make_abs(y); + big_int_sub_eq(&y1, &BIG_INT_ONE); + big_int__xor_abs(dst, x, &y1); + big_int_add_eq(dst, &BIG_INT_ONE); + dst->neg = true; + } return; } @@ -1316,13 +1316,15 @@ void big_int_or(BigInt *dst, BigInt const *x, BigInt const *y) { x = y; y = tmp; } - - // x | (-y) == x | ~(y-1) == ~((y-1) &~ x) == -(~((y-1) &~ x) + 1) - BigInt y1 = big_int_make_abs(y); - big_int_sub_eq(&y1, &BIG_INT_ONE); - big_int__and_not_abs(dst, &y1, x); - big_int_add_eq(dst, &BIG_INT_ONE); - dst->neg = true; + dst->neg = false; + if (y->neg) { + // x | (-y) == x | ~(y-1) == ~((y-1) &~ x) == -(~((y-1) &~ x) + 1) + BigInt y1 = big_int_make_abs(y); + big_int_sub_eq(&y1, &BIG_INT_ONE); + big_int__and_not_abs(dst, &y1, x); + big_int_add_eq(dst, &BIG_INT_ONE); + dst->neg = true; + } return; }