It's a shame, shame, shame, shame, shame, ...

SHAME ON ME! And on C, too!

If I'm ever going to design a programming language, the expression

(index == selected & (totalsize - 1))

is going to translate into

(index == (selected & (totalsize - 1)))

, not into

((index == selected) & (totalsize - 1))

I'm really ashamed I came up with this kind of optimisation for calculating a modulo for a number which is a power of 2 (which the compiler couldn't know). When you really optimize for speed on platforms where you still unroll your code by hand, you sometimes lose sight of clarity. (But that's not an excuse. Big SVN Blame to myself!) I also should have unit tested this earlier.

Now, time to pass the blame on to C!

If I'm ever going to design a programming language, it will also have an interactive interpreter for evaluating expressions. Actually, I tried to test the expression in Python's interactive shell, which has a more sensible operator precedence. (Or is it just that it makes a difference between numbers and truth values, unlike C?) I probably would have searched for a few more hours, if Steffen hadn't pointed out the issue. (Thanks!)

>>> a = 30
>>> b = 30+64
>>> a == b
>>> a == (b % 64)
>>> a == b & (64-1)
>>> c = 31
>>> c == b & (64-1)

Oh, wasted time! The weather was so beautiful today!