/// <summary> /// Performs an integer division, returning the quotient and remainder after division /// </summary> /// <param name="dividend"></param> /// <param name="divisor"></param> /// <param name="remainder"></param> /// <param name="quotient"></param> public static void LongDivision(BinaryNumber dividend, BinaryNumber divisor, out BinaryNumber remainder, out BinaryNumber quotient) { var modifiedDivisor = new BinaryNumber(divisor); remainder = new BinaryNumber(dividend); while (remainder > modifiedDivisor) { modifiedDivisor.RotateLeft(); } if (modifiedDivisor > remainder) { modifiedDivisor.RotateRight(); } quotient = new BinaryNumber(); while (remainder > divisor) { if (modifiedDivisor > remainder) { quotient.AddLast(false); } else { remainder -= modifiedDivisor; quotient.AddLast(true); } modifiedDivisor.RotateRight(); } quotient.Compact(); }
/// <summary> /// Performs multiplication of a and b, returning the new binary number /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static BinaryNumber operator *(BinaryNumber a, BinaryNumber b) { var result = new BinaryNumber(); var aPrime = new BinaryNumber(a); // Makes a copy of a var bNode = b.Last; // Do long multiplication while (bNode != null) { if (bNode.Value) { result += aPrime; } aPrime.RotateLeft(); bNode = bNode.Previous; } return(result); }