예제 #1
0
/*
* Modulo Operator
*/
word BigInt::operator%=(word mod)
{
    if(mod == 0)
        throw BigInt::DivideByZero();

    if(is_power_of_2(mod))
    {
        word result = (word_at(0) & (mod - 1));
        clear();
        grow_to(2);
        m_reg[0] = result;
        return result;
    }

    word remainder = 0;

    for(size_t j = sig_words(); j > 0; --j)
        remainder = bigint_modop(remainder, word_at(j-1), mod);
    clear();
    grow_to(2);

    if(remainder && sign() == BigInt::Negative)
        m_reg[0] = mod - remainder;
    else
        m_reg[0] = remainder;

    set_sign(BigInt::Positive);

    return word_at(0);
}
예제 #2
0
/*
* Modulo Operator
*/
word operator%(const BigInt& n, word mod)
   {
   if(mod == 0)
      throw BigInt::DivideByZero();
   if(power_of_2(mod))
      return (n.word_at(0) & (mod - 1));

   word remainder = 0;

   for(size_t j = n.sig_words(); j > 0; --j)
      remainder = bigint_modop(remainder, n.word_at(j-1), mod);

   if(remainder && n.sign() == BigInt::Negative)
      return mod - remainder;
   return remainder;
   }