Wraparound Counters and Their Range

A wrapping counter reduces each result modulo the number of possible states. The stored value alone cannot record every overflow.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Step a three-bit counter
  2. Compute a short elapsed count
  3. Trace a small cyclic sequence through the boundary
  4. Detect what the counter cannot encode

Step a three-bit counter

Starting at six, the next three values are seven, zero, and one. This follows the modulo rule with divisor eight. The value one can represent one step from zero or nine, among other possibilities.

Compute a short elapsed count

From old value seven to current value two, the difference modulo eight is three. That is the exact forward count only when fewer than eight increments can occur between observations. The function contract should state that measurement assumption.[1]

Trace a small cyclic sequence through the boundary

A four-bit unsigned event counter records values modulo sixteen. Starting at fourteen, three increments yield fifteen, zero and one. The modular difference from fourteen to one is three. It identifies the number of steps only if fewer than sixteen elapsed. See quotient and remainder.

Before: 14
After +1: 15
After +2: 0
After +3: 1
(1−14) mod 16 = 3

Detect what the counter cannot encode

Nineteen increments produce one as well. If updates can occur faster than sampling permits, use a wider count or additional epoch information. Test a sample crossing zero and one where an entire cycle has elapsed.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