Added some improvements for TBigInt shift left operator

This commit is contained in:
Stefan Müller 2024-05-23 22:01:25 +02:00
parent 4329041353
commit 2df8266d42
2 changed files with 42 additions and 39 deletions

View File

@ -626,8 +626,11 @@ var
begin begin
// Handles shift of zero. // Handles shift of zero.
if A = 0 then if A = 0 then
Result := TBigInt.Zero begin
else begin Result := TBigInt.Zero;
Exit;
end;
// Determines full digit shifts and bit shifts. // Determines full digit shifts and bit shifts.
DivMod(B, CBitsPerDigit, digitShifts, bitShifts); DivMod(B, CBitsPerDigit, digitShifts, bitShifts);
@ -654,7 +657,7 @@ begin
Result.FDigits[i] := Result.FDigits[i] or (A.FDigits[j] << bitShifts); Result.FDigits[i] := Result.FDigits[i] or (A.FDigits[j] << bitShifts);
end; end;
if Length(Result.FDigits) > newLength then if lastDigit > 0 then
Result.FDigits[newLength] := lastDigit; Result.FDigits[newLength] := lastDigit;
end end
else begin else begin
@ -669,7 +672,6 @@ begin
Result.FIsNegative := A.IsNegative; Result.FIsNegative := A.IsNegative;
end; end;
end;
operator shr(const A: TBigInt; const B: Integer): TBigInt; operator shr(const A: TBigInt; const B: Integer): TBigInt;
var var

View File

@ -732,7 +732,8 @@ end;
{ TBigIntShiftLeftTestCase } { TBigIntShiftLeftTestCase }
procedure TBigIntShiftLeftTestCase.Test(const AHexValueOperand: string; const AShift: Integer; const AHexValueResult: string); procedure TBigIntShiftLeftTestCase.Test(const AHexValueOperand: string; const AShift: Integer; const AHexValueResult:
string);
var var
a, s: TBigInt; a, s: TBigInt;
begin begin