Function GraycodeN with parameter N indicating the number of inputs transforms Boolean number K (in the range 0..2N-1) into the Kth pattern in the Gray code. If the circuit for such function is run in reverse, the output is the ordinal number of the corresponding Gray code pattern. Assuming GraycodeN function has inputs/outputs labeled a,b,...,z , its optimal circuit is TOF(b;a) TOF(c;b) ... TOF(z;y).
For example, circuit for Graycode6
is illustrated below.