Implementing the division operator

Discussion in 'C Programming' started by Sri, May 1, 2006.

  1. Sri

    Sri Guest

    Hi,

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

    Thanks,
    Sri
    Sri, May 1, 2006
    #1
    1. Advertising

  2. Sri

    Michael Mair Guest

    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.
    Michael Mair, May 1, 2006
    #2
    1. Advertising

  3. Sri

    osmium Guest

    "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.
    osmium, May 1, 2006
    #3
  4. Sri

    Sri Guest

    Thanks Michael. This is *not* a *homework* assignment. This was shot at
    me by a friend and it was picking my brain.
    Sri, May 1, 2006
    #4
  5. Sri

    Sri Guest

    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
    Sri, May 1, 2006
    #5
  6. Sri

    Sri Guest

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

    osmium Guest

    "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.
    osmium, May 1, 2006
    #7
  8. Sri

    Thad Smith Guest

    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
    Thad Smith, May 1, 2006
    #8
  9. Sri

    Sri Guest

    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, May 1, 2006
    #9
  10. 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.
    void * clvrmnky(), May 1, 2006
    #10
  11. Sri

    pete Guest

    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
    pete, May 1, 2006
    #11
  12. Sri

    Default User Guest

    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.
    Default User, May 1, 2006
    #12
  13. Sri

    Sri Guest

    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.
    Sri, May 1, 2006
    #13
  14. Sri

    Sri Guest

    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
    Sri, May 2, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    94
    Views:
    4,390
    ¬a\\/b
    Feb 9, 2007
  2. Replies:
    1
    Views:
    955
    Alf P. Steinbach
    Jan 19, 2007
  3. spl

    How to replace /(division) operator

    spl, Apr 25, 2008, in forum: C Programming
    Replies:
    14
    Views:
    1,567
    Tomás Ó hÉilidhe
    May 3, 2008
  4. bhargava.ram

    Implementing division operation.

    bhargava.ram, Feb 18, 2010, in forum: VHDL
    Replies:
    0
    Views:
    1,110
    bhargava.ram
    Feb 18, 2010
  5. Michael Neumann
    Replies:
    29
    Views:
    340
    Michael Neumann
    Jun 11, 2004
Loading...

Share This Page