In general, trying to perform operations which truncate on negative numbers is fraught with peril, because everyone seems to do it differently and you need to look it up. Another one that tends to bite people is the modulus operator where either operand is negative: programming languages tend to differ on what the result should be. For example:
$ echo "print(-7 % 3)" | python3
2
$ echo "print(-7 % -3)" | python3
-1
$ echo "System.out.println(-7 % 3)" | jshell -
-1
$ echo "System.out.println(-7 % -3)" | jshell -
-1