Monday, April 11, 2011

Division of two numbers without using divison operator




/* Division function Computes the quotient and remainder of two numbers
 using bit shifting */
 
int division(int t_dividend, int t_divisor) {
   
    
int quotient = 1;
       
    
if (t_divisor == t_dividend) {
        remainder
= 0;
       
return 1;
    
} else if (t_dividend < t_divisor) {
        remainder
= t_dividend;
       
return 0;
    
}  
   
    
while (t_divisor <= t_dividend) {
       
/* Here divisor <>
           divisor and quotient */
       
t_divisor = t_divisor << 1;
        quotient = quotient << 1;
     
    
}
   
     /* We have reached the point where divisor > dividend,
     therefore divide divisor and quotient by two */
    
t_divisor = t_divisor >> 1;
     quotient = quotient >> 1;

   
     /* Call division recursively for the difference to get the
     exact quotient */
    
quotient = quotient + division(t_dividend - t_divisor,divisor);
   
    
return quotient;
 }


No comments: