MutableBigInteger divideKnuth(MutableBigInteger b,MutableBigInteger quotient,boolean needRemainder){
  if (b.intLen == 0)   throw new ArithmeticException("BigInteger divide by zero");
  if (intLen == 0) {
    quotient.intLen=quotient.offset=0;
    return needRemainder ? new MutableBigInteger() : null;
  }
  int cmp=compare(b);
  if (cmp < 0) {
    quotient.intLen=quotient.offset=0;
    return needRemainder ? new MutableBigInteger(this) : null;
  }
  if (cmp == 0) {
    quotient.value[0]=quotient.intLen=1;
    quotient.offset=0;
    return needRemainder ? new MutableBigInteger() : null;
  }
  quotient.clear();
  if (b.intLen == 1) {
    int r=divideOneWord(b.value[b.offset],quotient);
    if (needRemainder) {
      if (r == 0)       return new MutableBigInteger();
      return new MutableBigInteger(r);
    }
 else {
      return null;
    }
  }
  if (intLen >= KNUTH_POW2_THRESH_LEN) {
    int trailingZeroBits=Math.min(getLowestSetBit(),b.getLowestSetBit());
    if (trailingZeroBits >= KNUTH_POW2_THRESH_ZEROS * 32) {
      MutableBigInteger a=new MutableBigInteger(this);
      b=new MutableBigInteger(b);
      a.rightShift(trailingZeroBits);
      b.rightShift(trailingZeroBits);
      MutableBigInteger r=a.divideKnuth(b,quotient);
      r.leftShift(trailingZeroBits);
      return r;
    }
  }
  return divideMagnitude(b,quotient,needRemainder);
}

MutableBigInteger divideKnuth ( MutableBigInteger b , MutableBigInteger quotient , boolean needRemainder ) { if ( b . intLen == 0 ) throw new ArithmeticException ( "BigInteger divide by zero" ) ; if ( intLen == 0 ) { quotient . intLen = quotient . offset = 0 ; return needRemainder ? new MutableBigInteger ( ) : null ; } int cmp = compare ( b ) ; if ( cmp < 0 ) { quotient . intLen = quotient . offset = 0 ; return needRemainder ? new MutableBigInteger ( this ) : null ; } if ( cmp == 0 ) { quotient . value [ 0 ] = quotient . intLen = 1 ; quotient . offset = 0 ; return needRemainder ? new MutableBigInteger ( ) : null ; } quotient . clear ( ) ; if ( b . intLen == 1 ) { int r = divideOneWord ( b . value [ b . offset ] , quotient ) ; if ( needRemainder ) { if ( r == 0 ) return new MutableBigInteger ( ) ; return new MutableBigInteger ( r ) ; } else { return null ; } } if ( intLen >= KNUTH_POW2_THRESH_LEN ) { int trailingZeroBits = Math . min ( getLowestSetBit ( ) , b . getLowestSetBit ( ) ) ; if ( trailingZeroBits >= KNUTH_POW2_THRESH_ZEROS * 32 ) { MutableBigInteger a = new MutableBigInteger ( this ) ; b = new MutableBigInteger ( b ) ; a . rightShift ( trailingZeroBits ) ; b . rightShift ( trailingZeroBits ) ; MutableBigInteger r = a . divideKnuth ( b , quotient ) ; r . leftShift ( trailingZeroBits ) ; return r ; } } return divideMagnitude ( b , quotient , needRemainder ) ; }
calculates the quotient of this div b and places the quotient in the provided mutablebiginteger objects and the remainder object is returned . uses algorithm d in knuth section 4 . 3 . 1 . many optimizations to that algorithm have been adapted from the colin plumb c library . it special cases one word divisors for speed . the content of b is not changed .
