We want to calculate x/240
, and to do that, we calculate (x*34953)>>23
. This only works for sufficiently small values of x.
The value of 34953 looks like some kind of magic constant. It is really just ceil((1 << 23)/240)
—the value 1/240
, scaled up by 2^23. As you use larger scaling factors (above 1<<23
), the value becomes more accurate, but you need more bits.
GCC can do this optimization for you under certain circumstances. GCC will convert x * 34953
to the same sequence of shifts and adds, if you are targeting an old processor like a 68000.