Skip to main content

Gray Code Converter

Calculate gray code instantly with our math tool. Shows detailed work, formulas used, and multiple solution methods. Get results you can export or share.

Skip to calculator
Mathematics

Gray Code Converter

Convert between decimal, binary, and Gray code. See step-by-step XOR conversion process, bit-by-bit comparison, and full Gray code lookup tables.

Last updated: December 2025Reviewed by NovaCalculator Mathematics Team

Calculator

Adjust values & calculate
Gray Code
1011
Binary: 1101 | Decimal: 13
Binary
1101
Gray Decimal
11
Gray Hex
0xB

XOR Conversion Steps

Binary:1101
Shifted right by 1:0110
XOR Result (Gray):1011

Bit Comparison

Position 3Binary: 1 | Gray: 1 (same)
Position 2Binary: 1 | Gray: 0 (different)
Position 1Binary: 0 | Gray: 1 (different)
Position 0Binary: 1 | Gray: 1 (same)
Your Result
Decimal 13 = Binary 1101 = Gray 1011
Share Your Result
Understand the Math

Formula

Gray = Binary XOR (Binary >> 1)

To convert binary to Gray code, XOR the binary number with itself shifted right by one position. The MSB of Gray code is the same as the MSB of binary. Each subsequent Gray bit is the XOR of the current and previous binary bits. This ensures consecutive Gray code values differ by exactly one bit.

Last reviewed: December 2025

Worked Examples

Example 1: Converting Decimal 13 to Gray Code

Convert decimal 13 to its Gray code representation.
Solution:
Step 1: Convert 13 to binary = 1101 Step 2: Apply Gray code conversion Binary: 1101 Shifted right: 0110 XOR result: 1011 (Gray code) Bit-by-bit: MSB: same as binary = 1 Bit 2: 1 XOR 1 = 0 Bit 1: 0 XOR 1 = 1 Bit 0: 1 XOR 0 = 1 Gray code: 1011 Verification: Gray decimal value = 11
Result: Decimal 13 = Binary 1101 = Gray Code 1011

Example 2: Converting Gray Code 1011 Back to Binary

Convert Gray code 1011 back to its binary and decimal equivalents.
Solution:
Step-by-step Gray to Binary: MSB: Gray[0] = 1, Binary[0] = 1 Bit 1: Gray[1] XOR Binary[0] = 0 XOR 1 = 1 Bit 2: Gray[2] XOR Binary[1] = 1 XOR 1 = 0 Bit 3: Gray[3] XOR Binary[2] = 1 XOR 0 = 1 Binary result: 1101 Decimal: 8 + 4 + 0 + 1 = 13
Result: Gray Code 1011 = Binary 1101 = Decimal 13
Expert Insights

Background & Theory

The Gray Code Converter applies the following established principles and formulas. Mathematics rests on a hierarchy of number systems, each extending the previous. The natural numbers (1, 2, 3, ...) support counting and ordering. The integers add negative values and zero, enabling subtraction without restriction. The rational numbers, expressible as p/q where p and q are integers and q is nonzero, close the system under division. The real numbers fill the gaps left by irrationals such as the square root of 2 or pi, forming a complete ordered field. The complex numbers, written as a + bi where i is the square root of negative one, complete the algebraic closure of the reals and allow every polynomial to have a root. Prime factorization states that every integer greater than one is uniquely expressible as a product of primes, a result known as the Fundamental Theorem of Arithmetic. Computing the greatest common divisor (GCD) of two integers relies most efficiently on the Euclidean algorithm: repeatedly replace the larger number with the remainder when it is divided by the smaller, until the remainder is zero. The last nonzero remainder is the GCD. The least common multiple (LCM) follows from the identity LCM(a, b) = |a * b| / GCD(a, b). Modular arithmetic defines equivalence classes of integers that share the same remainder under division by a modulus n. Fermat's Little Theorem and Euler's Theorem arise from this structure and underpin modern cryptography. Logarithms are the inverses of exponential functions. If b raised to the power x equals y, then the logarithm base b of y equals x. The natural logarithm uses base e, approximately 2.71828. Combinatorics counts arrangements and selections. The number of ordered arrangements (permutations) of r objects from n distinct objects is nPr = n! / (n - r)!. The number of unordered selections (combinations) is nCr = n! / (r! * (n - r)!). Pascal's triangle arranges these binomial coefficients so that each entry equals the sum of the two entries directly above it. The Fibonacci sequence, defined by F(1) = 1, F(2) = 1, and F(n) = F(n-1) + F(n-2), appears throughout nature and connects deeply to the golden ratio via Binet's formula.

