Re: integer divide by 7 trick?

Discussion in 'C++' started by yvan joffre, Jul 25, 2003.

  1. yvan joffre

    yvan joffre Guest

    I don't understand the meaning of "extra" in your code.

    Why do not write just this:

    unsigned int SeqDiv7(unsigned int val) {
    unsigned int v=val, result=0;
    while(v > 0) {
    v >>= 3;
    result += v;
    }
    return result;
    }

    (because
    x / 7 = x / 8 + x / 64 + x / 512 + ... + x / (2^(i * 3)) + ...
    )

    I think it should work for any value
    yvan joffre, Jul 25, 2003
    #1
    1. Advertising

  2. yvan joffre

    Roy Smith Guest

    (yvan joffre) wrote:
    > Why do not write just this:
    >
    > unsigned int SeqDiv7(unsigned int val) {
    > unsigned int v=val, result=0;
    > while(v > 0) {
    > v >>= 3;
    > result += v;
    > }
    > return result;
    > }


    Why the intermediate variable v? Why not just use val directly in the
    body of the function?
    Roy Smith, Jul 25, 2003
    #2
    1. Advertising

  3. On 25/7/03 12:07 am (UK time), yvan joffre let loose these words:

    > I don't understand the meaning of "extra" in your code.


    The remainders after shifting, summed up modulo 7.

    > Why do not write just this:

    <snip>
    > (because
    > x / 7 = x / 8 + x / 64 + x / 512 + ... + x / (2^(i * 3)) + ...
    > )
    >
    > I think it should work for any value


    Try it and see.

    Stewart.

    --
    My e-mail is valid but not my primary mailbox. Please keep replies on
    on the 'group where everyone may benefit.
    Stewart Gordon, Jul 25, 2003
    #3
  4. On 25/7/03 2:18 am (UK time), Roy Smith let loose these words:

    > (yvan joffre) wrote:
    >
    >>Why do not write just this:
    >>
    >>unsigned int SeqDiv7(unsigned int val) {
    >> unsigned int v=val, result=0;
    >> while(v > 0) {
    >> v >>= 3;
    >> result += v;
    >> }
    >> return result;
    >>}

    >
    >
    > Why the intermediate variable v? Why not just use val directly in the
    > body of the function?


    Not sure. Guess that to me, val means what was actually passed into the
    function, rather than some modification of it.

    Speaking of which, do most compilers tend to generate the same code
    either way? (g++ 3.1 (Mac OS X) is giving me the exact same except for
    one byte, and they seem to take the same amount of time.)

    Stewart.

    --
    My e-mail is valid but not my primary mailbox. Please keep replies on
    on the 'group where everyone may benefit.
    Stewart Gordon, Jul 25, 2003
    #4
    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. MG
    Replies:
    5
    Views:
    420
    Christian Bau
    Jul 22, 2003
  2. Robert W Hand

    Re: integer divide by 7 trick?

    Robert W Hand, Jul 20, 2003, in forum: C++
    Replies:
    12
    Views:
    667
    Paul Hsieh
    Jul 25, 2003
  3. JS
    Replies:
    1
    Views:
    316
    Peter van Merkerk
    Jul 22, 2003
  4. MG

    Re: integer divide by 7 trick?

    MG, Jul 20, 2003, in forum: C Programming
    Replies:
    5
    Views:
    3,900
    Christian Bau
    Jul 22, 2003
  5. Robert W Hand

    Re: integer divide by 7 trick?

    Robert W Hand, Jul 20, 2003, in forum: C Programming
    Replies:
    12
    Views:
    600
    Paul Hsieh
    Jul 25, 2003
Loading...

Share This Page