package leetcode151withexplain;

/**
 * ±¾ÌâÌâÒâ£º¸ø³öÁ½¸öÕûÊý  Çó³ö½á¹û  ²»ÄÜÓÃÒÑÓÐµÄ³Ë·¨   ³ö·¢ºÍÇóÓàº¯Êý
 * 
 *
 */
public class DivideTwoIntegers123 {
	
	public int divide(int dividend, int divisor) {
        long a = (long)Math.abs((long)dividend);  
        long b = (long)Math.abs((long)divisor);  
        long res = 0;
        
        while(a >= b)  
        {  
            long c = b;
            //through << to accelate the caluation
            //cÃ¿Ò»ÂÖ×óÒÆÒ»Î» ¾Í´ú±í³ËÒÔ2,ÄÇÃ´ÕâÀïÃæ¾Í°üº¬ÁË1<<i¸öb
            for(int i = 0; a >= c; i++, c <<=1)  
            {  
                a -= c;  
                res += 1<<i;  
            }  
        } 
        
        //¾ö¶¨Õý¸ººÅ
        boolean positiveOrNot = true;
        if(dividend > 0 && divisor < 0) positiveOrNot = false;
        if(dividend < 0 && divisor > 0) positiveOrNot = false;
        
        return positiveOrNot == true ? (int)(res) : (int)(-res);  
    }
}
