The Mythical Modulo Mask

from blog Psychosomatic, Lobotomy, Saw, | ↗ original
It is an optimisation well known to those who know it well that % of power of 2 numbers can be replaced by a much cheaper AND operator to the same power of 2 - 1. E.g: x % 8 == x & (8 - 1) [4/11/2014 NOTE] This works because the binary representation for N which is a power of 2 will have a single bit set to 1 and (N-1) will have all the bits...