@dave: in your algorithm, I have a doubt in the second loop( for loop ). q=0 initially so the first q<<1 stays zero and then q|=1 makes q=1 now. 1 then becomes x 2 and then again with the OR 2 becomes 3. 3 becomes 6 and with the OR 6 becomes 7. for example if i need to do 24/3, according to the code k=3 after while loop and then the for loop terminates with q as 7 as i mentioned the steps above.Have i got the understanding of the code wrong?
On Thu, Aug 18, 2011 at 7:18 PM, Dave <dave_and_da...@juno.com> wrote: > @Dheeraj: What about it? It doesn't give the quotient. What is it > supposed to do? > > Dave > > On Aug 18, 11:06 am, DheerajSharma <dheerajsharma1...@gmail.com> > wrote: > > wat about shifting 'a' right by floar(log2(b)) and adding 1 to it.. > > > > On Aug 18, 8:48 pm, aditya kumar <aditya.kumar130...@gmail.com> wrote: > > > > > > > > > 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 <dave_and_da...@juno.com> 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 <radhakrishnance...@gmail.com> > > > > 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 algogeeks@googlegroups.com. > > > > To unsubscribe from this group, send email to > > > > algogeeks+unsubscr...@googlegroups.com. > > > > 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 algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- "People often say that motivation doesn't last. Well, neither does bathing - that's why we recommend it daily." -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.