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

Answer: 
Since "mathematically" both are correct: 

```
-13 % 64 = -13 (on modulus 64)  
-13 % 64 = 51 (on modulus 64)

```

One of the options had to be chosen by Java language developers and they chose:

> 
> the sign of the result equals the sign of the dividend.
> 
> 
> 

Says it in Java specs:

<https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.3>
**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.