Intermediate

Gray Code Converter (Binary ↔ Gray)

Convert any value between standard binary and reflected Gray code at a chosen bit width.

Conversion mode

Direction of conversion
Interpreted as a bit pattern at the chosen width

bits

1 to 16 bits
Gray code (decimal)
5

Output bit pattern: 0101

Conversion
0110 → 0101
Input (decimal)6
Output (decimal)5
Input binary
0110
Output binary
0101
Bit width
4 bits
Results are estimates for general information only and are not professional advice — always verify important results independently before relying on them. Read the full disclaimer.
Quick answer

How does this calculator work?

Binary to Gray code uses g = b XOR (b >> 1); Gray to binary XOR-folds the value with each right shift. Enter a decimal value and bit width, choose the direction, and the converter returns the result as both a fixed-width binary string and a decimal number, where neighboring integers always differ by one bit.

Formula
Binary→Gray: g = b XOR (b >> 1). Gray→Binary: b = g XOR (g >> 1) XOR (g >> 2) XOR …
How this is calculated

Pick a conversion mode, enter a value, and set the bit width. The value is interpreted as an integer bit pattern and masked to the chosen width, so only the low bits matter. The result is shown both as a binary string at that width and as a decimal number.

Binary to Gray is a single step: each output bit is the XOR of the binary bit and the next-higher binary bit, which is exactly g = b XOR (b >> 1). Gray to binary reverses this by XOR-folding the Gray value with all of its right shifts (shift = 1, 2, 4, …), equivalent to computing each bit from the most significant downward as b[i] = g[i] XOR b[i+1].

Reflected Gray code has the defining property that successive integers differ in exactly one bit, which makes it useful for rotary encoders, error reduction, and Karnaugh maps. The width is limited to 1 to 16 bits here so the patterns stay within safe 32-bit integer arithmetic; values larger than the width simply wrap via masking, and negative inputs use their absolute value.

Frequently asked questions

Because g = b XOR (b >> 1) is constructed so that incrementing the underlying binary value flips exactly one Gray bit, which avoids spurious intermediate states in hardware like position encoders.

It fixes how many bits are displayed and masks the input to that many low bits. A value of 6 at width 4 is 0110; the same 6 at width 8 is 00000110.

Yes. This converter uses the reflected binary Gray code, the most common variant, defined by g = b XOR (b >> 1).

Also known as

gray code converter
binary to gray
gray to binary
reflected binary code
gray code calculator
convert gray code
gray code encoder

APA

TG we-Calculate Editorial Team. (2026). Gray Code Converter (Binary ↔ Gray) [Online calculator]. TG we-Calculate. https://we-calculate.com/calculator/gray-code-converter

Chicago

TG we-Calculate Editorial Team. "Gray Code Converter (Binary ↔ Gray)." TG we-Calculate. 2026. https://we-calculate.com/calculator/gray-code-converter.

IEEE

TG we-Calculate Editorial Team, "Gray Code Converter (Binary ↔ Gray)," TG we-Calculate, 2026. [Online]. Available: https://we-calculate.com/calculator/gray-code-converter

BibTeX

@misc{wecalculate_gray_code_converter, title = {Gray Code Converter (Binary ↔ Gray)}, author = {{TG we-Calculate Editorial Team}}, howpublished = {\url{https://we-calculate.com/calculator/gray-code-converter}}, year = {2026}, note = {TG we-Calculate} }

Did this calculator help you?