Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1996,6 +1996,7 @@ public static explicit operator decimal(BigInteger value)
Span<uint> stackallocedXd = stackalloc uint[1];
Span<uint> xd = stackallocedXd;
bool negx = GetPartsForBitManipulation(ref value, ref xd);
bool trackSignBit = false;

if (negx)
{
Expand All @@ -2020,9 +2021,13 @@ public static explicit operator decimal(BigInteger value)
}

NumericsHelpers.DangerousMakeTwosComplement(xd); // Mutates xd
if (xd[^1] == 0)
{
trackSignBit = true;
}
}

int zl = xd.Length - digitShift;
int zl = xd.Length - digitShift + (trackSignBit ? 1: 0);
uint[]? zdArray = null;
Span<uint> zd = stackalloc uint[0];
if (zl > 0)
Expand Down Expand Up @@ -2057,6 +2062,11 @@ stackalloc uint[StackallocUInt32Limit].Slice(0, zl) :
if (negx)
{
NumericsHelpers.DangerousMakeTwosComplement(zd); // Mutates zd

if (trackSignBit)
{
zd[^1] = 1;
}
}

return new BigInteger(zd, zdArray, negx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public static void RunRightShiftTests()
tempByteArray2 = new byte[] { (byte)32 };
VerifyRightShiftString(Print(tempByteArray2) + Print(tempByteArray1) + "b>>");
}

// RightShift Method - All One Uint Large BigIntegers - 32 bit Shift
for (int i = 0; i < s_samples; i++)
{
tempByteArray1 = GetRandomLengthAllOnesUIntByteArray(s_random);
tempByteArray2 = new byte[] { (byte)32 };
VerifyRightShiftString(Print(tempByteArray2) + Print(tempByteArray1) + "b>>");
}

// RightShift Method - Large BigIntegers - large - Shift
for (int i = 0; i < s_samples; i++)
{
Expand Down Expand Up @@ -211,6 +220,16 @@ private static byte[] GetRandomNegByteArray(Random random, int size)
return value;
}

private static byte[] GetRandomLengthAllOnesUIntByteArray(Random random)
{
int gap = random.Next(0, 128);
int byteLength = 4 + gap * 4 + 1;
byte[] array = new byte[byteLength];
array[0] = 1;
array[^1] = 0xFF;
return array;
}

private static string Print(byte[] bytes)
{
return MyBigIntImp.Print(bytes);
Expand Down