Skip to content

Commit

Permalink
Fix addition with negative int64_t
Browse files Browse the repository at this point in the history
Negative part wasn't always handled, so expressions like (1 << 256) - 1
behaved incorrectly.
  • Loading branch information
cerg2010cerg2010 committed Jul 11, 2022
1 parent 4dcf417 commit 4cacdb6
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/Bint/arithmetics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,13 @@ Bint& Bint::operator += (const int64_t a) {
bool carry = false;
addUintWithCarry(number[0], a, carry);

if (carry) {
if (negA) {
for(int j = 1; j < (int)number.size(); j++) {
addUintWithCarry(number[j], -1, carry);
}
} else {
for(int j = 1; j < (int)number.size(); j++) {
addUintWithCarry(number[j], 0, carry);
if (!carry) break;
}
if (negA) {
for(int j = 1; j < (int)number.size(); j++) {
addUintWithCarry(number[j], -1, carry);
}
} else {
for(int j = 1; carry && j < (int)number.size(); j++) {
addUintWithCarry(number[j], 0, carry);
}
}

Expand Down

0 comments on commit 4cacdb6

Please sign in to comment.