Christopher Key wrote:
The most concise form that I've found so far is:
const int d = 8; // 16, 32 etc
x = y / d - ((y % d 0) ? 1 : 0)
although this still produces rather longer code (see example below).
Any integer divide by constant can be replaced by a sequence of
multiply, shift, and add
Hello,
Apologies if this has already been covered; I've searched the archives
and not found anything.
I have some code that needs to perform signed division by a power of two
with rounding towards minus infinity, i.e. it requires an arithmetic
right shift. Now in the C specification, right
Christopher Key wrote:
I have some code that needs to perform signed division by a power of two
with rounding towards minus infinity, i.e. it requires an arithmetic
right shift. Now in the C specification, right shifting a signed
integer is implementation defined.
Because C may be compiled
Tim Prince wrote:
Christopher Key wrote:
I have some code that needs to perform signed division by a power of two
with rounding towards minus infinity, i.e. it requires an arithmetic
right shift. Now in the C specification, right shifting a signed
integer is implementation defined.