History

The history behind the Gray Code Converter traces back through the following developments. Mathematics as a systematic discipline traces to ancient Mesopotamia. Babylonian clay tablets dating to around 1800 BCE demonstrate knowledge of quadratic equations, Pythagorean triples, and base-60 arithmetic, suggesting a practical mathematical tradition far preceding Greek formalism. Euclid of Alexandria compiled the Elements around 300 BCE, establishing the axiomatic method that would define rigorous mathematics for over two thousand years. His work organized plane geometry, number theory, and proportion into logically chained propositions derived from a small set of postulates. The algorithm bearing his name for computing GCDs appears in Book VII and remains in use today. In the 9th century, the Persian scholar Muhammad ibn Musa Al-Khwarizmi wrote Al-Kitab al-mukhtasar fi hisab al-jabr wal-muqabala, the treatise whose title gave algebra its name. He systematized the solution of linear and quadratic equations and described procedures that operated on unknowns as objects, a conceptual leap away from purely numerical calculation. Rene Descartes introduced coordinate geometry in 1637 by uniting algebra and Euclidean geometry, allowing curves to be studied through equations. This synthesis set the stage for calculus. Isaac Newton and Gottfried Wilhelm Leibniz independently developed calculus during the 1660s and 1670s, triggering a priority dispute that lasted decades and divided British and Continental mathematicians. Carl Friedrich Gauss proved the Fundamental Theorem of Algebra in 1799, showing that every nonconstant polynomial has at least one complex root. His Disquisitiones Arithmeticae of 1801 established modern number theory. David Hilbert's formalist program at the turn of the 20th century sought to place all of mathematics on an explicit axiomatic foundation, a project that Kurt Godel's incompleteness theorems of 1931 showed to be fundamentally limited. Alan Turing's work in the 1930s on computability introduced the theoretical model of the stored-program computer and linked mathematical logic directly to the limits of algorithmic calculation. His proof that no algorithm can decide in general whether an arbitrary program will halt or run forever placed fundamental boundaries on what mathematics can mechanically determine, and it opened the discipline now known as theoretical computer science.

Share this calculator

Explore More

