コード例 #1
0
ファイル: BigNum.cs プロジェクト: DorShaar/RSAAlgorithm
        /// <summary>
        /// D.
        /// Converts the big number from this instance to long number.
        /// Checks that the number can fit into long. If so, returns it.
        /// The biggest positive value that can be hold by long variable is: 2^63 -1,
        /// therfore has 63 bits.
        /// </summary>
        /// <returns>longNumber</returns>
        public long?BigToLong()
        {
            BigNum copyNumber = new BigNum(this);
            long?  longNumber;
            int    fromBase   = 2;
            bool   isNegative = false;

            if (IsNegative)
            {
                // Make copyNumber represented as positive number.
                copyNumber.Complement();
                copyNumber.AddNum(GlobalVariables.one);
                isNegative = true;
            }

            if (IsBigNumFitsIntoLongType(copyNumber))
            {
                longNumber = Convert.ToInt64(copyNumber.WriteNum(), fromBase);
                if (isNegative)
                {
                    longNumber = -longNumber;
                }
            }
            else
            {
                longNumber = null;
                Console.WriteLine($"Convertion from BigNum to long is not possible," +
                                  $"this BigNum has more then {GlobalVariables.MaxNumberOfDigitsInLongType} digits");
            }

            return(longNumber);
        }
コード例 #2
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();
        }
コード例 #3
0
        public static void E()
        {
            Console.WriteLine("(E) Using AddNum method.");
            Console.WriteLine("8 + 13 = 21.");
            BigNum bigNumE1 = new BigNum(8L);
            BigNum bigNumE2 = new BigNum(13L);

            bigNumE1.AddNum(bigNumE2);
            Console.WriteLine(bigNumE1.WriteNum());

            Console.WriteLine();
        }
コード例 #4
0
        public static void C()
        {
            Console.WriteLine("(C) Using LongToBig method.");
            Console.WriteLine("Creating number with long 35.");
            BigNum bigNumC1 = new BigNum(35L);

            Console.WriteLine(bigNumC1.WriteNum());
            Console.WriteLine("Creating number with long 9223372036854775807 (max value of long).");
            BigNum bigNumC2 = new BigNum(9223372036854775807L);

            Console.WriteLine(bigNumC2.WriteNum());

            Console.WriteLine();
        }
コード例 #5
0
        public static void J()
        {
            Console.WriteLine("(I) Using DivNum method.");
            Console.WriteLine("257 / 13 ==> result = 19, reminder 10.");

            BigNum divided  = new BigNum(257L);
            BigNum divider  = new BigNum(13L);
            BigNum quitient = new BigNum();
            BigNum reminder = new BigNum();

            MathOps.DivNum(divided, divider, quitient, reminder);

            Console.WriteLine($"Quitient is: {quitient.WriteNum()}, Reminder is: {reminder.WriteNum()}.");
            Console.WriteLine();
        }
コード例 #6
0
        public static void I()
        {
            Console.WriteLine("(I) Using SubNum method.");
            Console.WriteLine("18 - 5 = 13");
            BigNum bigNumI1 = new BigNum(18L);
            BigNum bigNumI2 = new BigNum(5L);

            bigNumI1.SubNum(bigNumI2);
            Console.WriteLine(bigNumI1.WriteNum());

            Console.WriteLine("375683 - 12345 = 363338 (1011000101101001010 in binary.)");
            BigNum bigNumI3 = new BigNum(375683L);
            BigNum bigNumI4 = new BigNum(12345L);

            bigNumI3.SubNum(bigNumI4);
            Console.WriteLine(bigNumI3.WriteNum());

            Console.WriteLine();
        }
コード例 #7
0
        public static void G()
        {
            Console.WriteLine("(G) Using MultNum method.");
            Console.WriteLine("Multiply 7 by 3. Equals 21 (10101 in base 2)");
            BigNum bigNumG1 = new BigNum(7L);
            BigNum bigNumG2 = new BigNum(3L);

            bigNumG1.MultNum(bigNumG2);
            Console.WriteLine(bigNumG1.WriteNum());

            Console.WriteLine("Multiply 113 by 73. Equals 8249 (10000000111001 in base 2)");
            BigNum bigNumG3 = new BigNum(113L);
            BigNum bigNumG4 = new BigNum(73L);

            bigNumG3.MultNum(bigNumG4);
            Console.WriteLine(bigNumG3.WriteNum());

            Console.WriteLine();
        }
コード例 #8
0
        public static void A()
        {
            Console.WriteLine("(A) Using ReadNum methods.");
            Console.WriteLine("1. reading 0101");
            BigNum bigNum1a = new BigNum();

            bigNum1a.ReadNum("0101");
            Console.WriteLine(bigNum1a.WriteNum());
            Console.WriteLine($"The first 1' digit is in position: {bigNum1a.MSB1Location}");

            Console.WriteLine("2. reading positions of 1s. Getting a the list {399, 385}.");
            BigNum bigNum2a = new BigNum();

            bigNum2a.ReadNum(new List <int> {
                399, 385
            });
            Console.WriteLine(bigNum2a.WriteNum());
            Console.WriteLine($"The first 1' digit is in position: {bigNum2a.MSB1Location}");

            Console.WriteLine();
        }
コード例 #9
0
        public static void K()
        {
            Console.WriteLine("(K) Using ExtendedGCD method.");

            Console.WriteLine("Calculates GCD(26, 21). Should be = 1");
            BigNum bigNum3 = new BigNum(26);
            BigNum bigNum4 = new BigNum(21);
            BigNum x       = new BigNum();
            BigNum y       = new BigNum();
            BigNum gcd     = new BigNum();

            MathOps.ExtendedGCD(bigNum4, bigNum3, x, y, gcd);
            Console.WriteLine($"GCD = {gcd.WriteNum()} = {bigNum3.DecimalValue}*{x.DecimalValue}" +
                              $" + {bigNum4.DecimalValue}*{y.DecimalValue}");

            Console.WriteLine("Calculates GCD(15, 10). Should be = 5");
            BigNum bigNum1 = new BigNum(15);
            BigNum bigNum2 = new BigNum(10);

            MathOps.ExtendedGCD(bigNum2, bigNum1, x, y, gcd);
            Console.WriteLine($"GCD = {gcd.DecimalValue} = {bigNum1.DecimalValue}*{x.DecimalValue}" +
                              $" + {bigNum2.DecimalValue}*{y.DecimalValue}");
            Console.WriteLine();
        }