Divide two integers without using multiplication, division and mod operator.
public class Solution { public int divide(int dividend, int divisor) { int sign = (dividend < 0) ? -1 : 1; sign = (divisor < 0)? sign *(-1) : sign; long absDivd = Math.abs((long) dividend); long absDivs = Math.abs((long) divisor); long result = 0; //should be long if(absDivd < absDivs) return 0; else{ long temp = 1; //should be long while(absDivd - absDivs >= absDivs){ temp <<= 1; absDivs <<= 1; } while(temp > 0){ result <<= 1; if(absDivd >= absDivs){ result += 1; absDivd -= absDivs; } absDivs >>= 1; temp >>= 1; } } return (sign < 0) ? (int) (0 - result) : (int) (result); } }