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

Answer: 
Both definitions of modulus of negative numbers are in use - some languages use one definition and some the other.

If you want to get a negative number for negative inputs then you can use this:

```
int r = x % n;
if (r > 0 && x < 0)
{
    r -= n;
}

```

Likewise if you were using a language that returns a negative number on a negative input and you would prefer positive:

```
int r = x % n;
if (r < 0)
{
    r += n;
}

```
**Note that this answer was for a previous, different version of the question.**

Your result is wrong for Java.
Please provide some context how you arrived at it (your program, implementation and version of Java).

From the [Java Language Specification](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.3)

> 
> 15.17.3 Remainder Operator %
>   
> 
> [...]
>   
> 
> The remainder operation for operands that are integers after binary numeric promotion (§5.6.2) produces a result value such that (a/b)\*b+(a%b) is equal to a.
> 

> 
> 15.17.2 Division Operator /  
> 
> [...]  
> 
> Integer division rounds toward 0.
> 

Since / is rounded towards zero (resulting in zero), the result of % should be negative in this case.