how abt subtracting . like a=a-b till a becomes zero . no of times subtraction is done is the answer . correct me if i am wrong !
On Thu, Aug 18, 2011 at 8:59 PM, Dave <[email protected]> wrote: > @Radha: You could simulate long division. It would look something like > this: > > int divide(int a, int b) > { > int i, k=0, q=0, s=1; > // error checking > if( b == 0 ) return 0 // return 0 for division by zero > // handle signs > if( a < 0 ) > { > a = -a; > s = -1; > } > if( b < 0 ) > { > b = -b; > s = -s; > } > // quick cases > if( a < b ) > return 0; > if( a == b ) > return s; > // shift divisor to align with dividend > while( b < a ) > { > b <<= 1; > ++k; > } > // perform k steps of long division in binary > for( i = 0 ; i < k ; ++i ) > { > q <<= 1; > b >>= 1; > if( a > b ) > { > a -= b; > q |= 1; > } > } > // apply sign to result > if( s < 0 ) > q = -q; > > return q; > } > > Dave > > On Aug 18, 8:56 am, radha krishnan <[email protected]> > wrote: > > how to do using BIT manipulation ? > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
