**1 Feb 2007** (updated 1 Feb 2007 at 15:59 UTC)

»
fejj: the standard way to do
that is to
replace the division with a multiplication by the
reciprocal, multiplied by some power of 2. Then divide the
result by that power of 2, which can be done with a right
shift. In the case of division by 7, you'd end up with the
increasingly more accurate expressions:

`
`

`q = ((a<<3) + a) >> 6
`

`q = ((a<<6) + (a<<3) + a) >> 9
`

`q = ((a<<9) + (a<<6) + (a<<3) + a)
>> 12
`

`q = ((a<<12) + (a<<9) + (a<<6) +
(a<<3) + a) >> 15
`

`...
`

and so on (in binary, 1/7 is .001001001...).
Optionally,
the result can be fixed with something like

`
r = a - q*7; while (r >= 7) { q++; r -= 7; }
`

The multiplication by 7 can be replaced with shifts
and
adds, of course.