示例#1
0
        /// <summary>
        /// Computes the quotient and the remainder after a division by an int number
        /// </summary>
        ///
        /// <param name="Value">The BigInteger dividend</param>
        /// <param name="Divisor">The divisor</param>
        /// <param name="DivisorSign">The divisors sign</param>
        ///
        /// <returns>Returns an array of the form <c>[quotient, remainder]</c></returns>
        internal static BigInteger[] DivideAndRemainderByInteger(BigInteger Value, int Divisor, int DivisorSign)
        {
            // res[0] is a quotient and res[1] is a remainder:
            int[] valDigits = Value._digits;
            int   valLen    = Value._numberLength;
            int   valSign   = Value._sign;

            if (valLen == 1)
            {
                long a   = (valDigits[0] & 0xffffffffL);
                long b   = (Divisor & 0xffffffffL);
                long quo = a / b;
                long rem = a % b;
                if (valSign != DivisorSign)
                {
                    quo = -quo;
                }
                if (valSign < 0)
                {
                    rem = -rem;
                }
                return(new BigInteger[] { BigInteger.ValueOf(quo),
                                          BigInteger.ValueOf(rem) });
            }
            int quotientLength = valLen;
            int quotientSign   = ((valSign == DivisorSign) ? 1 : -1);

            int[] quotientDigits = new int[quotientLength];
            int[] remainderDigits;
            remainderDigits = new int[] { Division.DivideArrayByInt(
                                              quotientDigits, valDigits, valLen, Divisor) };
            BigInteger result0 = new BigInteger(quotientSign, quotientLength,
                                                quotientDigits);
            BigInteger result1 = new BigInteger(valSign, 1, remainderDigits);

            result0.CutOffLeadingZeroes();
            result1.CutOffLeadingZeroes();
            return(new BigInteger[] { result0, result1 });
        }
示例#2
0
        /// <summary>
        /// Returns a string containing a string representation of this  BigInteger with base radix.
        /// <para>If Radix &lt; CharHelper.MIN_RADIX} or Radix > CharHelper.MAX_RADIX then a decimal representation is returned.
        /// The CharHelpers of the string representation are generated with method CharHelper.forDigit.</para>
        /// </summary>
        ///
        /// <param name="Value">The value to convert</param>
        /// <param name="Radix">Base to be used for the string representation</param>
        ///
        /// <returns>Returns a string representation of this with radix 10</returns>
        internal static string BigInteger2String(BigInteger Value, int Radix)
        {
            int sign         = Value._sign;
            int numberLength = Value._numberLength;

            int[] digits = Value._digits;

            if (sign == 0)
            {
                return("0"); //$NON-NLS-1$
            }
            if (numberLength == 1)
            {
                int  highDigit = digits[numberLength - 1];
                long v         = highDigit & 0xFFFFFFFFL;
                // Long.ToString has different semantic from C# for negative numbers
                if (sign < 0)
                {
                    return("-" + Convert.ToString(v, Radix));
                }

                return(Convert.ToString(v, Radix));
            }

            if ((Radix == 10) || (Radix < CharUtils.MIN_RADIX) || (Radix > CharUtils.MAX_RADIX))
            {
                return(Value.ToString());
            }

            double bitsForRadixDigit;

            bitsForRadixDigit = System.Math.Log(Radix) / System.Math.Log(2);
            int resLengthInChars = (int)(Value.Abs().BitLength / bitsForRadixDigit + ((sign < 0) ? 1 : 0)) + 1;

            char[] result      = new char[resLengthInChars];
            int    currentChar = resLengthInChars;
            int    resDigit;

            if (Radix != 16)
            {
                int[] temp = new int[numberLength];
                Array.Copy(digits, 0, temp, 0, numberLength);
                int tempLen     = numberLength;
                int charsPerInt = DigitFitInInt[Radix];
                int i;
                // get the maximal power of radix that fits in int
                int bigRadix = BigRadices[Radix - 2];
                while (true)
                {
                    // divide the array of digits by bigRadix and convert remainders
                    // to CharHelpers collecting them in the char array
                    resDigit = Division.DivideArrayByInt(temp, temp, tempLen, bigRadix);
                    int previous = currentChar;
                    do
                    {
                        result[--currentChar] = CharUtils.ForDigit(resDigit % Radix, Radix);
                    } while (((resDigit /= Radix) != 0) && (currentChar != 0));

                    int delta = charsPerInt - previous + currentChar;
                    for (i = 0; i < delta && currentChar > 0; i++)
                    {
                        result[--currentChar] = '0';
                    }

                    for (i = tempLen - 1; (i > 0) && (temp[i] == 0); i--)
                    {
                        ;
                    }
                    tempLen = i + 1;

                    if ((tempLen == 1) && (temp[0] == 0)) // the quotient is 0
                    {
                        break;
                    }
                }
            }
            else
            {
                // radix == 16
                for (int i = 0; i < numberLength; i++)
                {
                    for (int j = 0; (j < 8) && (currentChar > 0); j++)
                    {
                        resDigit = digits[i] >> (j << 2) & 0xf;
                        result[--currentChar] = CharUtils.ForDigit(resDigit, 16);
                    }
                }
            }
            while (result[currentChar] == '0')
            {
                currentChar++;
            }

            if (sign == -1)
            {
                result[--currentChar] = '-';
            }

            return(new String(result, currentChar, resLengthInChars - currentChar));
        }