Question: Am I doing modulus wrong? Because in Java `-13 % 64` evaluates to `-13` but I want to get `51`.

Answer: 
`x = x + m = x - m` in modulus `m`.  

so `-13 = -13 + 64` in modulus `64` and `-13 = 51` in modulus `64`.  

assume `Z = X * d + r`, if `0 < r < X` then in division `Z/X` we call `r` the remainder.  

`Z % X` returns the remainder of `Z/X`.
In Java latest versions you get `-13%64 = -13`. The answer will always have sign of numerator.