📄 biginteger.java
字号:
public BigInteger or(BigInteger val) { BigInteger r = new BigInteger(); r.or0(this, val); return (r);}public BigInteger xor(BigInteger val) { BigInteger r = new BigInteger(); r.xor0(this, val); return (r);}public BigInteger not() { BigInteger r = new BigInteger(); r.not0(this); return (r);}public BigInteger andNot(BigInteger val) { BigInteger r = new BigInteger(); r.and0(this, val); r.not0(r); return (r);}public boolean testBit(int n) { checkIfBitAddressIsNotNegative(n); BigInteger b = new BigInteger(); b.setbit0(this, n); if (cmp0(b, this) == 0) { return (true); } else { return (false); }}public BigInteger setBit(int n) { checkIfBitAddressIsNotNegative(n); BigInteger r = new BigInteger(); r.setbit0(this, n); return (r);}public BigInteger clearBit(int n) { checkIfBitAddressIsNotNegative(n); BigInteger r = new BigInteger(); r.clrbit0(this, n); return (r);}public BigInteger flipBit(int n) { checkIfBitAddressIsNotNegative(n); BigInteger r = new BigInteger(); r.setbit0(r, n); r.xor0(r, this); return (r);}public int getLowestSetBit() { return (scansetbit0());}public int bitLength() { return bitLength0();}public int bitCount() { if (compareTo(ZERO) < 0) return (negate().hamDist0(ZERO)); else return (hamDist0(ZERO));}public boolean isProbablePrime(int certainty) { if (probablyPrime0(certainty) == 0) { return (false); } return (true);}public static BigInteger probablePrime(int bitLength, Random rnd) { return new BigInteger(bitLength, 100, rnd);}public int compareTo(Object obj) { return compareTo((BigInteger)obj);}public int compareTo(BigInteger val) { int r = cmp0(this, val); // compute sign since JDK spec asks us to return -1/0/1, // but the GMP doc does not guarantee that. return (r == 0) ? 0 : (r < 0) ? -1 : 1;}public boolean equals(Object o) { return (o instanceof BigInteger) && compareTo((BigInteger)o) == 0;}public BigInteger min(BigInteger val) { int r = compareTo(val); if (r > 0) { return (val); } else { return (this); }}public BigInteger max(BigInteger val) { int r = compareTo(val); if (r < 0) { return (val); } else { return (this); }}public int hashCode() { if (hash == 0) { int tempHash = 0; int sign = cmp0(this, ZERO); BigInteger copy = abs(), divisor = new BigInteger(); divisor.setbit0(divisor, 32); // prepare to shift right for (int i = bitLength() / 8 ; i > 4 ; i -= 4) { tempHash ^= copy.toInt0(); copy.div0(copy, divisor); } tempHash ^= copy.toInt0(); tempHash *= sign; hash = tempHash; // race condition here is ok } return hash;}public String toString(int radix) { if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX)) { radix = 10; } return (toString0(radix));}public String toString() { return (toString(10));}public byte[] toByteArray() { byte[] ret = new byte[1 + bitLength0()/8]; BigInteger copy = abs(), divisor = new BigInteger(); divisor.setbit0(divisor, 32); // prepare to shift right int sign = cmp0(this, ZERO); if (sign < 0) { copy.sub0(copy, ONE); // adjust two's complement } int i = ret.length; // we know it's >= 1 while (i > 4) { int num = copy.toInt0(); ret[--i] = (byte)num; ret[--i] = (byte)(num>>8); ret[--i] = (byte)(num>>16); ret[--i] = (byte)(num>>24); copy.div0(copy, divisor); } { int num = copy.toInt0(); switch (i) { case 4: ret[--i] = (byte)num; num >>= 8; // fall through case 3: ret[--i] = (byte)num; num >>= 8; // fall through case 2: ret[--i] = (byte)num; num >>= 8; // fall through case 1: ret[--i] = (byte)num; } } if (sign < 0) { i = ret.length; while (i-- > 0) { ret[i] = (byte)~ret[i]; // adjust two's complement } } return (ret);}public int intValue() { if (signum() < 0) { return -toInt0(); } else { return toInt0(); }}public long longValue() { long v = ((((long)abs().shiftRight(32).toInt0()) << 32) | (((long)toInt0())&0xffffffffL)); if (signum() < 0) { return -v; } else { return v; }}public float floatValue() { return ((float)doubleValue());}public double doubleValue() { return (toDouble0());}protected void finalize() throws Throwable { finalize0(); super.finalize();} /* serialized form is * int bitCount * * The bitCount of this BigInteger, as returned by * bitCount(), or -1 (either value is acceptable). * * int bitLength * * The bitLength of this BigInteger, as returned by bitLength(), * or -1 (either value is acceptable). * * int firstNonzeroByteNum * * The byte-number of the lowest-order nonzero byte in the * magnitude of this BigInteger, or -2 (either value is acceptable). * The least significant byte has byte-number 0, the next byte in * order of increasing significance has byte-number 1, and so forth. * * int lowestSetBit * * The lowest set bit of this BigInteger, as returned by * getLowestSetBit(), or -2 (either value is acceptable). * * byte[] magnitude * * The magnitude of this BigInteger, in big-endian byte-order: * the zeroth element of this array is the most-significant byte * of the magnitude. The magnitude must be "minimal" in that the * most-significant byte (magnitude[0]) must be non-zero. This is * necessary to ensure that there is exactly one representation for * each BigInteger value. Note that this implies that the BigInteger * zero has a zero-length magnitude array. * * int signum * * The signum of this BigInteger: -1 for negative, 0 for zero, * or 1 for positive. Note that the BigInteger zero must have a * signum of 0. This is necessary to ensures that there is exactly * one representation for each BigInteger value. */private void writeObject(ObjectOutputStream stream) throws IOException { ObjectOutputStream.PutField fieldMunger = stream.putFields(); fieldMunger.put("bitCount", (int)bitCount()); fieldMunger.put("bitLength", (int)bitLength()); fieldMunger.put("lowestSetBit", (int)getLowestSetBit()); fieldMunger.put("magnitude", (byte[])this.toByteArray()); fieldMunger.put("signum", (int)signum()); stream.writeFields();}private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException{ init0(); ObjectInputStream.GetField fieldMunger = stream.readFields(); // "bitCount" ignored // "bitLength" ignored // "lowestSetBit" ignored byte[] magnitude = (byte[])fieldMunger.get("magnitude", (byte[])null); int signum = fieldMunger.get("signum", (int)0); this.assignBytes0(signum, magnitude);} private native void init0();private native void finalize0();private native void assignBytes0(int s, byte[] m);private native int assignString0(String v, int i);private native void assignLong0(long v);private native void add0(BigInteger s1, BigInteger s2);private native void sub0(BigInteger s1, BigInteger s2);private native void mul0(BigInteger s1, BigInteger s2);private native void div0(BigInteger s1, BigInteger s2);private native void rem0(BigInteger s1, BigInteger s2);private native void abs0(BigInteger s);private native void neg0(BigInteger s);private native void pow0(BigInteger s, int p);private native void gcd0(BigInteger s1, BigInteger s2);private native void mod0(BigInteger s1, BigInteger s2);private native void modpow0(BigInteger s1, BigInteger s2, BigInteger s3);private native void modinv0(BigInteger s1, BigInteger s2);private native void and0(BigInteger s1, BigInteger s2);private native void or0(BigInteger s1, BigInteger s2);private native void xor0(BigInteger s1, BigInteger s2);private native void not0(BigInteger s);private native void clrbit0(BigInteger s, int n);private native void setbit0(BigInteger s, int n);private native int scansetbit0();private native int probablyPrime0(int cert);private native int bitLength0();private native int hamDist0(BigInteger s);private native static int cmp0(BigInteger s1, BigInteger s2);private native static void initialize0();private native static void divrem0(BigInteger r, BigInteger q, BigInteger s1, BigInteger s2);private native String toString0(int base);private native double toDouble0();private native int toInt0();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -