8
Research communications 837 Kybernetes, Vol. 28 No. 6/7, 1999, pp. 837-844. # MCB University Press, 0368-492X Algorithms for generating generalized gray codes Edward T. Lee and Madonna E. Lee L&P Culture and Technology, Inc., San Diego, California, USA Keywords Cybernetics, Codes, Algorithms Abstract Gray code is a code with the property that there is one and only one bit-change between any two neighboring numbers. An algorithm for generating gray codes is presented. It turns out that there are other codes which have the same characteristics as gray codes. We call this class of codes generalized gray code (GGC). More precisely, a GGC is a code which has both the reflective property and the unit distance property. Algorithms for generating n-bit GGC from the (n – 1)-bit GGC are presented with illustrative examples. It is found that the number of n-bit GGC is equal to 2n times the number of (n – 1)-bit GGC. GGC generation trees are used to find GGC. Shows that GGC may be used in the two cases: where 1: gray code cannot be used, and as 2: member of the GGC is better suited than the gray code. Deduces that through the use of GGC, we have more choices than using just gray codes, and that we may obtain better results in terms of fan-in, fan-out, propagation delays, power consumption, or other related constraints in designing digital systems. The results obtained in this paper may also have useful applications in implementing special logic functions such as fuzzy threshold functions or fuzzy symmetric functions. 1. Introduction The gray code is a code with the property that there is one and only one bit- change between any two neighboring numbers. An algorithm for generating gray codes is presented. It turns out there are other codes which have the same characteristics as gray codes. We call these class of codes generalized gray code (GGC). More precisely, a GGC is a code which has both the reflective property and the unit distance property. Algorithms for generating n-bit GGC from the (n – 1)-bit GGC are presented with illustrative examples. It is found that the number of n-bit GGC is equal to 2n times the number of (n – 1)-bit GGC. GGC generation trees are used to find GGC. GGC may be used in the following two cases. Case 1: gray code can not be used. Case 2: member of the GGC is better suited than the gray code. Through the use of GGC, we have more choices than using just gray codes. Thus, we may obtain better results in terms of fan-in, fan-out, propagation delays, power consumption, or other related constraints in designing digital systems. The results obtained in this paper may also have useful applications in implementing special logic functions such as fuzzy threshold functions or fuzzy symmetric functions. 2. Gray code The unique characteristic of gray coded binary numbers, or simply gray code (Wilson, 1962), is that there is one and only one bit-change between any two neighboring numbers. The current issue and full text archive of this journal is available at http://www.emerald-library.com

Algorithms for generating generalized gray codes

Embed Size (px)

Citation preview

Researchcommunications

837

Kybernetes,Vol. 28 No. 6/7, 1999, pp. 837-844.

# MCB University Press, 0368-492X

Algorithms for generating generalizedgray codes

Edward T. Lee and Madonna E. LeeL&P Culture and Technology, Inc., San Diego, California, USA

Keywords Cybernetics, Codes, Algorithms

Abstract Gray code is a code with the property that there is one and only one bit-changebetween any two neighboring numbers. An algorithm for generating gray codes is presented. Itturns out that there are other codes which have the same characteristics as gray codes. We callthis class of codes generalized gray code (GGC). More precisely, a GGC is a code which has boththe reflective property and the unit distance property. Algorithms for generating n-bit GGC fromthe (n ± 1)-bit GGC are presented with illustrative examples. It is found that the number of n-bitGGC is equal to 2n times the number of (n ± 1)-bit GGC. GGC generation trees are used to findGGC. Shows that GGC may be used in the two cases: where 1: gray code cannot be used, and as 2:member of the GGC is better suited than the gray code. Deduces that through the use of GGC, wehave more choices than using just gray codes, and that we may obtain better results in terms offan-in, fan-out, propagation delays, power consumption, or other related constraints in designingdigital systems. The results obtained in this paper may also have useful applications inimplementing special logic functions such as fuzzy threshold functions or fuzzy symmetricfunctions.

1. IntroductionThe gray code is a code with the property that there is one and only one bit-change between any two neighboring numbers. An algorithm for generatinggray codes is presented. It turns out there are other codes which have the samecharacteristics as gray codes. We call these class of codes generalized gray code(GGC). More precisely, a GGC is a code which has both the reflective propertyand the unit distance property.

Algorithms for generating n-bit GGC from the (n ± 1)-bit GGC are presentedwith illustrative examples. It is found that the number of n-bit GGC is equal to 2ntimes the number of (n ± 1)-bit GGC. GGC generation trees are used to find GGC.

GGC may be used in the following two cases.Case 1: gray code can not be used.Case 2: member of the GGC is better suited than the gray code.

Through the use of GGC, we have more choices than using just gray codes.Thus, we may obtain better results in terms of fan-in, fan-out, propagationdelays, power consumption, or other related constraints in designing digitalsystems. The results obtained in this paper may also have useful applicationsin implementing special logic functions such as fuzzy threshold functions orfuzzy symmetric functions.

2. Gray codeThe unique characteristic of gray coded binary numbers, or simply gray code(Wilson, 1962), is that there is one and only one bit-change between any twoneighboring numbers.

