Report Tool or Give Us Suggestions

Binary to Gray Code Converter

Convert a standard binary string to its corresponding Gray code equivalent with real-time feedback and step-by-step bit logic explanation.

L ading . . .

What is Gray Code?

The Gray Code (also known as Reflected Binary Code or RBC) is an ordering of the binary numeral system such that two successive values differ in only one bit position. It was originally patented by Frank Gray at Bell Labs in 1953 as a method to prevent spurious or erroneous outputs from electro-mechanical switches.

In standard binary representation, incrementing a number can cause multiple bits to change simultaneously. For example: $$\text{Decimal } 3 \to 4 \implies 011_2 \to 100_2 \quad (\text{Three bits change!})$$ In a physical system (like a rotary encoder or mechanical switch), these bits do not transition at exactly the same microsecond, which can lead to temporary erroneous readings like $010_2$ or $111_2$. In Gray Code, only a single bit changes: $$\text{Decimal } 3 \to 4 \implies 010_{Gray} \to 110_{Gray} \quad (\text{Only one bit changes!})$$

Mathematical Formula: Binary to Gray Code

Converting a standard binary number to its Gray code equivalent is computationally simple and can be done using the bitwise Exclusive OR (XOR) operation.

For a binary string $B = b_{n-1}b_{n-2}\dots b_0$, the corresponding Gray code $G = g_{n-1}g_{n-2}\dots g_0$ is defined by: $$g_{n-1} = b_{n-1} \quad (\text{The most significant bit remains identical})$$ $$g_i = b_{i+1} \oplus b_i \quad \text{for } 0 \le i < n-1$$ Where $\oplus$ represents the Exclusive OR (XOR) logical operator.

In whole-number arithmetic or high-performance programming, the entire number can be converted instantly using a shift-and-XOR operation: $$G = B \oplus (B \gg 1)$$ Where $\gg$ is the bitwise right shift operator.

Example Step-by-Step Transition

Let's convert binary $1011_2$ (decimal 11) to Gray code:

  1. Bit 3 (MSB): $g_3 = b_3 = 1$
  2. Bit 2: $g_2 = b_3 \oplus b_2 = 1 \oplus 0 = 1$
  3. Bit 1: $g_1 = b_2 \oplus b_1 = 0 \oplus 1 = 1$
  4. Bit 0 (LSB): $g_0 = b_1 \oplus b_0 = 1 \oplus 1 = 0$

So, the Gray code of $1011_2$ is $1110_{Gray}$.

Frequently Asked Questions

Why is Gray Code called a reflected binary code?

It is called reflected because of its recursive generation pattern. The n-bit Gray code list is constructed by taking the (n-1) bit list, reflecting/reversing it, prefixing the original half with "0"s, and prefixing the reflected/reversed half with "1"s.

Where is Gray Code commonly used in modern engineering?

Gray code is widely used in rotary optical encoders (measuring rotation angles), error correction systems in digital communications (such as QAM modulation), and clock domain crossings in digital logic design to avoid metastability.

Does Gray Code have weights like standard binary?

Unlike standard binary (where columns have weights $8, 4, 2, 1$), standard Gray code is a non-weighted code. The position of the 1s in a Gray code string does not directly represent a power of 2. It must be converted back to standard binary to retrieve its mathematical value.

logo OnlineMiniTools

OnlineMiniTools.com is your ultimate destination for a wide range of web-based tools, all available for free.

Feel free to reach out with any suggestions or improvements for any tool at admin@onlineminitools.com. We value your feedback and are continuously striving to enhance the tool's functionality.

© 2026 OnlineMiniTools . All rights reserved.

Hosted on Hostinger

v1.10.0