content: package org.redquark.tutorials.leetcode;
  
  /**
   * @author Anirudh Sharma
   */
  public class DivideTwoIntegers {
  
      private static int divide(int dividend, int divisor) {
          // Check for overflow
          if (divisor == 0 || (dividend == Integer.MIN_VALUE && divisor == -1)) {
              return Integer.MAX_VALUE;
          }
          // Sign of result
          int sign = (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0) ? -1 : 1;
          // Quotient
          int quotient = 0;
          // Take the absolute value
          long absoluteDividend = Math.abs((long) dividend);
          long absoluteDivisor = Math.abs((long) divisor);
          // Loop until the  dividend is greater than divisor
          while (absoluteDividend >= absoluteDivisor) {
              // This represents the number of bits shifted or
              // how many times we can double the number
              int shift = 0;
              while (absoluteDividend >= (absoluteDivisor << shift)) {
                  shift++;
              }
              // Add the number of times we shifted to the quotient
              quotient += (1 << (shift - 1));
              // Update the dividend for the next iteration
              absoluteDividend -= absoluteDivisor << (shift - 1);
          }
          return sign == -1 ? -quotient : quotient;
      }
  
      public static void main(String[] args) {
          System.out.println(divide(10, 3));
          System.out.println(divide(7, -3));
          System.out.println(divide(0, 1));
          System.out.println(divide(1, 1));
          System.out.println(divide(Integer.MAX_VALUE, 1));
          System.out.println(divide(Integer.MIN_VALUE, 1));
      }
  }
  