Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Division by 7 without using the divide operator

Reply
Thread Tools

Division by 7 without using the divide operator

 
 
Justin.Velazquez@gmail.com
Guest
Posts: n/a
 
      01-19-2007
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-19-2007
* :
> 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=(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?
 
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
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
Implementing the division operator Sri C Programming 13 05-02-2006 08:27 AM
How to calculate size of an int without using the sizeof operator but using bitwise operator Manish_Ganvir C Programming 13 02-14-2005 07:24 PM
Unsupported error,& Right operand of "Divide" operator must be a power of 2.. Kim JM VHDL 1 04-02-2004 06:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57