@Override public int hashCode() { if (hashCode != 0) { return hashCode; } prepareJavaRepresentation(); for (int i = 0; i < numberLength; ++i) { hashCode = hashCode * 33 + digits[i]; } hashCode = hashCode * sign; return hashCode; }
/** * Performs a fast bit testing for positive numbers. The bit to to be tested * must be in the range {@code [0, val.bitLength()-1]} */ static boolean testBit(BigInteger val, int n) { val.prepareJavaRepresentation(); // PRE: 0 <= n < val.bitLength() return ((val.digits[n >> 5] & (1 << (n & 31))) != 0); }
/** * Returns a copy of the current instance to achieve immutability */ BigInteger copy() { prepareJavaRepresentation(); int[] copyDigits = new int[numberLength]; System.arraycopy(digits, 0, copyDigits, 0, numberLength); return new BigInteger(sign, numberLength, copyDigits); }
/** * Returns a {@code BigInteger} whose value is {@code this & value}. * * <p><b>Implementation Note:</b> Usage of this method is not recommended * as the current implementation is not efficient. * * @param value value to be and'ed with {@code this}. * @throws NullPointerException if {@code value == null}. */ public BigInteger and(BigInteger value) { this.prepareJavaRepresentation(); value.prepareJavaRepresentation(); return Logical.and(this, value); }
/** * Returns a {@code BigInteger} whose value is {@code this | value}. * * <p><b>Implementation Note:</b> Usage of this method is not recommended as * the current implementation is not efficient. * * @param value value to be or'ed with {@code this}. * @throws NullPointerException if {@code value == null}. */ public BigInteger or(BigInteger value) { this.prepareJavaRepresentation(); value.prepareJavaRepresentation(); return Logical.or(this, value); }
/** * Returns a {@code BigInteger} whose value is {@code this ^ value}. * * <p><b>Implementation Note:</b> Usage of this method is not recommended as * the current implementation is not efficient. * * @param value value to be xor'ed with {@code this} * @throws NullPointerException if {@code value == null} */ public BigInteger xor(BigInteger value) { this.prepareJavaRepresentation(); value.prepareJavaRepresentation(); return Logical.xor(this, value); }
/** * Returns a {@code BigInteger} whose value is {@code this & ~value}. * Evaluating {@code x.andNot(value)} returns the same result as {@code * x.and(value.not())}. * * <p><b>Implementation Note:</b> Usage of this method is not recommended * as the current implementation is not efficient. * * @param value value to be not'ed and then and'ed with {@code this}. * @throws NullPointerException if {@code value == null}. */ public BigInteger andNot(BigInteger value) { this.prepareJavaRepresentation(); value.prepareJavaRepresentation(); return Logical.andNot(this, value); }
static BigInteger shiftLeftOneBit(BigInteger source) { source.prepareJavaRepresentation(); int srcLen = source.numberLength; int resLen = srcLen + 1; int[] resDigits = new int[resLen]; shiftLeftOneBit(resDigits, source.digits, srcLen); return new BigInteger(source.sign, resLen, resDigits); }
/** * Returns a {@code BigInteger} whose value is {@code ~this}. The result * of this operation is {@code -this-1}. * * <p><b>Implementation Note:</b> Usage of this method is not recommended as * the current implementation is not efficient. */ public BigInteger not() { this.prepareJavaRepresentation(); return Logical.not(this); }
/** * Returns the number of bits in the two's complement representation of * {@code this} which differ from the sign bit. If {@code this} is negative, * the result is equivalent to the number of bits set in the two's * complement representation of {@code -this - 1}. * * <p>Use {@code bitLength(0)} to find the length of the binary value in * bits. * * <p><b>Implementation Note:</b> Usage of this method is not recommended as * the current implementation is not efficient. */ public int bitCount() { prepareJavaRepresentation(); return BitLevel.bitCount(this); }
/** * Returns the position of the lowest set bit in the two's complement * representation of this {@code BigInteger}. If all bits are zero (this==0) * then -1 is returned as result. * * <p><b>Implementation Note:</b> Usage of this method is not recommended as * the current implementation is not efficient. */ public int getLowestSetBit() { prepareJavaRepresentation(); if (sign == 0) { return -1; } // (sign != 0) implies that exists some non zero digit int i = getFirstNonzeroDigit(); return ((i << 5) + Integer.numberOfTrailingZeros(digits[i])); }
/** * Returns a {@code BigInteger} which has the same binary representation * as {@code this} but with the bit at position n flipped. The result is * equivalent to {@code this ^ pow(2, n)}. * * <p><b>Implementation Note:</b> Usage of this method is not recommended as * the current implementation is not efficient. * * @param n position where the bit in {@code this} has to be flipped. * @throws ArithmeticException if {@code n < 0}. */ public BigInteger flipBit(int n) { prepareJavaRepresentation(); if (n < 0) { throw new ArithmeticException("n < 0: " + n); } return BitLevel.flipBit(this, n); }
/** @see BigInteger#bitLength() */ static int bitLength(BigInteger val) { val.prepareJavaRepresentation(); if (val.sign == 0) { return 0; } int bLength = (val.numberLength << 5); int highDigit = val.digits[val.numberLength - 1]; if (val.sign < 0) { int i = val.getFirstNonzeroDigit(); // We reduce the problem to the positive case. if (i == val.numberLength - 1) { highDigit--; } } // Subtracting all sign bits bLength -= Integer.numberOfLeadingZeros(highDigit); return bLength; }
/** @see BigInteger#bitCount() */ static int bitCount(BigInteger val) { val.prepareJavaRepresentation(); int bCount = 0; if (val.sign == 0) { return 0; } int i = val.getFirstNonzeroDigit(); if (val.sign > 0) { for ( ; i < val.numberLength; i++) { bCount += Integer.bitCount(val.digits[i]); } } else {// (sign < 0) // this digit absorbs the carry bCount += Integer.bitCount(-val.digits[i]); for (i++; i < val.numberLength; i++) { bCount += Integer.bitCount(~val.digits[i]); } // We take the complement sum: bCount = (val.numberLength << 5) - bCount; } return bCount; }
source.prepareJavaRepresentation(); int intCount = count >> 5; // count of integers count &= 31; // count of remaining bits
/** * Returns this {@code BigInteger} as an int value. If {@code this} is too * big to be represented as an int, then {@code this % (1 << 32)} is * returned. */ @Override public int intValue() { if (nativeIsValid && bigInt.twosCompFitsIntoBytes(4)) { return (int) bigInt.longInt(); } this.prepareJavaRepresentation(); return (sign * digits[0]); }
/** * Returns this {@code BigInteger} as a long value. If {@code this} is too * big to be represented as a long, then {@code this % pow(2, 64)} is * returned. */ @Override public long longValue() { if (nativeIsValid && bigInt.twosCompFitsIntoBytes(8)) { return bigInt.longInt(); } prepareJavaRepresentation(); long value = numberLength > 1 ? ((long) digits[1]) << 32 | digits[0] & 0xFFFFFFFFL : digits[0] & 0xFFFFFFFFL; return sign * value; }
/** * Returns a {@code BigInteger} which has the same binary representation * as {@code this} but with the bit at position n cleared. The result is * equivalent to {@code this & ~pow(2, n)}. * * <p><b>Implementation Note:</b> Usage of this method is not recommended as * the current implementation is not efficient. * * @param n position where the bit in {@code this} has to be cleared. * @throws ArithmeticException if {@code n < 0}. */ public BigInteger clearBit(int n) { prepareJavaRepresentation(); if (testBit(n)) { return BitLevel.flipBit(this, n); } else { return this; } }
/** * Returns a {@code BigInteger} which has the same binary representation * as {@code this} but with the bit at position n set. The result is * equivalent to {@code this | pow(2, n)}. * * <p><b>Implementation Note:</b> Usage of this method is not recommended as * the current implementation is not efficient. * * @param n position where the bit in {@code this} has to be set. * @throws ArithmeticException if {@code n < 0}. */ public BigInteger setBit(int n) { prepareJavaRepresentation(); if (!testBit(n)) { return BitLevel.flipBit(this, n); } else { return this; } }
/** * Returns a string containing a string representation of this {@code * BigInteger} with base radix. If {@code radix < Character.MIN_RADIX} or * {@code radix > Character.MAX_RADIX} then a decimal representation is * returned. The characters of the string representation are generated with * method {@code Character.forDigit}. * * @param radix base to be used for the string representation. */ public String toString(int radix) { if (radix == 10) { return getBigInt().decString(); } else { prepareJavaRepresentation(); return Conversion.bigInteger2String(this, radix); } }