private uint[] Bits; //Массив в котором хранятся коэффициенты разложения числа в обратном подядке
private int Sign; //Отвечает за знак числа
private int Length; //Длина массива
public const Int64 Base = (long)UInt32.MaxValue + 1; //Основание системы счисление 2^32
public BigInt(UInt64 value) //Один из конструкторов
{
Sign = 1;
if (value <= Int32.MaxValue)
{
Bits = new uint[] { (uint)value };
Length = 1;
}
else
{
Bits = new uint[2];
Bits[0] = unchecked((uint)value);
Bits[1] = (uint)(value >> 32);
Length = 2;
}
}
public static BigInt Multiplication(BigInt x, BigInt y) //Сам алгоритм умножения "в столбик"
{
uint[] result = new uint[x.Length + y.Length];
ulong c = 0;
for(int i = 0; i < y.Length; i++)
{
ulong d = 0;
int j = 0;
for(; j < x.Length; j++)
{
c = (d + ((ulong)x.Bits[j] * (ulong)y.Bits[i]) + result[i+j]) % Base;
d = (d + ((ulong)x.Bits[j] * (ulong)y.Bits[i]) + result[i+j]) / Base;
result[i + j] = (uint)c;
}
result[i + j] = (uint)d;
}
result = Normalize(result);
return new BigInt(result, 1);
}
При умножении
BigInt test = new BigInt(UInt64.MaxValue);
test *= new BigInt(UInt64.MaxValue);
Я получаю BigInt, в котором массив Bits = {1, 0, 4294967294, 4294967295}
то есть в десятичной системе счисления 340282366920938463426481119288644075520
. А как мне получить это число из массива Bits?
То есть примерно что-то такое должно быть.
public string ToString(BigInt value)
{
string result = "";
if (value.Sign == -1) result += "-";
//Сам алгоритм
return result;
}
Bits = {1, 0, 4294967294, 4294967295}