public static integer operator >>(integer x, int shift) { if (shift == 0) return x; else if (shift < 0) return x << -shift; int digitShift = shift / BitsPerDigit; int smallShift = shift - (digitShift * BitsPerDigit); int xl = x.length; uint[] xd = x.data; int zl = xl - digitShift; uint[] zd = new uint[zl]; if (smallShift == 0) { for (int i = xl - 1; i >= digitShift; i--) { zd[i - digitShift] = xd[i]; } } else { int carryShift = BitsPerDigit - smallShift; uint carry = 0; for (int i = xl - 1; i >= digitShift; i--) { uint rot = xd[i]; zd[i - digitShift] = rot >> smallShift | carry; carry = rot << carryShift; } } integer result = new integer(x.sign, zd); if (x.sign < 0 && (x.data[0] & 1) != 0) result = result - 1; return result; }
internal integer modpow(int power, integer mod) { if (power == 0) return ONE; if (power < 0) throw new ArgumentOutOfRangeException("power", power, "power must be >= 0"); integer factor = this; integer result = ONE; //!!! want a mutable here for efficiency while (power != 0) { if ((power & 1) != 0) { result = result * factor; result = result % mod; //!!! should do all in one step } factor = factor.square(); power >>= 1; } return result; }
public static integer lshift(integer x, int shift) { return x << shift; }
public static integer rshift(integer x, int shift) { return x >> shift; }
public static integer or(integer x, integer y) { return x | y; }
public static integer xor(integer x, integer y) { return x ^ y; }
public static integer divrem(integer x, integer y, out integer mod) { integer div; uint[] q; uint[] r; DivModUnsigned(x.data, y.data, out q, out r); div = new integer(x.sign * y.sign, q); mod = new integer(x.sign, r); return div; }
public static integer and(integer x, integer y) { return x & y; }
public static integer modulo(integer x, integer y) { return x % y; }
public static integer divmod(integer x, integer y, out integer mod) { integer div = divrem(x, y, out mod); if (x.sign != y.sign && mod != ZERO) { div = div - 1; mod = mod + y; } return div; }
public static integer divide(integer x, integer y) { return x / y; }
public static integer multiply(integer x, integer y) { return x * y; }
public static integer subtract(integer x, integer y) { return x - y; }
public static integer add(integer x, integer y) { return x + y; }
public static int Compare(integer x, integer y) { if (x.sign == y.sign) { int xl = x.length; int yl = y.length; if (xl == yl) { for (int i = xl - 1; i >= 0; i--) { if (x.data[i] == y.data[i]) continue; return x.data[i] > y.data[i] ? x.sign : -x.sign; } return 0; } else { return xl > yl ? +x.sign : -x.sign; } } else { return x.sign > y.sign ? +1 : -1; } }