Exemple #1
0
        /// <summary>
        /// Adds the values of an Integer and another Integer.
        /// </summary>
        /// <param name="right">The value to add.</param>
        /// <returns>The sum of the Integer and right.</returns>
        public Integer Add(Integer right)
        {
            if (right.length == 0)
            {
                return(Plus());
            }
            if (sign != right.sign)
            {
                return(Subtract(right.Negate()));
            }

            var result = length < right.length
                ? Calc.Add(right.bits, right.length, bits, length)
                : Calc.Add(bits, length, right.bits, right.length);

            return(new Integer(result, result.Length, sign));
        }
Exemple #2
0
        /// <summary>
        /// Subtracts an Integer from another Integer.
        /// </summary>
        /// <param name="right">The value to subtract.</param>
        /// <returns>
        /// The result of subtracting right from the Integer.
        /// </returns>
        public Integer Subtract(Integer right)
        {
            if (right.length == 0)
            {
                return(Plus());
            }
            if (sign != right.sign)
            {
                return(Add(right.Negate()));
            }

            var diff   = Bits.Compare(bits, length, right.bits, right.length);
            var result = diff < 0
                ? Calc.Subtract(right.bits, right.length, bits, length)
                : Calc.Subtract(bits, length, right.bits, right.length);

            return(new Integer(result, result.Length, diff < 0 ? !sign : sign));
        }