TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Gray Code

124 点作者 stansmith超过 10 年前

12 条评论

smoyer超过 10 年前
Coming from a embedded systems background (mostly in the telecommunications sector), I have a deep love for Gray Code. My fondest memory is teaching a group of software engineers how &quot;us hardware guys&quot; do logic minimization.<p>Imagine a set of deeply nested if-then-else conditions that are dependent upon a fixed set of boolean flags - this is pretty common in a state machine. Reducing those conditions to a formula that calculates a boolean value is often far less taxing on a small uC.<p>So I brought out one of my Electrical Engineering books (The Art of Electronics by Horowitz and Hill), let them each read the section on Karnaugh Maps [1] and then ran through some examples using their decision networks. Watching those light bulbs come on was one of the favorite events in my career.<p>[1] <a href="https://en.wikipedia.org/wiki/Karnaugh_map" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Karnaugh_map</a>
评论 #8647395 未加载
schoen超过 10 年前
This is such a beautiful explanation! And I love the shaft orientation detector example.<p>The biggest effect that Gray code had on my life was when it helped me pass an entrance exam to a math camp when I was pretty young (younger than most other students), because one of the questions on the entrance exam was &quot;can you prove that there is or isn&#x27;t a Hamiltonian circuit on any n-dimensional hypercube?&quot;.<p>The Hamiltonian path is a path that visits every node once, and a Hamiltonian circuit does this and also returns to the starting point.<p><a href="https://en.wikipedia.org/wiki/Hamiltonian_path" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hamiltonian_path</a><p>Well, I knew about the binary Gray code from Martin Gardner (in his book <i>Knotted Doughnuts and Other Mathematical Entertainments</i>), and I realized that if you think of an n-bit number as a coordinate in n-dimensional space (like 10101110 is the coordinate (1, 0, 1, 0, 1, 1, 1, 0)), then the n-bit binary Gray code is already a description of a Hamiltonian cycle on the unit n-dimensional hypercube.<p>It tells you how to trace the path, because it tells you which vertices to go to in which order. Every transition from one number to the next is an edge of the cube because it involves changing exactly one bit (so, exactly one dimension), which is exactly what defines the edge of a cube (it&#x27;s a movement in exactly one dimension). You visit every vertex because the Gray code includes every number, and as this linked article says, the default Gray code is cyclic and comes back to the original starting point at the end.<p>We know that there&#x27;s a Gray code for any number of bits because there is a recursive reflective procedure for constructing them to any length (as described in this article): take the (n-1)-bit Gray code, write it forwards prefixed with 0 and then backwards prefixed with 1, and you have an n-bit Gray code. This also has a nice geometric interpretation, which is that given a Hamiltonian circuit on an (n-1)-dimensional hypercube, you can do it on the &quot;lower&quot; hypercube, go &quot;up&quot;, do it backwards, and then come &quot;down&quot;, and now you&#x27;ve created a Hamiltonian circuit on the larger n-dimensional hypercube.<p>This answer let me pass the test and get into the math camp, but I found it pretty difficult when I actually went, I think because I didn&#x27;t exactly come up with this answer &quot;on my own&quot;: Martin Gardner did a ton of the work for me in teaching me about Gray codes, and I had already been thinking about the isomorphism between binary numbers and hypercubes before for some reason.
Marcus10110超过 10 年前
This is a fundamental in FPGAs. The mode where the bit is unstable is called metastability. Most FPGA tools can automatically infer gray code for state machine states, and pretty much all cross clock domain FIFOs will use grey code to indicate where the read and write pointers are located.
评论 #8646681 未加载
评论 #8646599 未加载
ddingus超过 10 年前
Great piece! I enjoyed it.<p>The author comments: It&#x27;s possible to generate Gray codes without this restriction (though to be honest, I can&#x27;t understand the value of this, as the step-change on the warp around would experience the exact problem we are trying to solve!)<p>Linear encoders seems to me a perfect application.
评论 #8646949 未加载
apitheia超过 10 年前
With ternary grey code, only one digit is changed at a time, but sometimes that change can be more than one value, why isn&#x27;t ternary ordered so that each digit only changes by at most 1 number? for example:<p><pre><code> 0 → 000 | 000 1 → 001 | 001 2 → 002 | 002 10 → 012 | 012 11 → 010 | 011 12 → 011 | 010 20 → 021 | 020 21 → 022 | 021 22 → 020 | 022 100 → 120 | 122 101 → 121 | 121 102 → 122 | 120 110 → 102 | 110 111 → 100 | 111 112 → 101 | 112 120 → 111 | 102 121 → 112 | 101 122 → 110 | 100 200 → 210 | 200 201 → 211 | 201 202 → 212 | 202 210 → 222 | 212 211 → 220 | 211 212 → 221 | 210 220 → 201 | 220 221 → 202 | 221 222 → 200 | 222</code></pre>
评论 #8648152 未加载
fintler超过 10 年前
I still think the most interesting thing about gray codes is the relationship to hilbert curves. It&#x27;s really useful for figuring out locality with a cheap calculation (axes to&#x2F;from transpose).
Throwaway1224超过 10 年前
I use gray code to generate sobol sequences, which are psuedo random number sequences that help you approximate a distribution more quickly than purely random sequences.
fredsted超过 10 年前
A great example of a useful article that keeps me reading HN.
IshKebab超过 10 年前
If you liked this, google &quot;single track gray codes&quot;.
评论 #8646589 未加载
AndyNemmity超过 10 年前
There was an article about computers optimizing a card trick earlier this week, and it was using Gray code.
EugeneOZ超过 10 年前
What about to add control digits?
stansmith超过 10 年前
There are many, many, other cool articles on the site.<p><a href="http://www.datagenetics.com/blog.html" rel="nofollow">http:&#x2F;&#x2F;www.datagenetics.com&#x2F;blog.html</a><p>Once you start reading, you&#x27;ll spend the rest of the day!