aboutsummaryrefslogtreecommitdiff
path: root/divide_two_int.cpp
blob: 6afa1ea7086fb7e03126e4fbe7c9ab9be595f903 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "leetcode.h"

class Solution {
public:
  int divide(int dividend, int divisor) {
    if (dividend == INT_MIN && divisor == -1)
      return INT_MAX;
    long dvd = labs(dividend), dvs = labs(divisor), ans = 0;
    int sign = dividend > 0 ^ divisor > 0 ? -1 : 1;
    while (dvd >= dvs) {
      long temp = dvs, m = 1;
      while (temp << 1 <= dvd) {
        temp <<= 1;
        m <<= 1;
      }
      dvd -= temp;
      ans += m;
    }
    return sign * ans;
  }
};

int main() {}