# We (almost) have a ternary Gray code solution

In my "n-ary" Gray Code blog a couple of days ago, I asked if it were possible to get a full gray code in a "3-ary" (ternary, base-3) system, in which each digit can adopt values of 0, 1, and 2? The example I gave is as follows (we are playing with 2-digit values for the sake of simplicity):

Although the right-hand table may look like a Gray code at first glance, it fails at the final fence, because the transition from 22 back to 00 involves two digits changing at the same time.

Actually, if the truth be told, I wasn't sure if it would be possible to have a Gray code when the total count value was an odd number (nine states in this case). However, a reader just emailed me with the following solution:

So, that answers the first portion of my ponderable, but what about the second part, in which I said: *"If it is possible to achieve a ternary Gray code, can we map binary values onto the ternary digits such that we have Gray codes in both bases?"*. Well... the same reader came up with the following solution, assuming ternary 0 is mapped to binary 00, ternary 1 is binary 10, and ternary 2 is binary 11:

Oh... it's so close. The only problem is the wrap-around from the final state back to the starting point. The ternary transition from 02 to 00 is OK, but the equivalent transition for this binary transition is 0011 to 0000, which involves two bits changing.

But I live in hope – we are so close – maybe a different ternary Gray code sequence and/or a different binary mapping will get us there (although when I say "us" I actually mean "you" because I have a paper to write that must be completed before the end of the day)...

Questions? Comments? Feel free to email me – Clive "Max" Maxfield – at max@techbites.com). And, of course, if you haven't already done so, don't forget to Sign Up for our weekly Programmable Logic DesignLine Newsletter.