/** @return sign = 0, magnitude = longer.magnitude | shorter.magnitude */ static BigInteger xorPositive(BigInteger longer, BigInteger shorter) { // PRE: longer and shorter are positive; // PRE: longer has at least as many digits as shorter int resLength = longer.numberLength; int[] resDigits = new int[resLength]; int i = Math.min(longer.getFirstNonzeroDigit(), shorter.getFirstNonzeroDigit()); for ( ; i < shorter.numberLength; i++) { resDigits[i] = longer.digits[i] ^ shorter.digits[i]; } for ( ; i < longer.numberLength; i++ ){ resDigits[i] = longer.digits[i]; } return new BigInteger(1, resLength, resDigits); }
/** @return sign = 1, magnitude = val.magnitude & that.magnitude*/ static BigInteger andPositive(BigInteger val, BigInteger that) { // PRE: both arguments are positive int resLength = Math.min(val.numberLength, that.numberLength); int i = Math.max(val.getFirstNonzeroDigit(), that.getFirstNonzeroDigit()); if (i >= resLength) { return BigInteger.ZERO; } int[] resDigits = new int[resLength]; for ( ; i < resLength; i++) { resDigits[i] = val.digits[i] & that.digits[i]; } return new BigInteger(1, resLength, resDigits); }
/** @return sign = 1, magnitude = val.magnitude & ~that.magnitude*/ static BigInteger andNotPositive(BigInteger val, BigInteger that) { // PRE: both arguments are positive int[] resDigits = new int[val.numberLength]; int limit = Math.min(val.numberLength, that.numberLength); int i; for (i = val.getFirstNonzeroDigit(); i < limit; i++) { resDigits[i] = val.digits[i] & ~that.digits[i]; } for ( ; i < val.numberLength; i++) { resDigits[i] = val.digits[i]; } return new BigInteger(1, val.numberLength, resDigits); }
/** @return sign = 1, magnitude = positive.magnitude & ~(-negative.magnitude)*/ static BigInteger andNotPositiveNegative(BigInteger positive, BigInteger negative) { // PRE: positive > 0 && negative < 0 int iNeg = negative.getFirstNonzeroDigit(); int iPos = positive.getFirstNonzeroDigit(); if (iNeg >= positive.numberLength) { return positive; } int resLength = Math.min(positive.numberLength, negative.numberLength); int[] resDigits = new int[resLength]; // Always start from first non zero of positive int i = iPos; for ( ; i < iNeg; i++) { // resDigits[i] = positive.digits[i] & -1 (~0) resDigits[i] = positive.digits[i]; } if (i == iNeg) { resDigits[i] = positive.digits[i] & (negative.digits[i] - 1); i++; } for ( ; i < resLength; i++) { // resDigits[i] = positive.digits[i] & ~(~negative.digits[i]); resDigits[i] = positive.digits[i] & negative.digits[i]; } return new BigInteger(1, resLength, resDigits); }
/** @return sign = -1, magnitude = -(-val.magnitude | -that.magnitude) */ static BigInteger orNegative(BigInteger val, BigInteger that){ // PRE: val and that are negative; // PRE: val has at least as many trailing zeros digits as that int iThat = that.getFirstNonzeroDigit(); int iVal = val.getFirstNonzeroDigit(); int i; if (iVal >= that.numberLength) { return that; }else if (iThat >= val.numberLength) { return val; } int resLength = Math.min(val.numberLength, that.numberLength); int[] resDigits = new int[resLength]; //Looking for the first non-zero digit of the result if (iThat == iVal) { resDigits[iVal] = -(-val.digits[iVal] | -that.digits[iVal]); i = iVal; } else { for (i = iThat; i < iVal; i++) { resDigits[i] = that.digits[i]; } resDigits[i] = that.digits[i] & (val.digits[i] - 1); } for (i++; i < resLength; i++) { resDigits[i] = val.digits[i] & that.digits[i]; } return new BigInteger(-1, resLength, resDigits); }
int iPos = positive.getFirstNonzeroDigit(); int iNeg = negative.getFirstNonzeroDigit();
/** * 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])); }
int iLonger = longer.getFirstNonzeroDigit(); int iShorter = shorter.getFirstNonzeroDigit();
int iVal = val.getFirstNonzeroDigit(); int iThat = that.getFirstNonzeroDigit();
int iVal = val.getFirstNonzeroDigit(); int iThat = that.getFirstNonzeroDigit(); int i = iThat; int limit;
/** @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; }
int iNeg = negative.getFirstNonzeroDigit(); int iPos = positive.getFirstNonzeroDigit(); int i; int limit;
/** @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; }
int digit; int iNeg = negative.getFirstNonzeroDigit(); int iPos = positive.getFirstNonzeroDigit();
} else { int firstNonZeroDigit = val.getFirstNonzeroDigit(); if (intCount > firstNonZeroDigit) { resDigits[intCount] ^= bitNumber;
int resLength = Math.max(negative.numberLength, positive.numberLength); int[] resDigits; int iNeg = negative.getFirstNonzeroDigit(); int iPos = positive.getFirstNonzeroDigit(); int i; int limit;
int iThis = getFirstNonzeroDigit(); int bytesLen = (bitLen >> 3) + 1;
n = (1 << (n & 31)); // int with 1 set to the needed position if (sign < 0) { int firstNonZeroDigit = getFirstNonzeroDigit(); if (intCount < firstNonZeroDigit) { return false;
if (that.sign > 0) { return orDiffSigns(that, val); } else if (that.getFirstNonzeroDigit() > val.getFirstNonzeroDigit()) { return orNegative(that, val); } else {
if (that.sign > 0) { return xorDiffSigns(that, val); } else if (that.getFirstNonzeroDigit() > val.getFirstNonzeroDigit()) { return xorNegative(that, val); } else {