Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Implementing the division operator

Reply
Thread Tools

Implementing the division operator

 
 
Sri
Guest
Posts: n/a
 
      05-01-2006
Hi,

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

Thanks,
Sri

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      05-01-2006
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.
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      05-01-2006
"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.


 
Reply With Quote
 
Sri
Guest
Posts: n/a
 
      05-01-2006
Thanks Michael. This is *not* a *homework* assignment. This was shot at
me by a friend and it was picking my brain.

 
Reply With Quote
 
Sri
Guest
Posts: n/a
 
      05-01-2006
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

 
Reply With Quote
 
Sri
Guest
Posts: n/a
 
      05-01-2006
Can't we do better than repeated subtraction? Could we say
powf(10, (log(dividend) - log(divisor));

 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      05-01-2006
"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.


 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      05-01-2006
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
 
Reply With Quote
 
Sri
Guest
Posts: n/a
 
      05-01-2006
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.


 
Reply With Quote
 
void * clvrmnky()
Guest
Posts: n/a
 
      05-01-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing division operation. bhargava.ram VHDL 0 02-18-2010 10:02 AM
How to replace /(division) operator spl C Programming 14 05-03-2008 12:24 PM
division by 7 without using division operator krypto.wizard@gmail.com C Programming 94 02-09-2007 06:57 AM
Division by 7 without using the divide operator Justin.Velazquez@gmail.com C++ 1 01-19-2007 09:32 AM
[RCR] Floating point division operator /. (or fdiv method) Michael Neumann Ruby 29 06-11-2004 09:48 AM



Advertisments