Language Design: Binary Operators are Overused

Published on 2019-09-21.

Many languages provide binary operators, usually for operations on numbers (addition, multiplication), bits (shifts) and boolean values.

The problem with <<, >>, >>>

The problem with %

In most languages the % operator implements a remainder operation, not a modulo operation.

  • remainder: has the same sign as the dividend
  • modulo: has the same sign as the divisor

+———+———–+——–+ | | remainder | modulo | +———+———–+——–+ | +4 % +3 | 1 | 1 | | -4 % +3 | -1 | 1 | | +4 % -3 | 1 | -1 | | -4 % -3 | -1 | -1 | +———+———–+——–+