The current issue and full text archive of this journal is available athttp://www.emerald-library.com

Kybernetes28,6/7

838

3. An algorithm for generating n-bit gray codesAlgorithm 1: An algorithm for generating the n-bit gray code may be specifiedrecursively.

The one-bit gray code is 0 followed by 1 as shown in Figure 1. Given the(n ± 1)-bit gray code, the n-bit gray code can be obtained by performing thefollowing three tasks consecutively:

Task 1: Put a mirror at the bottom of the (n ± 1)-bit gray code.Task 2: Generate the mirror image of the (n ± 1)-bit gray code.Task 3: For all the codewords above the mirror add a `̀ zero'' as the most

significant bit. For all the codewords below the mirror add a `̀ one''as the most significant bit.

The process of generating 4-bit gray code is summarized in Figure 1.Furthermore, the horizontal bars represent the mirrors. In addition, the decimalnumbers corresponding to the gray code are also presented.

Theorem 1: The code generated by algorithm 1 is the n-bit gray code.Proof: Note that we add a zero for all the codewords above the mirror and a

one for all the codewords below the mirror.We prove the validity of algorithm 1 by induction. We know that the given one-

bit code is a gray code. In addition, we assume that the generated (n ± 1)-bit code isa gray code. We want to prove that the generated n-bit code is also a gray code.

(i) There is only one bit change (the most significant bit) between thegenerated first codeword and the last codeword.

(ii) There is only one bit change (the most significant bit) between the twocodewords adjacent to the mirror.

(iii) For all the other adjacent codewords, the number of bit change remainsthe same as in the (n ± 1) bit case because the two most significant bits

Decimalnumber

Four-bitgray code

Figure 1.Generation of 4-bitgray code

Researchcommunications

839

are the same. Through the use of the induction hypothesis the number ofbit change of two neighboring codewords remains one.

4. Properties of gray codesDefinition 1: A reflected code is characterized by the fact that all pairs ofcodewords with respect to the center of the code have one bit change.

Property 1: A gray code is a reflective code.Definition 2: A unit distance code is a code that changes in only one bit

position from one codeword to the next adjacent codeword.Property 2: A gray code is a unit distance code.Property 3: There are 2n codewords for an n-bit gray code.Property 4: For every column in an n-bit gray code, the number of `̀ one'' is

equal to the number of `̀ zero'' which is equal to 2nÿ1. In other words, for eachcolumn, half of the entries are `̀ one'' and half of the entries are `̀ zero'':

number of ``1'' = number of ``0'' = 2nÿ1 �1�

5. Gray-to-binary and binary-to-gray code convertersIf we want to use the whole gray code, then algorithm 1 presented is aneffective way to generate the whole 2n codewords. On the other hand, if we onlydeal with one gray codeword, then gray-to-binary and binary-to-gray codeconverters may be used.

Let G4;G3;G2;G1 represent the four bits of the gray code with G4

representing the most significant bit.Let B4;B3;B2;B1 represent the four bits of the corresponding binary code

with B4 representing the most significant bit.

Gray-to-binary code converterB4;B3;B2 and B1, in terms of G4;G3;G2;G1 are summarized in the followingfour equations:

B4 � G4

B3 � G4 � G3

B2 � G4 � G3 � G2

B1 � G4 � G3 � G2 � G1

�2�

where� denotes the exclusive Or operation.The logic diagram of a gray-to-binary code converter is shown in Figure 2.

Binary-to-gray code converterG4;G3;G2;G1 in terms of B4;B3;B2 and B1 are summarized in the followingfour equations:

Kybernetes28,6/7

840

G4 � B4

G3 � B4 � B3

G2 � B3 � B2

G1 � B2 � B1

�3�

where� denotes the exclusive Or operation.The logic diagram of a binary-to-gray code converter is shown in Figure 3.

6. Generalized gray codesDefinition 3: A generalized gray code (GGC) is a code which has both thereflective property and the unit distance property.

The relationship among generalized gray codes, reflective codes, and unitdistance codes is presented in Figure 4.

Theorem 2: The generalized gray codes are the intersection of reflectivecodes and unit distance codes.

As stated in Fletcher (1980), the main advantage of generalized gray codes isthat they eliminate `̀ transitional'' errors or `̀ flashing'' errors.

A transitional error is explained in the following example.

Example 1 (a transitional error)Suppose we use three-bit binary code. Consider the transition from 001 to 010.Owing to misalignment, a transient code of 011 may appear. Thus, erroneousdata may be received, that is, 001! 011! 010. This type of error is called atransitional error.

GGC can eliminate transitional errors due to the unit distance property.GGC may be generated using algorithm 1 with the following modifications:

Figure 3.Logic diagram of abinary-to-gray codeconverter

Figure 2.Logic diagram of gray-to-binary code converter

Researchcommunications

841

(i) To include the cases that `̀ logic zero'' and `̀ logic one'' are interchanged.

(ii) To include the cases that inserts a column at different locations.

This leads to the introduction of the GGC generation tree which is presented inthe next section.

7. Generalized gray code generation treeA generalized gray code generation tree is constructed using the followingalgorithm.

