/************************************************* * Comparison Function * *************************************************/ s32bit BigInt::cmp(const BigInt& n, bool check_signs) const { if(check_signs) { if(n.is_positive() && this->is_negative()) return -1; if(n.is_negative() && this->is_positive()) return 1; if(n.is_negative() && this->is_negative()) return (-bigint_cmp(data(), sig_words(), n.data(), n.sig_words())); } return bigint_cmp(data(), sig_words(), n.data(), n.sig_words()); }
/************************************************* * Count how many bits are being used * *************************************************/ u32bit BigInt::bits() const { if(sig_words() == 0) return 0; u32bit full_words = sig_words() - 1, top_bits = MP_WORD_BITS; word top_word = word_at(full_words), mask = MP_WORD_TOP_BIT; while(top_bits && ((top_word & mask) == 0)) { mask >>= 1; top_bits--; } return (full_words * MP_WORD_BITS + top_bits); }
/* * 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); }
/* * Left Shift Operator */ BigInt& BigInt::operator<<=(size_t shift) { if(shift) { const size_t shift_words = shift / MP_WORD_BITS, shift_bits = shift % MP_WORD_BITS, words = sig_words(); grow_to(words + shift_words + (shift_bits ? 1 : 0)); bigint_shl1(mutable_data(), words, shift_words, shift_bits); } return (*this); }