modulo

The operation that gives the remainder after division, written % in many programming languages, so 17 % 5 is 2.

In more detail

Hash tables use modulo to fold a large hash into a slot number between zero and the table size minus one. Ring buffers use it to wrap an index back to zero at the end of the array. Languages differ for negative numbers: in Python the result has the same sign as the divisor, while in C, Java and JavaScript it has the sign of the number being divided.

Examples

(7 + 1) % 8 == 0  # index 7 wraps to 0 in an 8-slot buffer
7 % 3 == 1
Kafka partitioners use hash % partition_count, so adding partitions moves keys.

Related terms

Taught in

References

Certification courses