A counter has a fixed number of states, its modulus, so it can't keep counting forever. When it passes its last state it wraps around, like a car odometer going from 99999 to 00000.
- An n-bit up counter goes from all 1s (2ⁿ − 1) to all 0s.
- A down counter goes from all 0s to all 1s.
- A mod n counter goes from N − 1 back to 0.
The wrap isn't an error. It's the same as overflow in unsigned addition: the carry out of the top bit is simply dropped, because there's no flip-flop to hold it. The terminal count output tells the rest of the circuit when a wrap is about to happen, and that's how counters are cascaded.
Mathematically, a counter counts modulo N. After k edges from a start value s:
- up: (s + k) mod N
- down: (s − k) mod N, adding N if the result is negative.