コード例 #1
0
ファイル: big_ops2.cpp プロジェクト: randombit/botan
/*
* 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
ファイル: big_ops3.cpp プロジェクト: BenjaminSchiborr/safe
/*
* 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;
   }