rishu110067

Untitled

Jan 15th, 2022
863
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. long long int divide(long long int dividend, long long int divisor)
  2. {
  3.     int sign = 1;
  4.     if((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0))
  5.     {
  6.         sign = -1;
  7.     }
  8.      
  9.     if(dividend < 0) dividend = -dividend;
  10.     if(divisor < 0) divisor = -divisor;
  11.    
  12.     unsigned long long int d[64], p[64];
  13.     int last_index = 0;
  14.     p[0] = 1;
  15.     d[0] = divisor;
  16.     for(int i = 1; i < 64 && d[i-1] < dividend; i++)
  17.     {
  18.         d[i] = d[i-1] + d[i-1];
  19.         p[i] = p[i-1] + p[i-1];
  20.         last_index = i;
  21.     }
  22.    
  23.     unsigned long long int answer = 0;
  24.     for(int i =last_index; i >= 0; i--)
  25.     {
  26.         if(d[i] <= dividend)
  27.         {
  28.             dividend -= d[i];
  29.             answer += p[i];
  30.         }
  31.     }
  32.     if(sign == -1) return -answer;
  33.     else return answer;
  34. }
RAW Paste Data