/// <summary> /// Calculates 'this' mod n2 (using the schoolbook division algorithm as above) /// </summary> /// <param name="n2"></param> public void Mod(BigInt n2) { if (n2.digitArray.Length != digitArray.Length) MakeSafe(ref n2); int OldLength = digitArray.Length; //First, we need to prepare the operands for division using Div_32, which requires //That the most significant digit of n2 be set. To do this, we need to shift n2 (and therefore n1) up. //This operation can potentially increase the precision of the operands. int shift = MakeSafeDiv(this, n2); BigInt Q = new BigInt(this.pres); BigInt R = new BigInt(this.pres); Q.digitArray = new UInt32[this.digitArray.Length]; R.digitArray = new UInt32[this.digitArray.Length]; Div_32(this, n2, Q, R); //Restore n2 to its pre-shift value n2.RSH(shift); R.RSH(shift); R.sign = (sign != n2.sign); AssignInt(R); //Reset the lengths of the operands SetNumDigits(OldLength); n2.SetNumDigits(OldLength); }
/// <summary> /// Calculates 'this'^power /// </summary> /// <param name="power"></param> public void Power(BigInt power) { if (power.IsZero() || power.sign) { Zero(); digitArray[0] = 1; return; } BigInt pow = new BigInt(power); BigInt temp = new BigInt(this); BigInt powTerm = new BigInt(this); pow.Decrement(); for (; !pow.IsZero(); pow.RSH(1)) { if ((pow.digitArray[0] & 1) == 1) { temp.Mul(powTerm); } powTerm.Square(); } Assign(temp); }
/// <summary> /// The right-shift operator /// </summary> public static BigInt operator >>(BigInt n1, int n2) { BigInt res = new BigInt(n1); res.RSH(n2); return res; }