Division by 7 without using the divide operator

Discussion in 'C++' started by Justin.Velazquez@gmail.com, Jan 19, 2007.

  1. Guest

    Hello everyone,

    I'm not really new to programming but my bitwise skills definately need
    work.
    I came across a problem I've been trying to figure out for fun. I'm
    trying to write
    a routine that will divide a number by 7 without using the divide
    operator. I
    figured how to multiply by 7 using bitwise operators ( (x << 3) - x )
    but I having
    a hard time trying to figure a bitwise approach to do the divide. The
    closed I've
    been able to come is:

    ((x>> 3) + x) >> 3

    This works for most cases but not all. Am I approaching this problem
    wrong?
    is it possible to divide by 7 using bitwise operators? Any advice or
    help would be
    greatly appreciated.

    Thanks
    justin
     
    , Jan 19, 2007
    #1
    1. Advertising

  2. * :
    > Hello everyone,
    >
    > I'm not really new to programming but my bitwise skills definately need
    > work.
    > I came across a problem I've been trying to figure out for fun. I'm
    > trying to write
    > a routine that will divide a number by 7 without using the divide
    > operator. I
    > figured how to multiply by 7 using bitwise operators ( (x << 3) - x )
    > but I having
    > a hard time trying to figure a bitwise approach to do the divide. The
    > closed I've
    > been able to come is:
    >
    > ((x>> 3) + x) >> 3
    >
    > This works for most cases but not all. Am I approaching this problem
    > wrong?
    > is it possible to divide by 7 using bitwise operators? Any advice or
    > help would be
    > greatly appreciated.


    Your draft solution can be approximated as (x/8+x)/8 = (9x/8)/8=(9/64)x.
    Since 9 decimal = 1001 binary, and since 1/64 = 0.000001 binary, that
    is essentially the same as multiplying x by 1001*0.000001 = 0.001001
    binary, wheras 1/7 = 0.0010010010010010... binary. Perhaps you can see
    a pattern both in that expansion and in your first draft solution?

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 19, 2007
    #2
    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. Kim JM
    Replies:
    1
    Views:
    1,718
    Riyaz
    Apr 2, 2004
  2. Manish_Ganvir
    Replies:
    13
    Views:
    1,599
    Keith Thompson
    Feb 14, 2005
  3. Sri

    Implementing the division operator

    Sri, May 1, 2006, in forum: C Programming
    Replies:
    13
    Views:
    4,144
  4. Replies:
    94
    Views:
    4,570
    ¬a\\/b
    Feb 9, 2007
  5. spl

    How to replace /(division) operator

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

Share This Page