According to Google Calculator (-13) % 64 is 51.
(-13) % 64
51
According to Javascript (see this JSBin) it is -13.
-13
How do I fix this
If x is an integer and n is a power of 2, you can use x & (n - 1) instead of x % n.
x
n
x & (n - 1)
x % n
> -13 & (64 - 1) 51