Frequently Asked Questions

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 at Bell Labs in 1947, originally for use in pulse-code communication systems. The key property of Gray code is that it eliminates the risk of errors during transitions between adjacent values. In standard binary, transitioning from 7 (0111) to 8 (1000) requires all four bits to change simultaneously, and if the bits change at slightly different times, transient incorrect values can appear. Gray code avoids this by ensuring only one bit changes at a time, making transitions reliable. This property makes Gray code essential in rotary encoders, analog-to-digital converters, and error correction systems.
Converting binary to Gray code follows a straightforward algorithm. The most significant bit (MSB) of the Gray code is the same as the MSB of the binary number. For each subsequent bit position, the Gray code bit is the XOR (exclusive OR) of the current binary bit and the previous binary bit (one position to the left). This can also be expressed as a single operation: take the binary number, shift it right by one position, and XOR the result with the original binary number. For example, binary 1101 (decimal 13): shift right gives 0110, XOR of 1101 and 0110 gives 1011, which is the Gray code. This operation is extremely fast in hardware, requiring just a single XOR gate per bit and a single shift operation.
Gray code has numerous practical applications in engineering and computing. Rotary encoders, which measure angular position in motors, robotics, and CNC machines, use Gray code patterns on their encoding disks to ensure accurate position reading during rotation. Karnaugh maps in digital logic design use Gray code ordering on their axes to ensure that adjacent cells differ by only one variable, facilitating visual simplification of Boolean expressions. Analog-to-digital converters use Gray code to prevent glitch errors during voltage transitions. Genetic algorithms in computer science use Gray code encoding because single-bit mutations correspond to small value changes, improving search performance. Communication systems use Gray coding in QAM (Quadrature Amplitude Modulation) to minimize bit errors.
The defining property of Gray code is that consecutive values always have a Hamming distance of exactly 1, meaning they differ in precisely one bit position. The Hamming distance between two binary strings is the number of positions at which the corresponding bits differ. In standard binary counting, consecutive numbers can differ in many bits simultaneously: going from 7 (0111) to 8 (1000) has a Hamming distance of 4. This property of Gray code prevents transient errors in mechanical and electronic systems where multiple bits changing simultaneously could cause momentary incorrect readings. The property also makes Gray code cyclic: the last value and the first value (in an n-bit Gray code sequence) also differ by exactly one bit, making it ideal for circular applications.
Karnaugh maps (K-maps) use Gray code ordering on both axes to ensure that physically adjacent cells in the map correspond to minterms that differ by exactly one variable. This arrangement is critical because it allows groups of adjacent ones (representing simplified product terms) to be visually identified. Without Gray code ordering, adjacent cells might represent minterms that differ in multiple variables, breaking the visual grouping property. For a 4-variable K-map, the rows might be ordered as 00, 01, 11, 10 (Gray code) rather than 00, 01, 10, 11 (binary). This ordering means that wrapping around the edges also maintains the single-bit-difference property. K-maps are one of the most intuitive methods for simplifying Boolean expressions with up to six variables.
Reflected binary code is another name for Gray code, derived from its recursive construction method. To build an n-bit Gray code from an (n-1)-bit Gray code, you list the (n-1)-bit codes in order, then reflect (reverse) the list and append it below the original. Prefix all entries in the original list with 0 and all entries in the reflected list with 1. For example, the 1-bit Gray code is just 0 and 1. Reflecting gives 1 and 0, prefixing gives 00, 01, 11, 10, which is the 2-bit Gray code. Reflecting the 2-bit code and prefixing gives the 3-bit code: 000, 001, 011, 010, 110, 111, 101, 100. This elegant recursive structure guarantees the single-bit-difference property and is why the code is called reflected binary code.
Educational Note: This calculator is provided for educational and informational purposes. Results are based on the formulas and inputs provided. Always verify important calculations independently. NovaCalculator processes calculator inputs client-side; optional analytics follow visitor consent settings.Reviewed by: NovaCalculator Mathematics Team โ€” Verified against standard mathematical and scientific references. Last reviewed: December 2025. ยฉ 2024โ€“2026 NovaCalculator.

Share this calculator

Formula

Gray = Binary XOR (Binary >> 1)

To convert binary to Gray code, XOR the binary number with itself shifted right by one position. The MSB of Gray code is the same as the MSB of binary. Each subsequent Gray bit is the XOR of the current and previous binary bits. This ensures consecutive Gray code values differ by exactly one bit.

Worked Examples

Example 1: Converting Decimal 13 to Gray Code

Problem: Convert decimal 13 to its Gray code representation.

Solution: Step 1: Convert 13 to binary = 1101\nStep 2: Apply Gray code conversion\n Binary: 1101\n Shifted right: 0110\n XOR result: 1011 (Gray code)\n\nBit-by-bit:\n MSB: same as binary = 1\n Bit 2: 1 XOR 1 = 0\n Bit 1: 0 XOR 1 = 1\n Bit 0: 1 XOR 0 = 1\n\nGray code: 1011\nVerification: Gray decimal value = 11

Result: Decimal 13 = Binary 1101 = Gray Code 1011

Example 2: Converting Gray Code 1011 Back to Binary

Problem: Convert Gray code 1011 back to its binary and decimal equivalents.

Solution: Step-by-step Gray to Binary:\n MSB: Gray[0] = 1, Binary[0] = 1\n Bit 1: Gray[1] XOR Binary[0] = 0 XOR 1 = 1\n Bit 2: Gray[2] XOR Binary[1] = 1 XOR 1 = 0\n Bit 3: Gray[3] XOR Binary[2] = 1 XOR 0 = 1\n\nBinary result: 1101\nDecimal: 8 + 4 + 0 + 1 = 13

