Convert Decimal to Gray Code
online decimal to Gray code (reflected binary) converter with bit-width, grouping, and step-by-step XOR breakdown. Client-side, instant.
Encode a non-negative integer as Gray code (reflected binary) using the gray = n XOR (n >> 1) formula. Choose bit width, grouping, and optional 0b prefix. Enable the breakdown to see the decimal → binary → Gray steps.
How to Use Convert Decimal to Gray Code
- Enter decimals, one per line. Non-negative only - Gray code isn't defined for negatives.
- Pick bit width to control zero-padding. Auto uses the minimum bits needed; a fixed width pads shorter values.
- Optional grouping breaks long strings every 4, 8, or 16 bits with spaces. Optional prefix (
0b,%) for pasting into code. - Turn on the breakdown to see the
decimal → binary → Grayderivation row-by-row - the key teaching mechanic. - Press Convert (or Ctrl+Enter). Live preview runs 150 ms after typing stops. Invalid lines are flagged per-line without aborting the batch.
- Test the one-bit property with 0-7 on separate lines: the output
0, 1, 11, 10, 110, 111, 101, 100differs by exactly one bit between consecutive values.
Frequently Asked Questions
What is Gray code in one sentence?
A binary numbering scheme where consecutive values differ by exactly one bit – computed from standard binary by gray = n XOR (n >> 1).
Why does only one bit change between consecutive Gray codes?
Because the XOR operation cancels out all higher bit transitions. When you go from 011 to 100 in binary (3 bits flip), the Gray-code equivalents are 010 to 110 (one bit flip). This property makes Gray code valuable for hardware where multiple simultaneous bit transitions would cause ambiguous readings.
What’s the exact formula?
gray = n ^ (n >> 1) where ^ is XOR and >> is right-shift by 1 bit. Example: for n = 5 (binary 101), n >> 1 = 2 (010), and 101 XOR 010 = 111, so Gray(5) = 111. The formula is its own inverse only via a different expression – see the Gray-to-binary tool.
Where is Gray code actually used?
Absolute rotary encoders (the shaft-angle sensors in robotics and industrial equipment use Gray-coded optical disks so only one sensor transitions at a time, avoiding read glitches). Karnaugh maps for Boolean simplification use Gray-ordered rows and columns. Some error-correction schemes and genetic-algorithm representations use Gray code because a single-bit mutation produces an integer-adjacent value.
Why is it called “reflected binary”?
Because the sequence can be built by mirroring. Start with 0, 1. Reflect to get 0, 1, 1, 0, then prefix 0 to the first half and 1 to the second: 00, 01, 11, 10. Reflect again to double: 00, 01, 11, 10, 10, 11, 01, 00, prefix: 000, 001, 011, 010, 110, 111, 101, 100. The mirror-and-prefix construction guarantees the single-bit-change property at every step.
Is the same formula valid for any bit width?
Yes – gray = n XOR (n >> 1) works for 1 bit, 64 bits, or BigInt. The output width equals the input width (with leading zeros preserved if you’ve picked a fixed width). Gray code doesn’t add or remove bits.
Can I represent negative numbers in Gray code?
Standard Gray code is defined for non-negative integers only. For signed integers you’d usually: (1) convert to two’s complement binary first, (2) then apply Gray encoding on the bit pattern. This tool rejects negatives explicitly. Use a binary-to-gray tool on the two’s-complement bits if you need that workflow.
What’s the decimal → binary → Gray size?
Same as the binary bits. The decimal 5 is 3 bits in binary (101) and 3 bits in Gray (111). A fixed bit width pads both with leading zeros. There’s no compression or expansion – Gray code is a one-to-one remapping of N-bit values.
What about XS-3, BCD-Gray, or signed Gray variants?
Those are domain-specific encodings (Excess-3 Gray used in some old decimal hardware, signed Gray variants for analog-to-digital converters). This tool implements standard reflected binary only. For specialized variants, apply a pre/post transform to the standard Gray output.
Is the tool free, offline, and private?
Yes. Pure JavaScript arithmetic with BigInt in your browser. No upload, no tracking, no account. Load once and it keeps working offline indefinitely.