Josh Gray wrote:
* Is there likely to be a tangible benefit to doing this or would a compiler (gcc in my case) make this optimisation?
VC++
compiler takes care of such optimization (I see no
div
operation).
I guess
gcc
do the same, you may verify it using the
-S
switch and looking at the assembly file produced.
Josh Gray wrote:
* Is it possible to perform a similar optimisation to the modulus operation when working with a known value that is a multiple of 2?
Yes, for instance
i = i & 7;
is equivalent to
i = i % 8;
for positive values of
i
(an advantage of letting the compiler optimize for you, is the automatically handling of the negative numbers too).
[added]
I assume
integer operation, of course.
[/added]
:)