Binary Gray Code - code example 
Author Message
 Binary Gray Code - code example

Working on Karnaugh Maps, I needed a way to produce binary Gray coded
numbers. For those who don't know, the idea behind Gray codes is that
successive integers differ in only one digit. For example, in binary, we can
count from 00 to 01 but we can not continue to 10 since from 01 to 10 we
change both digits.
There is more than one variant of binary Gray code, the commonly used one
called reflected Gray is the one my code produces.

The algorithm used is explained in detail by Martin Gardner in chapter two
of his "Knotted Doughnuts".

The idea is to have a verb "next" which will enalrge a list of Gray numbers.

The code follows.

   reflect=. , |.                  NB. This adds the reflect seq. to itself

                                   NB. and 1's to the other half
   next=.T ,. reflect              NB. Stitch the prev. seq. and T

For example, let's start with seed=.0 1.

Next will work by stitching 0 0 1 1 (=T seed) to 0 1 1 0 (reflect seed) to
produce 0 0
        0 1
        1 1
        1 0

which is the Gray code for i. 4

Notice it is very easy to do something like this:
seed=. 0 1
next^:3 seed

That's all....
Any comments on the code are welcome,

--



Wed, 10 Mar 1999 03:00:00 GMT  
 Binary Gray Code - code example

Reposting article removed by rogue canceller.

Working on Karnaugh Maps, I needed a way to produce binary Gray coded
numbers. For those who don't know, the idea behind Gray codes is that
successive integers differ in only one digit. For example, in binary, we can
count from 00 to 01 but we can not continue to 10 since from 01 to 10 we
change both digits.
There is more than one variant of binary Gray code, the commonly used one
called reflected Gray is the one my code produces.

The algorithm used is explained in detail by Martin Gardner in chapter two
of his "Knotted Doughnuts".

The idea is to have a verb "next" which will enalrge a list of Gray numbers.

The code follows.

   reflect=. , |.                  NB. This adds the reflect seq. to itself

                                   NB. and 1's to the other half
   next=.T ,. reflect              NB. Stitch the prev. seq. and T

For example, let's start with seed=.0 1.

Next will work by stitching 0 0 1 1 (=T seed) to 0 1 1 0 (reflect seed) to
produce 0 0
        0 1
        1 1
        1 0

which is the Gray code for i. 4

Notice it is very easy to do something like this:
seed=. 0 1
next^:3 seed

That's all....
Any comments on the code are welcome,

--



Wed, 10 Mar 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. help: binary code -> gray code convertation

2. Example of Python code (C code to Python code)

3. Anyone have a good coding style example for coding up FIR FILTER?(EASY METHOD)

4. Reflected Gray Codes

5. How to implement a Gray Code counter?

6. gray code counter in asyn FIFO design

7. Do gray code counters comsume less power

8. One-Hot or Gray-Code State Machine

9. One-Hot or Gray-Code State Machine?

10. Gray Code Counter - Comments?

11. Gray code counters

12. Do gray code counters comsume less power

 

 
Powered by phpBB® Forum Software