468,484 Members | 1,540 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

Implementing the division operator

Sri
Hi,

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

Thanks,
Sri

May 1 '06 #1
13 12005
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.
May 1 '06 #2
"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.
May 1 '06 #3
Sri
Thanks Michael. This is *not* a *homework* assignment. This was shot at
me by a friend and it was picking my brain.

May 1 '06 #4
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

May 1 '06 #5
Sri
Can't we do better than repeated subtraction? Could we say
powf(10, (log(dividend) - log(divisor));

May 1 '06 #6
"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.
May 1 '06 #7
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.

--
Thad
May 1 '06 #8
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. "
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.


May 1 '06 #9
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.
May 1 '06 #10
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!


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;
}

--
pete
May 1 '06 #11
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!


See below.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
May 1 '06 #12
Sri
Will do so....Thanks!

Default User wrote:
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!


See below.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.


May 1 '06 #13
Sri
Thanks pete!

pete wrote:
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!


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;
}

--
pete


May 2 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sebastian Haase | last post: by
24 posts views Thread by Teis Draiby | last post: by
1 post views Thread by akickdoe22 | last post: by
17 posts views Thread by seb.haase | last post: by
2 posts views Thread by kermit | last post: by
94 posts views Thread by krypto.wizard | last post: by
1 post views Thread by youjay | last post: by
16 posts views Thread by spl | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.