Hacker News new | ask | show | jobs
by linknoid 2023 days ago
The simple trick with modulo 7 is take all the 3 bit sequences: (x & 111), (x & 111000) >> 3, (x & 111000000) >> 6, etc. and add them all together. It means you just have to do bitshift + add instead of all the complexity of division. Same applies to higher powers of 2^n-1 as well.
2 comments

You can also combine those shift/adds by multiplying by a 1001001001 pattern or perhaps some 1000001000001 patterns.
Sure. That's why I said it was the same as the mod 9 tricks in base 10.