Algorithm 2: At level 0, the starting point is called the root of the tree. Theroot has two branches labeled as A1 and B1 respectively where A1 indicatesinserting a column starting with 0s followed by 1s, and B1 indicates inserting acolumn starting with 1s followed by 0s. In addition, the horizontal barsrepresent mirrors.

At level 1, each node generates four branches at the next level labeled as A1,A2, B1, B2 where A and B indicate the inserted column starts with 0s followedby 1s or starts with 1s followed by 0s respectively. 1 and 2 indicate the locationof the inserted column.

At level 2, each node generates six branches at the next level labeled as A1,A2, A3, B1, B2, B3 as shown in Figure 5.

At level (n ± 1), each node generates 2n branches at the next level labeled asA1, A2, . . ., An, B1, B2, . . ., Bn.

In terms of nodes, for each node at level n, (2n + 2) new nodes will begenerated at the next level in the following manner.

Step 1: Put a mirror at the bottom of the n-bit generalized gray code.Step 2: Generate the mirror image with respect to the mirror.

Figure 4.Relationship among

generalized gray codes,reflective codes, and unit

distance codes

Kybernetes28,6/7

842

Step 3: Generate (n + 1) new nodes at level (n + 1) corresponding to insertinga new column with 2n zeros followed by 2n ones at the (n + 1)possible location between existing columns as shown in Figure 5 (forn = 2 case).

Step 4: Generate additional (n + 1) new nodes at level (n + 1) correspondingto inserting a new column with 2n ones followed by 2n zeros at the(n + 1) possible locations between existing columns as shown inFigure 5 (for n = 2 case).

Theorem 3: For the generalized gray code generation tree (as shown in Figure5), every node at level n is an n-bit generalized gray code.

Theorem 4: All the nodes at the same level of a GGC generation tree aredifferent.

Lemma 1: In the GGC generation tree, if we always select the A1 branch,then we obtain the gray code.

Lemma 2: For every code (node) in the GGC generation tree, the number ofones is equal to the number of zeros for every column.

Root

Figure 5.Partial generalized graycode generation tree

Researchcommunications

843

Lemma 3: (i) There are at least two 1-bit GGC.(ii) There are at least eight 2-bit GGC.(iii) There are at least 48 3-bit GGC.

Theorem 5: In the GGC generation tree, let N(n ± 1) and N(n) denote the numberof nodes at level (n ± 1) and level n respectively. Then,

N�1� � 2 �4�N�2� � 8 �5�N�3� � 48 �6�

N�n� � 2n�N�nÿ 1� �7�where * denotes multiplication.

Equation (7) denotes the recursive relationship of the number of nodes fromone level to the next.

Lemma 4: There are at least N(n) n-bit GGC.

8. Generalized gray coded disk encoderThe generalized gray code has some interesting applications in a special inter-domain converter called a positional or rotational encoder. A rotational encoderor shaft encoder is used to convert some angular position of a shaft to a digitalformat. A simple technique for this employs a photographic disk attached tothe shaft. This disk has windows strategically placed in circular patterns,through which light can pass and activate a photo sensor (light-sensitiveswitch). If these windows are arranged on the circumference of concentriccircles around the center of the shaft, a digital code related to its angularposition can be generated.

A three-bit gray coded disk encoder is shown in Figure 6.A generalized gray code can be implemented in a similar manner by

repeatedly

(i) interchanging appropriate disks; and/or

(ii) interchanging zero and one for certain disks.

9. Application of GGCGGC may be used in the following situations. First, assume that codes withreflective property and unit distance property are required to be used forcertain applications. Then, GGC are used over gray codes for the following twocases.

Case 1: Owing to other issues such as costs, propagation delays, or powerconsumption, gray code cannot be used. In this situation, we can use GGC.

Case 2: Certain members of the GGC may be better suited than gray code interms of the overall considerations such as the combined effects of costs, speed,reliability, etc.

Kybernetes28,6/7

844

10. ConclusionGeneralized gray codes are presented. Using generalized gray codes we mayobtain better results than using gray codes in terms of other considerationssuch as fan-in, fan-out, total propagation delays, power consumption, etc. Theresults obtained in this paper may also have useful applications inimplementing special logic functions such as fuzzy threshold functions (Leeand Chou, 1995), or fuzzy symmetric functions (Lee, 1993).

References

Fletcher, W.I. (1980), An Engineering Approach to Digital Design, Prentice-Hall, EnglewoodCliffs, NJ.

Lee, E.T. (1993), `̀ Fuzzy functions with don't-care conditions'', Kybernetes, Vol. 22 No. 4,pp. 54-68.

Lee, E.T. and Chou, T.S. (1995), `̀ Fuzzy threshold functions and applications,'' Kybernetes, Vol. 24No. 7, pp. 99-122.

Wilson, M.C. (1962), `̀ Gray to binary converter'', Instruments & Control Systems, June, pp. 149-50.

04590135180225270315

GrayCode Word

0

LSB

MSB

000001011010110111101100

Angle

LSB: least significant bit.MSB: most significant bit.

Figure 6.Three-bit gray codeddisk encoder