Skip to content

Latest commit

 

History

History
14 lines (10 loc) · 659 Bytes

2020-08-18_shortcut-for-modulo-of-a-power-of-two.md

File metadata and controls

14 lines (10 loc) · 659 Bytes

Shortcut for modulo of a power of two

I was reading the Redis source code, specifically the siphash.c file, as well as this Ruby version and I noticed that they both seemed to do achieve the same thing but differently. Redis used n & 7 while the Ruby version used n % 8

And it turns out that when you're trying to get the power of two modulo of a number, you can instead do n & (m-1) where m is the power of two.

So it works with 2, 4, 8, 16, 32, 64, ... you get it

More info on StackOverflow: https://stackoverflow.com/questions/6670715/mod-of-power-2-on-bitwise-operators