473,385 Members | 1,427 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,385 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 12396
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.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.