[...]
Practical example: two way counter
You probably already know about this thing:
View attachment 12827
This is basically a counter, a decreasing counter actually, the current state can be seen as the binary number 111 (7 in decimal) and if i press the button i get:
View attachment 12828
110 (6 in decimal)
once again:
View attachment 12829
101 (5 in decimal)
That's great, but what if i want an increasing counter?
Well actually if i say that on=0 and off=1 instead of on=1 and off=0, i get 000 then 001 then 010, so it can also be seen as an increasing counter.
Okay, but if i want two buttons, an increase AND a decrease one? Now this is less trivial.
Actually there is a really simple solution:
View attachment 12830
just press that new button and...
View attachment 12831
Nice! But why does it work?
Well remember how sending a signal to the rightmost block can be seen as decrementing the counter? Actually sending a signal the middle block can also be seen as decrementing the counter but by 10 (binary) instead of 1, and sending a signal to the leftmost can be seen as decrementing the counter by 100.
So what we're really doing here is decrementing the counter by 111 (7 in decimal) and obviously:
5 - 7 = 6
Wait, let me correct that:
5 - 7 ≡
6 [
8]
Remember that our little counter can only store numbers from 0 (000) to 7 (111), if you try to get a number outside of these bounds, the counter just wraps around (by adding or substracting 8 until you have a valid number if you want, in french we say that "5-7" and "6" are "congrus modulo 8", i don't know how it's called in english).
In general if you have N Flip-flop you make computations modulo 2^N and sending a signal to each Flip-flop can be seen as decrementing by (2^N - 1), here N=3 so 2^3=8 and 2^3-1=7.
[...]