@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.

Reply via email to