/// <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); }
/// <summary> /// I. /// Gets BigNum type and substract between the current value of the binary number with the given one. /// Let do substraction A = A-B. We know that A-B = A + (-B). /// Calculating -B: Doing complement and then add 1. /// </summary> /// <param name="numberToSubtract"></param> public void SubNum(BigNum numberToSubtract) { // numberToSubstract = B. BigNum modifiedNumberToSubtract = new BigNum(numberToSubtract); modifiedNumberToSubtract.Complement(); // numberToSubtract = -B - 1. modifiedNumberToSubtract.AddNum(GlobalVariables.one); // numberToSubtract = -B. AddNum(modifiedNumberToSubtract); // Allready updates <mMSB1Location>. UpdateMSB1Location(); }
public static void D() { Console.WriteLine("(D) Using BigToLong method."); BigNum bigNumC1 = new BigNum(35L); long? num1 = bigNumC1.BigToLong(); Console.WriteLine(num1); BigNum bigNumC2 = new BigNum(9223372036854775807L); long? num2 = bigNumC2.BigToLong(); Console.WriteLine(num2); BigNum bigNumD = new BigNum(); bigNumD.Complement(); long?num3 = bigNumD.BigToLong(); Console.WriteLine(num3); Console.WriteLine(); }