+bool Arbint::IsZero() const
+{
+ for (unsigned i = m_digits.size()-1; i > 0; --i)
+ {
+ if (m_digits[i] != 0L) return false;
+ }
+ return (m_digits[0] == 0L);
+}
+
+bool Arbint::operator==(const Arbint & equ) const
+{
+ if (m_sign != equ.m_sign)
+ return false;
+ unsigned min_size = m_digits.size();
+ const Arbint * larger = &equ;
+ if (m_digits.size() > equ.m_digits.size())
+ {
+ min_size = equ.m_digits.size();
+ larger = this;
+ }
+
+ if (memcmp(m_digits.data(), equ.m_digits.data(), sizeof(digit_t)*min_size) != 0)
+ return false;
+
+ for (unsigned i = min_size; i < larger->m_digits.size(); ++i)
+ {
+ if (larger->m_digits[i] != 0L)
+ return false;
+ }
+ return true;
+}
+
+bool Arbint::operator<(const Arbint & less) const
+{
+ Arbint cpy(*this);
+ cpy -= less;
+ return (cpy.m_sign && !cpy.IsZero());
+}
+
+string Arbint::DigitStr() const
+{
+ stringstream ss("");
+ //ss << std::hex << std::setfill('0');
+ for (unsigned i = 0; i < m_digits.size(); ++i)
+ {
+ if (i != 0) ss << ',';
+ //ss << std::setw(2*sizeof(digit_t)) << static_cast<digit_t>(m_digits[i]);
+ ss << static_cast<digit_t>(m_digits[i]);
+ }
+ return ss.str();
+}
+
+Arbint & Arbint::operator>>=(unsigned amount)
+{
+ // Shift by whole number of digits
+ unsigned whole = amount/(8*sizeof(digit_t));
+ unsigned old_size = m_digits.size();
+
+ if (whole >= old_size)
+ {
+ m_digits.resize(1,0L);
+ m_digits[0] = 0L;
+ return *this;
+ }
+ memmove(m_digits.data(), m_digits.data()+whole, sizeof(digit_t)*(old_size-whole));
+ m_digits.resize(old_size-whole, 0L);
+
+ // Shift by partial amount
+ amount = amount %(8*sizeof(digit_t));
+ if (amount == 0)
+ return *this;
+
+ digit_t underflow = 0L;
+ for (int i = (int)(m_digits.size()-1); i >= 0; --i)
+ {
+ unsigned shl = (8*sizeof(digit_t)-amount);
+ digit_t next_underflow = (m_digits[i] << shl);
+ //digit_t mask_upper = ~(0L >> amount);
+ m_digits[i] = (m_digits[i] >> amount);// & mask_upper;
+ m_digits[i] |= underflow;
+ underflow = next_underflow;
+ }
+ Shrink();
+ return *this;
+}
+
+Arbint & Arbint::operator<<=(unsigned amount)
+{
+ // Shift by whole number of digits
+ unsigned whole = amount/(8*sizeof(digit_t));
+ unsigned old_size = m_digits.size();
+ for (unsigned i = 0; i < whole; ++i)
+ {
+
+ GrowDigit(0L);//m_digits.resize(m_digits.size() + whole);
+ }
+ memmove(m_digits.data()+whole, m_digits.data(), sizeof(digit_t)*old_size);
+ memset(m_digits.data(), 0L, whole*sizeof(digit_t));
+
+
+
+ // Partial shifting
+ amount = amount % (8*sizeof(digit_t));
+ if (amount == 0)
+ return *this;
+
+ //Debug("Shift by %u from %u", amount, whole);
+ digit_t overflow = 0L;
+ for (unsigned i = whole; i < m_digits.size(); ++i)
+ {
+ //Debug("Digit is %.16lx", m_digits[i]);
+ unsigned shr = (8*sizeof(digit_t)-amount);
+ //Debug("shr is %u", shr);
+ digit_t next_overflow = (m_digits[i] >> shr);
+ //Debug("Next overflow %.16lx", next_overflow);
+ m_digits[i] <<= amount;
+ //Debug("Before overflow %.16lx", m_digits[i]);
+ m_digits[i] |= overflow;
+ overflow = next_overflow;
+ }
+ if (overflow != 0L)
+ m_digits.push_back(overflow);
+ Shrink();
+ return *this;
+}
+
+bool Arbint::GetBit(unsigned i) const
+{
+ unsigned digit = i/(8*sizeof(digit_t));
+ if (digit >= m_digits.size())
+ return false;
+
+ i = i % (8*sizeof(digit_t));
+
+ return (m_digits[digit] & (1L << i));
+
+}
+
+void Arbint::BitClear(unsigned i)
+{
+ unsigned digit = i/(8*sizeof(digit_t));
+ if (digit >= m_digits.size())
+ return;
+ i = i % (8*sizeof(digit_t));
+ m_digits[digit] &= ~(1L << i);
+}
+
+void Arbint::BitSet(unsigned i)
+{
+ unsigned digit = i/(8*sizeof(digit_t));
+ while (m_digits.size() < digit+1)
+ {
+ Debug("Grow BitSet Size %u, digit %u", m_digits.size(), digit);
+ GrowDigit(0L);
+ }
+
+ i = i % (8*sizeof(digit_t));
+ m_digits[digit] |= (1L << i);
+}
+