# Java solution 13ms

• ``````public int divide(int dividend, int divisor) {
if(divisor==0){
return 0;
}
long div = Math.abs((long)dividend);
long divor = Math.abs((long)divisor);
int boundaryBit = getBoundaryBit(div,divor);
long result=0;
divor=divor<<boundaryBit;
while(boundaryBit>=0){
if(divor<=div){
result+=(1l<<boundaryBit);
div-=divor;
}
boundaryBit--;
divor=divor>>1;
}
if((dividend<0 && divisor>0)||(dividend>0 && divisor<0)){
result=-result;
}
else{
if(result==(long)Integer.MAX_VALUE+1){
result=Integer.MAX_VALUE;
}
}

return (int)result;
}
private int getBoundaryBit(long dividend, long divisor){
int bits =0;
while((divisor<<bits)<dividend){
bits++;
}
return bits;
}``````

• public class Solution {
public static int divide(int dividend, int divisor) {
if(dividend==0||divisor==0)
{
return 0;
}

``````    boolean flag=false;
flag=(dividend>0&&divisor<0)||(dividend<0&&divisor>0);
long dividend1=Math.abs((long)dividend);
long divisor1=Math.abs((long)divisor);
if(divisor1>dividend1)
return 0;
long result=0;
while(dividend1>=divisor1)
{
long sum=divisor1;
int count=1;
while(sum+sum<dividend1)
{
sum+=sum;
count+=count;
}
dividend1-=sum;
result+=count;
}
long final_result=flag?-result:result;
if(final_result>Integer.MAX_VALUE)
return Integer.MAX_VALUE;
else
return (int)final_result;

}
``````

}

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.