public static long divideUnsignedLong(long dividend, long divisor) {
    if (divisor < 0L) {

      // Answer must be 0 or 1 depending on relative magnitude
      // of dividend and divisor.
      return (compareUnsignedLong(dividend, divisor)) < 0 ? 0L : 1L;
    }

    if (dividend >= 0) { // Both inputs non-negative
      return dividend / divisor;
    } else {

      // simple division.
      // Yes, we should do something like this:
      // http://www.hackersdelight.org/divcMore.pdf
      // but later... (anyway this will be eventually replaced by
      // intrinsics in Java 8)

      // an equivalent algorithm exists in
      // com.google.common.primitives.UnsignedLongs
      long quotient = ((dividend >>> 1L) / divisor) << 1L;
      long remainder = dividend - quotient * divisor;
      if (compareUnsignedLong(remainder, divisor) >= 0) {
        return quotient + 1;
      }
      return quotient;
    }
  }
If we can assume JDK 1.8, this should use java.lang.Long.divideUnsigned(),
which will be replaced with intrinsics in JVM.

@param dividend
         the value to be divided
@param divisor
         the value doing the dividing
@return the unsigned quotient of the first argument divided by the second
        argument
@see "http://hg.openjdk.java.net/jdk8/tl/jdk/rev/71200c517524"
common/src/java/org/apache/hadoop/hive/common/type/SqlMathUtil.java