## Counting in Gray – Part II – Observations

May 13, 2007

In the last post we discussed the different approaches, their advantages and disadvantages in terms of implementation, design requirements etc. We finished with the promise to have a solution for counting in Gray code, with registered outputs and which is could easily be described in HDL.

In this post we will observe some interesting facts concerning mirrored Gray codes, which in turn will lead us to our implementation.

Let’s start.

One of the most important and basic things we can see when observing Gray codes, is that with each increment or decrement the parity of the entire number changes. This is pretty obvious since each time only a single bit changes.

The Next observation is the “toggling period” of each of the bits in Gray representation. Bit 0, or the LSB has a “toggle period” of 2 – i.e. it flips each 2 counts. Bit 1 (one to the left of the LSB) has a “toggle period” of 4. In General with each move towards the MSB side, the toggle period doubles. An Exception is the MSB which has the same toggle period has the bit to its immediate right.
The top figure on the right demonstrates this property for a 5 bit Gray code.

The reason why this is true can be easily understood if we consider the way mirrored Gray codes are being constructed (which I assume is well known). Notice that this fact just tells us only the toggle period of each bit, not when it should toggle! To find this out, we will need our third observation.

Let us now look at when each bit flips with respect to its position. In order to help us, we will have to recall our first observation – parity changes with each count. The bottom figure on the right reveals the hidden patterns.

In General: Gray bit n will toggle in the next cycle, when the bit to its immediate right is “1” and all the other bits to its right are 0 – or in other words a 100…00 pattern
The only exception is the MSB which toggles when all the bits to its right except the one to its immediate right are “0” – or a X00…00 pattern

sounds complicated? look in the picture again, the pattern will just pop out to you.

You can take my word for it or check for yourself, anyways the rules for counting backwards (or down), in Gray, are:

The LSB toggles when the parity bit is “0”
For all the other bits: Gray bit n will toggle in the next cycle, when the bit to its immediate right is “1”, all the other bits to its right are 0 and the parity bit is “1” – or in other words a 100…01 pattern

On the next post we will see how to use those observations to create a simple “gray bit cell”, which will be used as our building block for the final goal – the up/down Gray counter.