Example #1
0
// a += b, ignoring signal
void add_inplace(bignum& a, const bignum& b)
{
    a.resize( 1 + max(a.size(), b.size()) );
    b_it i;
    b_cit j;
    Digit carry = 0;
    for (i=a.begin(), j=b.begin();
         i!=a.end();
         ++i) {
        *i += (j!=b.end() ? *j++ : 0) + carry;
        if (*i >= bignum::base) {
            *i -= bignum::base;
            carry = 1;
        } else
            carry = 0;
    }
    assert(carry == 0);
    a.clean();
}
Example #2
0
// a -= b, ignoring signal, assume a is larger than b
void subtract_inplace(bignum& a, const bignum& b)
{
    bignum x=a, y=b;

    b_it i;
    b_cit j;
    Digit borrow = 0;
    for (i=a.begin(), j=b.begin();
         i!=a.end();
         ++i) {
        *i -= (j!=b.end() ? *j++ : 0) + borrow;
        if (*i < 0) {
            *i += bignum::base;
            borrow = 1;
        } else
            borrow = 0;
    }
    assert(borrow == 0);
    a.clean();
}