private static long remainder ( long dividend , long divisor ) { if ( divisor < 0 ) { // i.e., divisor >= 2^63: if ( compareUnsigned ( dividend , divisor ) < 0 ) { return dividend ; // dividend < divisor } else { return dividend - divisor ; // dividend >= divisor } } // Optimization - use signed modulus if dividend < 2^63 if ( dividend >= 0 ) { return dividend % divisor ; } /* * Otherwise, approximate the quotient, check, and correct if necessary. Our approximation is guaranteed to be * either exact or one less than the correct value. This follows from fact that floor(floor(x)/i) == floor(x/i) * for any real x and integer i != 0. The proof is not quite trivial. */ long quotient = ( ( dividend >>> 1 ) / divisor ) << 1 ; long rem = dividend - quotient * divisor ; return rem - ( compareUnsigned ( rem , divisor ) >= 0 ? divisor : 0 ) ; }
Returns dividend % divisor where the dividend and divisor are treated as unsigned 64 - bit quantities .