encode | decode | visualize

> gray | reflected | binary <

// Gray Code - Binary numeral system with single-bit transitions

0 chars
0 chars
[SINGLE-BIT]

Unit Distance

Only one bit changes between consecutive values.

[ERROR-REDUCTION]

Error Minimization

Reduces errors in analog-to-digital conversion.

[CYCLIC]

Cyclic Property

First and last values differ by only one bit.

>> technical info

How Gray Code Works:

Gray code (reflected binary code) is a binary numeral system where two successive values differ in only one bit. Created by Frank Gray, it's formed by XORing each bit with the previous bit. This property makes it ideal for position encoders and error reduction in digital systems.

Gray Code Examples:

Decimal | Binary | Gray Code
0       | 0000   | 0000
1       | 0001   | 0001
2       | 0010   | 0011
3       | 0011   | 0010
4       | 0100   | 0110
5       | 0101   | 0111
6       | 0110   | 0101
7       | 0111   | 0100

Note: Only 1 bit changes between consecutive Gray codes

Why Use Gray Code:

  • Rotary encoders
  • Karnaugh maps
  • Error correction
  • Digital communications
  • Position sensors

>> frequently asked questions

What is Gray code?

Gray code, also known as reflected binary code, is a binary numeral system where two successive values differ in only one bit. It was invented by Frank Gray and is widely used in digital systems to prevent spurious output during transitions.

How to convert binary to Gray code?

To convert binary to Gray: 1) Keep the MSB (most significant bit) as is, 2) XOR each bit with the previous bit. Formula: G[i] = B[i] XOR B[i-1]. For example, binary 1011 becomes Gray 1110.

Why is Gray code better for encoders?

Gray code prevents ambiguous readings during transitions. In binary, changing from 7 (0111) to 8 (1000) requires changing all 4 bits simultaneously. If not perfectly synchronized, intermediate values could be read. Gray code ensures only one bit changes at a time.

Where is Gray code used?

Gray code is used in rotary encoders, Karnaugh map simplification, error correction in digital communications, genetic algorithms, and analog-to-digital converters. It's essential wherever single-bit transitions are needed.

格雷码是什么?它和二进制有什么区别?

格雷码(Gray code)又称反射二进制码,是一种相邻两个数字只有 1 位不同的二进制编码。与普通二进制相比,最大区别是:
二进制:从 01111000 需要 4 位同时翻转,中间态会出现错误读数。
格雷码:任何相邻数只改变 1 位,比如 0100(4)→ 0101(5)→ 0111(6)。
这让格雷码特别适合旋转编码器、位置传感器、A/D 转换器和异步跨时钟域信号传输。

格雷码转二进制怎么算?

二进制 → 格雷码:最高位保持不变,后续每一位 = 当前二进制位 XOR 前一位二进制位。
公式:G[i] = B[i] XOR B[i-1]G[0] = B[0]

格雷码 → 二进制:最高位保持不变,后续每一位 = 当前格雷码位 XOR 前一位二进制结果(累积异或)。
公式:B[i] = G[i] XOR B[i-1]

例:格雷码 1110 → 二进制:B3=1, B2=1⊕1=0, B1=1⊕0=1, B0=0⊕1=11011 = 11。

2 進数 グレイコード変換(Binary to Gray code conversion)?

2 進数からグレイコード(グレイコード 変換)への変換は XOR 演算で行います:
G[i] = B[i] XOR B[i+1](MSB から見た場合)あるいは同等に G[i] = B[i] XOR B[i-1](LSB から見た場合)。
例:B = 1010G = 1_1⊕0_0⊕1_1⊕0 = 1111
逆方向(グレイ → 2 進数)は累積 XOR:B[i] = G[i] XOR G[i+1] XOR … XOR G[n-1]。JavaScript では const gray = bin ^ (bin >> 1) の 1 行で実装できます。

How do I convert Gray code to binary in one line of code?

A classic bit-twiddling identity turns any Gray-coded integer back into binary in O(log n):

// JavaScript / C / Java / Rust
let bin = gray;
bin ^= bin >>> 1;
bin ^= bin >>> 2;
bin ^= bin >>> 4;
bin ^= bin >>> 8;
bin ^= bin >>> 16;
And binary → Gray in one line: const gray = bin ^ (bin >>> 1);. Python: bin_from_gray = g ^ (g >> 1) ^ (g >> 2) ^ ... or use functools.reduce.

Code Gray binaire réfléchi — à quoi sert-il exactement ?

Le code Gray (aussi appelé code binaire réfléchi) a été breveté par Frank Gray chez Bell Labs en 1953. Son application principale : les encodeurs rotatifs absolus et les convertisseurs A/N. Comme un seul bit change à la fois, même si deux pistes ne commutent pas parfaitement en phase, la lecture intermédiaire reste valide (soit l'ancienne valeur, soit la nouvelle). Autres usages : minimisation de fonctions booléennes via tables de Karnaugh, algorithmes génétiques (mutations d'un bit), communication numérique (QAM, réduction des erreurs de symbole).

What are reflected Gray codes beyond 1 bit per step?

Standard binary-reflected Gray code has the 1-bit distance property. Generalizations include:
Balanced Gray codes — each bit position flips roughly the same number of times.
n-ary Gray codes — base-3 (ternary), base-4, etc., where consecutive digits differ by 1.
Snake-in-the-box codes — Gray codes embedded in a hypercube used in error-correcting channels.
BCD Gray code — only cycles through BCD digits 0–9 instead of all 16 values.
For most practical work (encoders, K-maps, fuzz testing), the standard G = B ^ (B >> 1) reflected code is what you want.

Is Gray code related to error-correcting codes like Hamming?

They solve different problems. Gray code prevents transient errors during signal transitions by guaranteeing only one bit changes at a time — there is no redundancy, so a corrupted Gray code is just the wrong value. Hamming codes add redundant parity bits to detect and correct bit errors after they have happened. Both are often used together in digital comms: Gray-coded QAM symbols minimize the chance of multi-bit errors, and an outer Hamming (or Reed–Solomon) code recovers any remaining errors. See /hamming/ for the companion tool.

Other Languages