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

Answer: 
To overcome this, you could add `64` (or whatever your modulus base is) to the negative value until it is positive

```
int k = -13;
int modbase = 64;

while (k < 0) {
    k += modbase;
}

int result = k % modbase;

```

The result will still be in the same equivalence class.
I don't think Java returns 51 in this case. I am running Java 8 on a Mac and I get:

```
-13 % 64 = -13

```

Program:

```
public class Test {
    public static void main(String[] args) {
        int i = -13;
        int j = 64;
        System.out.println(i % j);
    }
}

```