Result: Gray Code 1011 = Binary 1101 = Decimal 13

Frequently Asked Questions

What is Gray code and why was it invented?

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 at Bell Labs in 1947, originally for use in pulse-code communication systems. The key property of Gray code is that it eliminates the risk of errors during transitions between adjacent values. In standard binary, transitioning from 7 (0111) to 8 (1000) requires all four bits to change simultaneously, and if the bits change at slightly different times, transient incorrect values can appear. Gray code avoids this by ensuring only one bit changes at a time, making transitions reliable. This property makes Gray code essential in rotary encoders, analog-to-digital converters, and error correction systems.

How do you convert binary to Gray code?

Converting binary to Gray code follows a straightforward algorithm. The most significant bit (MSB) of the Gray code is the same as the MSB of the binary number. For each subsequent bit position, the Gray code bit is the XOR (exclusive OR) of the current binary bit and the previous binary bit (one position to the left). This can also be expressed as a single operation: take the binary number, shift it right by one position, and XOR the result with the original binary number. For example, binary 1101 (decimal 13): shift right gives 0110, XOR of 1101 and 0110 gives 1011, which is the Gray code. This operation is extremely fast in hardware, requiring just a single XOR gate per bit and a single shift operation.

Where is Gray code used in real-world applications?

Gray code has numerous practical applications in engineering and computing. Rotary encoders, which measure angular position in motors, robotics, and CNC machines, use Gray code patterns on their encoding disks to ensure accurate position reading during rotation. Karnaugh maps in digital logic design use Gray code ordering on their axes to ensure that adjacent cells differ by only one variable, facilitating visual simplification of Boolean expressions. Analog-to-digital converters use Gray code to prevent glitch errors during voltage transitions. Genetic algorithms in computer science use Gray code encoding because single-bit mutations correspond to small value changes, improving search performance. Communication systems use Gray coding in QAM (Quadrature Amplitude Modulation) to minimize bit errors.

What is the Hamming distance property of Gray code?

The defining property of Gray code is that consecutive values always have a Hamming distance of exactly 1, meaning they differ in precisely one bit position. The Hamming distance between two binary strings is the number of positions at which the corresponding bits differ. In standard binary counting, consecutive numbers can differ in many bits simultaneously: going from 7 (0111) to 8 (1000) has a Hamming distance of 4. This property of Gray code prevents transient errors in mechanical and electronic systems where multiple bits changing simultaneously could cause momentary incorrect readings. The property also makes Gray code cyclic: the last value and the first value (in an n-bit Gray code sequence) also differ by exactly one bit, making it ideal for circular applications.

How does Gray code relate to Karnaugh maps?

Karnaugh maps (K-maps) use Gray code ordering on both axes to ensure that physically adjacent cells in the map correspond to minterms that differ by exactly one variable. This arrangement is critical because it allows groups of adjacent ones (representing simplified product terms) to be visually identified. Without Gray code ordering, adjacent cells might represent minterms that differ in multiple variables, breaking the visual grouping property. For a 4-variable K-map, the rows might be ordered as 00, 01, 11, 10 (Gray code) rather than 00, 01, 10, 11 (binary). This ordering means that wrapping around the edges also maintains the single-bit-difference property. K-maps are one of the most intuitive methods for simplifying Boolean expressions with up to six variables.

What is reflected binary code and how is it constructed?

Reflected binary code is another name for Gray code, derived from its recursive construction method. To build an n-bit Gray code from an (n-1)-bit Gray code, you list the (n-1)-bit codes in order, then reflect (reverse) the list and append it below the original. Prefix all entries in the original list with 0 and all entries in the reflected list with 1. For example, the 1-bit Gray code is just 0 and 1. Reflecting gives 1 and 0, prefixing gives 00, 01, 11, 10, which is the 2-bit Gray code. Reflecting the 2-bit code and prefixing gives the 3-bit code: 000, 001, 011, 010, 110, 111, 101, 100. This elegant recursive structure guarantees the single-bit-difference property and is why the code is called reflected binary code.

References

Reviewed by Manoj Kumar, Mathematics Educator ยท Editorial policy