473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 12399
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sebastian Haase | last post by:
Hi, I'm interested in having more people in our lab using numarray/NumPy instead of MatLab. For that I have put together a couple useful modules and written many myself. But then I got reminded of...
24
by: Teis Draiby | last post by:
In .NET, can I be sure that the result of a division between two integers always is truncated rather that rounded to nearest? Example: 99 / 50 = 1 regards, Teis
1
by: akickdoe22 | last post by:
Please help me finish this program. i have completed the addition and the subtraction parts, but i am stuck on the multiplication and division. any suggestions, hints, code, anyhting. it's not a...
17
by: seb.haase | last post by:
Hi, Is it true that that "Python 3000" is dead ? Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would just break to much code :-( On the otherhand I'm using Python as "Matlab...
10
by: Mike S | last post by:
Does anyone know the logic behind why in VB.NET the result of a floating-point division ('/') is -rounded- on being converted to an integer type, such as with statements like Dim x As Integer =...
2
by: kermit | last post by:
For a long time,, There has been a discussion of trueFor division versus integer division in Python. I myslef prefer that / be used for integer division since almost always, I want the...
94
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and...
1
by: youjay | last post by:
I've been out of perl for a while, so I am starting from scratch. I have a small applet which scans a set of directories, getting information from some files in each one, and displaying selected...
16
by: spl | last post by:
To increase the performance, how to change the / operator with bitwise operators? for ex: 25/5, 225/25 or 25/3 or any division, but I am not bothered of any remainder.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.