Search code examples
mathgray-code

Does Gray code exist for other bases than two?


Just a matter of curiosity, is the Gray code defined for bases other than base two?

I tried to count in base 3, writing consecutive values paying attention to change only one trit at a time. I've been able to enumerate all the values up to 26 (3**3-1) and it seems to work.

        000              122              200
        001              121              201
        002              120              202
        012              110              212
        011              111              211
        010              112              210
        020              102              220
        021              101              221
        022              100              222

The only issue I can see, is that all three trits change when looping back to zero. But this is only true for odd bases. When using even bases looping back to zero would only change a single digit, as in binary.

I even guess it can be extended to other bases, even decimal. This could lead to another ordering when counting in base ten ... :-)

    0  1  2  3  4  5  6  7  8  9 19 18 17 16 15 14 13 12 11 10
   20 21 22 23 24 25 26 27 28 29 39 38 37 36 35 34 33 32 31 30

Now the question, has anyone ever heard of it? Is there an application for it? Or it is just mathematical frenzy?


Solution

  • Yes. Have a look at the Gray code article at wikipedia. It has a section on n-ary Gray Code.

    There are many specialized types of Gray codes other than the binary-reflected Gray code. One such type of Gray code is the n-ary Gray code, also known as a non-Boolean Gray code. As the name implies, this type of Gray code uses non-Boolean values in its encodings.