Hi,
Is there anyway I can implement division in C without using the '/'
operator? Can I use bit aritmetic or such?
Thanks,
Sri 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.
"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.
Thanks Michael. This is *not* a *homework* assignment. This was shot at
me by a friend and it was picking my brain.
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
Can't we do better than repeated subtraction? Could we say
powf(10, (log(dividend) - log(divisor));
"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.
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
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.
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.
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
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.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |