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
The % operator in JavaScript is the remainder operator, not the modulo operator (the main difference being in how negative numbers are treated):
%
-1 % 8 // -1, not 7