

Gray Code - brudgers
https://en.wikipedia.org/wiki/Gray_code

======
ziedaniel1
I count in Gray code using my fingers. It's twice as fast as binary because
the LSB switches half as often! I've also found patterns that are even better,
but they're harder to learn.

------
kilovoltaire
some pleasant tree patterns:

    
    
      0000000000000000000000000000000011111111111111111111111111111111
      0000000000000000111111111111111111111111111111110000000000000000
      0000000011111111111111110000000000000000111111111111111100000000
      0000111111110000000011111111000000001111111100000000111111110000
      0011110000111100001111000011110000111100001111000011110000111100
      0110011001100110011001100110011001100110011001100110011001100110
    

(the first 64 gray codes, as columns)

~~~
m1el
python code to generate this thing:

    
    
        def graytree(n):
          for row in range(n, 0, -1):
            d = 2**(row-1)
            yield ''.join(str(((i+d)>>row)%2) for i in range(2**n))
    
        for r in graytree(6):
          print(r)

~~~
acc54321
or in J

    
    
        graycodes=: verb define
            >(0&,&.>,1&,&.>@:|.)^:(<:y) 0 1
        )
        ' +'{~|:graycodes 6
    
                                    ++++++++++++++++++++++++++++++++
                    ++++++++++++++++++++++++++++++++                
            ++++++++++++++++                ++++++++++++++++        
        ++++++++        ++++++++        ++++++++        ++++++++    
      ++++    ++++    ++++    ++++    ++++    ++++    ++++    ++++  
     ++  ++  ++  ++  ++  ++  ++  ++  ++  ++  ++  ++  ++  ++  ++  ++

------
JoeAltmaier
To convert between binary and Gray, take the xor of adjacent bits. It works in
both directions.

------
orand
In the past 15 years, Gray code is the only part of my Computer Science
education I've actually found a use for in my day-to-day work developing
business applications. I've used it to simplify overly complex if/else logic
down to its simplest possible form. All the rest of the knowledge I use on a
daily basis has been learned on the job.

~~~
ucha
Could you expand on that? I don't really understand how you could simplify
if/else logic by using this which is a bijection from one binary counting
system to another.

~~~
defect
Karnaugh maps I'm guessing.
[https://en.wikipedia.org/wiki/Karnaugh_map](https://en.wikipedia.org/wiki/Karnaugh_map)

~~~
iso8859-1
Made a tiny webapp to do this, which lets you hover over the different parts
of the resulting expression, to see which parts of the karnaugh map they
cover: [https://github.com/ysangkok/py-kmap](https://github.com/ysangkok/py-
kmap)

------
m1el
That's so cool. When I was a kid, I had a panel with a ton of switches. I've
been thinking about the way to cycle through all of the possible switch
combinations (I already knew combinatorics and binary), and I invented Gray
codes in the process :)

I never thought it could be useful for anything but my amusement.

~~~
brudgers
Knuth thinks they're pretty cool too and talks about them in _TAoCP_ volume 4.
The pre-fascicle of his discussion is available in gzipped PostScript.

[http://www-cs-faculty.stanford.edu/~uno/fasc2a.ps.gz](http://www-cs-
faculty.stanford.edu/~uno/fasc2a.ps.gz)

------
IIAOPSW
I remember years ago when I accidentally reinvented this a few years ago.

[https://www.reddit.com/r/math/comments/o1l8p/i_accidentally_...](https://www.reddit.com/r/math/comments/o1l8p/i_accidentally_took_a_fractal_trip_through_the/)

------
vernie
As used by the Intel RealSense depth camera
([http://2.bp.blogspot.com/-ICUq6I74X7A/VMvn4I4ujWI/AAAAAAAALZ...](http://2.bp.blogspot.com/-ICUq6I74X7A/VMvn4I4ujWI/AAAAAAAALZE/k9ehl9WFleA/s1600/LaserProjectorPatterns_0.JPG))

------
greyone
Yes, I dig Gray Code. It's not at all related to my site
[http://GreyCoder.Com](http://GreyCoder.Com)

~~~
squeakynick
I see what you did there :)

I dig Gray Code too, and here are some pretty pictures
[http://datagenetics.com/blog/november32014/index.html](http://datagenetics.com/blog/november32014/index.html)

~~~
mikeash
Nothing related to Gray codes, but I love your site. There are so many
interesting things to explore there. Thanks for making it!

~~~
squeakynick
Thanks for the kind words!

------
blt
The application to optical encoders is one of the most beautiful intersections
between discrete math and the physical world ever imo.

------
jd3
So much innovation that came from one place: Bell Labs. Quite extraordinary.

------
jadengore
Learned about this in school, cool to see it on HN.

------
graycoder
Mmmmm, yes?

