Hacker News new | past | comments | ask | show | jobs | submit login

Using &, rather than % is important, if someone used signed types for index for example, then % would've generate division, while & would still keep masking it - the example uses unsigned.

Also even if it was unsigned, but % was used then people might have the idea that any value could be used, while if & was used - it's more clear (at least to me) that power of 2 - 1 mask is needed - it just brings the right message.

The only case where % would've been used in similar fashion is in a hash-table, where the number of elements in the current bucket is prime number.




It reminds me also how clojure has recur, for tail-calls, rather than rely on the user to write code that would do so, which started as limitation of what the Java VM could do, but now it turns out that this design is better - as the intention is - make sure that the code is tail-call recursive, rather than relying on programmer or compiler to do it right.

In the same spirit - & rather than % for that example.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: