User Rating 0.0
Total Usage 0 times
Only 0 and 1 allowed. Max 64 bits per value. One value per line for batch mode.
Enter a binary or Gray code value above and press Convert.
Is this tool helpful?

Your feedback helps us improve.

About

Gray code (reflected binary code) changes only one bit between successive values. This property eliminates transition glitches in rotary encoders, ADCs, and Karnaugh maps where multiple simultaneous bit flips cause spurious intermediate states. A standard binary counter transitioning from 0111 to 1000 flips all four bits. If hardware latches read mid-transition, the sampled value could be any of 16 states. Gray code guarantees exactly one bit changes per step, removing that race condition entirely.

This converter implements the canonical XOR algorithm: the Gray code bit gi equals bi bi1 for each position after the MSB. It handles arbitrary bit widths up to 64 bits. Note: this tool works with unsigned integers only. Gray code has no inherent sign representation, so negative values require a separate encoding convention such as offset binary before conversion.

binary to gray code gray code converter XOR conversion reflected binary code digital logic binary converter

Formulas

Binary to Gray code conversion operates bitwise. The most significant bit (MSB) of the Gray code equals the MSB of the binary input. Every subsequent Gray bit is derived by XOR of the current binary bit with its left neighbor.

g0 = b0
gi = bi1 bi for i = 1, 2, …, n1

The reverse (Gray to Binary) reconstructs each binary bit by XOR of the Gray bit with the previously decoded binary bit:

b0 = g0
bi = bi1 gi for i = 1, 2, …, n1

Where bi is the i-th binary bit (index 0 = MSB), gi is the i-th Gray code bit, denotes exclusive OR (XOR), and n is the total bit count. The Hamming distance between any two consecutive Gray code values is always exactly 1.

Reference Data

DecimalBinaryGray CodeHamming Distance
0000000000
1000100011
2001000111
3001100101
4010001101
5010101111
6011001011
7011101001
8100011001
9100111011
10101011111
11101111101
12110010101
13110110111
14111010011
15111110001
1610000110001
1710001110011
1810010110111
1910011110101
2010100111101
2110101111111
2210110111011
2310111111001
2411000101001
2511001101011
2611010101111
2711011101101
2811100100101
2911101100111
3011110100011
3111111100001

Frequently Asked Questions

Gray code is constructed by reflecting the sequence at each bit-width expansion. For n bits, you take the (n1)-bit Gray sequence, duplicate it in reverse, prepend 0 to the first half and 1 to the second. The mirror point has identical suffixes, so only the new prefix bit differs. This reflection property propagates recursively from the 1-bit base case (0, 1), guaranteeing Hamming distance of exactly 1 at every boundary.
In a standard binary counter, transitioning from 0111 to 1000 requires all four bits to flip simultaneously. Physical switches or sensor tracks never toggle at exactly the same instant. During the transition window, the encoder may read intermediate garbage values like 0000 or 1111. Gray code's single-bit-change property means the reading is either the old valid value or the new valid value. No intermediate spurious states exist.
Yes. Leading zeros are preserved and participate in the XOR chain. However, since the MSB is 0 and XOR with 0 produces the same bit, leading zeros in binary map to leading zeros in Gray code. The converted value is numerically identical whether you input 0011 or 11. Padding matters only for display alignment and when interfacing with fixed-width hardware registers.
Karnaugh maps arrange variable combinations so that adjacent cells differ by exactly one variable. This is the Gray code ordering applied to both row and column headers. The adjacency ensures that any rectangle of 1-cells on the map corresponds to a product term where the differing variables can be eliminated. Without Gray code ordering, visually adjacent cells might differ in multiple variables, breaking the graphical minimization method.
The formula gi = bi1 bi is purely bitwise and has no dependency on total width. It works for any positive integer bit length. This tool supports up to 64 bits. Beyond that, the conversion logic remains valid, but JavaScript's native bitwise operators work on 32-bit integers, so this implementation uses string-level XOR to avoid truncation.
For values that fit in a 32-bit integer, the entire conversion is G = B (B >> 1), where >> is a right shift. This tool implements string-based XOR to support up to 64 bits, but for hardware or embedded systems, the shift-and-XOR single instruction is the standard approach.