Xerobit

Gray Code Converter: Binary to Gray and Gray to Binary

Enter a binary string to get its gray code equivalent, or enter a reflected binary value to decode it back to standard binary. The converter also generates an n-bit reference table so you can verify each value at a glance. Everything runs in your browser and nothing is uploaded.

Result

  • Input (Binary)
    1011
  • Output (Gray code)
    1110
  • Bit length
    4
DecimalBinaryGray code
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000

How to use the gray code converter

  1. Select the conversion direction: Binary to Gray or Gray to Binary.
  2. Type your binary or reflected binary string into the input field using only 0s and 1s.
  3. Read the converted result immediately below the input.
  4. Adjust the Table bit width field to generate a reference table for that many bits.

How the conversion works

The standard gray code (also called reflected binary code) ensures that consecutive values differ by exactly one bit. To convert binary to gray, keep the most significant bit unchanged, then XOR each subsequent bit with the bit to its left. In formula form: G[i] = B[i] XOR B[i+1].

For example, converting the binary value 1011:

  • Keep the first bit: 1
  • XOR bit 1 and bit 0: 1 ^ 0 = 1
  • XOR bit 0 and bit 1: 0 ^ 1 = 1
  • XOR bit 1 and bit 1: 1 ^ 1 = 0

The resulting sequence is 1110. You can verify this programmatically with the expression n ^ (n >> 1), where n is the decimal integer. For 11 (decimal), 11 ^ (11 >> 1) equals 1011 ^ 0101 = 1110.

Decoding back to binary

To reverse the process and convert gray to binary, start with the most significant bit of the encoded string as the first binary bit. Then, for each subsequent position, XOR the current bit with the previously computed binary bit.

Using the sequence 1110 from above:

  • First binary bit = first bit: 1
  • Second binary bit = 1 ^ 1 = 0
  • Third binary bit = 0 ^ 1 = 1
  • Fourth binary bit = 1 ^ 0 = 1

This recovers the original binary value 1011. Unlike encoding, which uses adjacent bits of the same number, decoding requires a running XOR across the entire string.

4-bit reference table

The table below shows all 16 values of a 4-bit reflected binary sequence. Notice how each row differs from the one above it by exactly one bit, which is the defining property of this encoding.

DecimalBinaryReflected binary
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000

Why use this encoding instead of standard binary

In standard binary counting, transitioning between certain values flips multiple bits simultaneously. For instance, moving from 7 (0111) to 8 (1000) changes all four bits. If these bits control physical switches or sensors, slight timing differences can cause the system to briefly read an incorrect intermediate state.

A single-bit transition scheme eliminates this problem because only one bit changes per step. This makes it essential in rotary encoders, analog-to-digital converters, and Karnaugh maps used in digital logic design. It is also applied in error correction and communication protocols where minimizing transition errors improves reliability. For other encoding tasks like character mapping, tools such as the Baudot code converter handle teleprinter alphabets, while general base conversions are better served by a number base converter.

Common mistakes to avoid

A frequent error is treating reflected binary as a numeric base. It is not a positional number system like binary, octal, or hexadecimal. You cannot perform arithmetic directly on these values without first converting them to standard binary. Attempting to add two encoded strings together will yield meaningless results.

Another mistake is confusing the encoding direction. Remember that binary to gray uses adjacent bits of the original binary number, while gray to binary uses a cumulative XOR chain. Mixing up these two algorithms is the most common source of manual calculation errors.

Worked examples

ExampleInputResult
Binary 1011 to GrayConversion direction: b2g; Input value: 1011; Table bit width (1-8): 4Input (Binary): 1011; Output (Gray code): 1110; Bit length: 4
Gray 1101 to BinaryConversion direction: g2b; Input value: 1101; Table bit width (1-8): 4Input (Gray code): 1101; Output (Binary): 1001; Bit length: 4
3-bit reference tableConversion direction: b2g; Input value: 000; Table bit width (1-8): 3Input (Binary): 000; Output (Gray code): 000; Bit length: 3
Single bit conversionConversion direction: b2g; Input value: 1; Table bit width (1-8): 1Input (Binary): 1; Output (Gray code): 1; Bit length: 1

FAQ

What is gray code?

It is a binary numeral system, also known as reflected binary code, where two successive values differ in only one bit. It is widely used in hardware encoders and digital logic to prevent spurious output during transitions.

How do I convert binary to gray code?

Keep the most significant bit the same. Then, for each remaining bit, compute the XOR of that bit and the bit immediately to its left in the original binary string.

How do I convert gray code to binary?

The first binary bit matches the first bit of the encoded string. Each subsequent binary bit is the XOR of the current bit and the previously calculated binary bit.

Can I do math with reflected binary values?

No. This format is an encoding, not a positional number system. You must convert it to standard binary or decimal before performing any arithmetic operations.

Is this tool secure for sensitive data?

Yes. The gray code converter runs entirely in your browser. No input or output is sent to any server, making it safe for local development and testing.

Where is this encoding used in practice?

It is commonly found in rotary and optical encoders, Karnaugh maps for simplifying Boolean algebra, genetic algorithms, and clock domain crossings in digital circuit design.