示例#1
0
        public static void F()
        {
            Console.WriteLine("(F) Using ShiftLeft method.");
            Console.WriteLine("Multiply 25 by 2.");
            BigNum bigNumF = new BigNum(25L);

            bigNumF.ShiftLeft();
            Console.WriteLine(bigNumF.WriteNum());

            Console.WriteLine();
        }
示例#2
0
        /// <summary>
        /// G.
        /// Every number can be represented as 2N or 2N+1, when N is even and natural number.
        /// Converts <numberToMult> into long and into representation of 2N or 2N+1.
        /// Then doing N times ShiftLeft() method, If the number is 2N+1, adding one time the initial value
        /// of this instance.
        /// Or
        /// Let the current value of this intance be <value>.
        /// If <numberToMult[i]> == 0 then mult by 2 (ShiftLeft() method).
        /// If <numberToMult[i]> == 1 then mult by 2 (ShiftLeft() method) and adds <value>.
        /// </summary>
        /// <param name="numberToMult"></param>
        public void MultNum(BigNum numberToMult)
        {
            BigNum valueToAdd = new BigNum(this);

            InitializeToZero();
            for (int i = GlobalVariables.MAX_NUM - 1; i >= numberToMult.m1MSBLocation; --i)
            {
                if (numberToMult[i] == 1)
                {
                    AddNum(valueToAdd);
                }

                valueToAdd.ShiftLeft();
            }

            UpdateMSB1Location();
        }
示例#3
0
        /// <summary>
        /// J.
        /// Gets two BigNum type numbers, divided and divider.
        /// Returns by reference the result - quotient and reminder.
        /// </summary>
        /// <param name="divider"></param>
        public static void DivNum(BigNum divided, BigNum divider, BigNum quotientRetrunByRef, BigNum reminderReturnByRef)
        {
            List <int> positionsOf1s = new List <int>();

            if (divided.MSB1Location.HasValue)
            {
                int indexPos = divided.MSB1Location.Value + divider.Length - 1;

                BigNum subResult = new BigNum(divided);
                while (indexPos < GlobalVariables.MAX_NUM)
                {
                    BigNum numberFromLeftToIndex = GetSubBigNumFromLeftToIndex(subResult, indexPos);

                    if (numberFromLeftToIndex >= divider)
                    {
                        // Saving quotient's info.
                        positionsOf1s.Add(indexPos);

                        // Subtract.
                        int    numberOfShifts   = GlobalVariables.MAX_NUM - indexPos - 1;
                        BigNum numberToSubtract = new BigNum(divider);
                        numberToSubtract.ShiftLeft(numberOfShifts);
                        subResult.SubNum(numberToSubtract);
                    }

                    indexPos++;
                }

                quotientRetrunByRef.ReadNum(positionsOf1s);
                reminderReturnByRef.Set(subResult);
            }
            else
            {
                Console.WriteLine("Cannot divide by zero");
            }
        }