Implementing the division operator

S

Sri

Hi,

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

Thanks,
Sri
 
M

Michael Mair

Sri said:
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
 
O

osmium

Sri said:
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.
 
S

Sri

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

Sri

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
 
S

Sri

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

osmium

Sri said:
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.
 
T

Thad Smith

Sri said:
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.
 
S

Sri

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. "
 
V

void * clvrmnky()

Sri said:
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. "
And do try not to top-post.

There really are few rules on USENET.
 
P

pete

Sri said:
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!

unsigned fs_div(unsigned x, unsigned y)
{
unsigned a, b, q, counter;

q = 0;
if (y != 0) {
while (x >= y) {
a = x >> 1;
b = y;
counter = 1;
while (a >= b) {
b <<= 1;
counter <<= 1;
}
x -= b;
q += counter;
}
}
return q;
}
 
S

Sri

Thanks pete!
unsigned fs_div(unsigned x, unsigned y)
{
unsigned a, b, q, counter;

q = 0;
if (y != 0) {
while (x >= y) {
a = x >> 1;
b = y;
counter = 1;
while (a >= b) {
b <<= 1;
counter <<= 1;
}
x -= b;
q += counter;
}
}
return q;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top