Velocity Reviews > Implementing the division operator

# Implementing the division operator

Sri
Guest
Posts: n/a

 05-01-2006
Hi,

Is there anyway I can implement division in C without using the '/'
operator? Can I use bit aritmetic or such?

Thanks,
Sri

Michael Mair
Guest
Posts: n/a

 05-01-2006
Sri schrieb:
> Is there anyway I can implement division in C without using the '/'
> operator?

Yes:

int divide (int dividend, int divisor)
{
int sign = 1;
int result = 0;
if (dividend == 0)
return 0;
else if (dividend > 0) {
sign *= -1;
dividend *= -1;
}
if (divisor == 0) {
/* handle error */
}
else if (divisor > 0) {
sign *= -1;
divisor *= -1;
}
while (dividend < 0) {
dividend -= divisor;
++result;
}
return result;
}

> Can I use bit aritmetic or such?

If you want to.

If this is a homework assignment or similar, please state so.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.

osmium
Guest
Posts: n/a

 05-01-2006
"Sri" writes:

> Is there anyway I can implement division in C without using the '/'
> operator? Can I use bit aritmetic or such?

Yes, you could use repeated subtraction, which is what division is really
about. You could be even more obscure and use bitwise operations if you
wished.

Sri
Guest
Posts: n/a

 05-01-2006
Thanks Michael. This is *not* a *homework* assignment. This was shot at
me by a friend and it was picking my brain.

Sri
Guest
Posts: n/a

 05-01-2006
Can't we be more efficient? If I were to divide 100,000 by 2, it will
make 50,000 iterations of the while loop!

- Sri

Sri
Guest
Posts: n/a

 05-01-2006
Can't we do better than repeated subtraction? Could we say
powf(10, (log(dividend) - log(divisor));

osmium
Guest
Posts: n/a

 05-01-2006
"Sri" writes:

> Can't we do better than repeated subtraction? Could we say
> powf(10, (log(dividend) - log(divisor));

Please use Usenet protocol and quote the message to which you referring.
The people at Google have invented their own perverse, poorly thought out,
method and tried to superimpose it on Usenet.

Guest
Posts: n/a

 05-01-2006
Sri wrote:
> Can't we be more efficient? If I were to divide 100,000 by 2, it will
> make 50,000 iterations of the while loop!

Sure, you can use the normal bit-per-cycle algorithm using shift,
compare, and subtract. You have to be careful to avoid overflows.

--

Sri
Guest
Posts: n/a

 05-01-2006
I was referring to this:

"Yes, you could use repeated subtraction, which is what division is
really
about. You could be even more obscure and use bitwise operations if
you
wished. "

> Please use Usenet protocol and quote the message to which you referring.
> The people at Google have invented their own perverse, poorly thought out,
> method and tried to superimpose it on Usenet.

void * clvrmnky()
Guest
Posts: n/a

 05-01-2006
Sri wrote:
> I was referring to this:
>
> "Yes, you could use repeated subtraction, which is what division is
> really
> about. You could be even more obscure and use bitwise operations if
> you
> wished. "
>
>> Please use Usenet protocol and quote the message to which you referring.
>> The people at Google have invented their own perverse, poorly thought out,
>> method and tried to superimpose it on Usenet.

>

And do try not to top-post.

There really are few rules on USENET.