Decimal, binary and Gray code three ways, gray = n^(n>>1), one click each side, all in your browser
- Runs locally
- Category Developer & DevOps
- Best for Formatting, validating, shrinking, or inspecting code-adjacent text.
Direction
Gray code sequence (teaching table)
Each row differs from the one above by exactly one bit.
gray = n ^ (n >> 1). Everything runs on plain JavaScript integers in your browser tab.
What this tool does
A free Gray code converter that moves between decimal, plain binary and reflected binary (Gray) code in any direction. Type a decimal number and read its Gray code instantly using the standard formula gray = n ^ (n >> 1); type plain binary and get the same answer; or paste a Gray code string and decode it back to binary and decimal with the bit by bit XOR reduction. Optional bit width zero pads the output to 4, 8 or 16 bits so it lines up with a register or an encoder word. A teaching table prints the Gray sequence for 0..n so you can see the defining property with your own eyes, every neighbour differs by exactly one bit, which is why rotary encoders and Karnaugh maps rely on it. One click copy on every cell and a shareable URL that reopens your exact input. 100% client side, nothing is uploaded.
Tool details
- Input
- Numbers
- The page exposes text boxes, numeric controls, file pickers, or structured inputs depending on the tool.
- Output
- Live result + Copy
- The result area focuses on usable output, with copy, download, or preview actions when supported.
- Privacy
- Browser-side processing
- The main tool logic does not call an external API, so inputs normally stay in the current tab.
- Save / share
- Shareable URL state
- Key settings are encoded in the URL so another person can reopen the same setup.
- Performance budget
- Initial JS <= 9 KB
- No WASM budget is declared, keeping the tool quick to open on mobile.
- Best fit
- Developer & DevOps · Developer
- Category and role tags drive related tools, internal links, and quick fit checks.
How to use
-
1. Input
Paste or drop your content into the tool panel.
-
2. Process
Click the button. All processing is local in your browser.
-
3. Copy / Download
Copy the result or download to disk in one click.
How Gray Code Converter fits into your work
Use it in the small gaps between coding, reviewing, debugging, and shipping.
Developer jobs
- Formatting, validating, shrinking, or inspecting code-adjacent text.
- Preparing snippets for documentation, tickets, commits, or handoff.
- Checking a small payload quickly without switching tools.
Developer checks
- Run irreversible transforms like minify or obfuscate on a copy.
- Keep secrets out of pasted snippets unless the tool explicitly stays local.
- Use your normal tests or linter before shipping transformed code.
Good next steps
These links move the current task into a more complete workflow.
- 1 Unit Converter Convert between length, weight, temperature, area, volume, speed, time — instant, browser-only Open
- 2 Percentage Calculator 5 common percentage calculations — "x% of y", "x is what% of y", percentage change, increase/decrease — instant, browser-only Open
- 3 Number Base Converter Number base converter — binary, octal, decimal, hex, and any base 2-36. Bitwise too. Open
Real-world use cases
Decode an absolute rotary encoder
Your encoder outputs a Gray coded position word and the datasheet insists you must convert it before using it as an index. Flip the tool to Gray to decimal, paste the raw bits straight off the bus, and read the true position. Because the encoder guarantees one bit changes per step, you never see a glitchy intermediate value, and the decoded decimal is the number you actually drop into firmware.
Build a Karnaugh map by hand
Karnaugh maps only work because the row and column headers are in Gray code, so neighbouring cells differ by a single variable. Set the teaching table to print the 2 bit or 3 bit Gray sequence and copy the order straight onto your grid. Now adjacent ones group cleanly and your simplified boolean expression falls out without the off by one wraparound mistakes that plain binary headers cause.
Verify a Gray code implementation in code review
A colleague pushes a function that claims to produce Gray codes. Drop n = 5 into decimal to Gray and confirm it returns 111, then run a few more values against the teaching table. If their output ever differs from this reference, or two neighbours differ by more than one bit, you have caught the bug before it ships into the encoder driver where it would be far harder to trace.
Teach the one bit property to a class
Explaining why Gray code matters lands better when students see it. Open the teaching table, bump the row count up, and walk down the list pointing out that every neighbour flips exactly one digit. Share the URL and each student reopens the same table on their own laptop, then they convert their own birthday number and compare the forward formula against the reverse decode.
Common pitfalls
Printing plain binary and calling it Gray code. The Gray code of 5 is 111, not 101. If your output equals the ordinary binary representation, the n ^ (n >> 1) step was skipped, so always sanity check against a known value like 5 to 111.
Forgetting that decoding is not the same XOR as encoding. Encoding is one shift and one XOR, but decoding XORs every bit with all higher bits. Reusing the encode formula to decode gives wrong answers for any value above 1, which is why this tool has a separate Gray to decimal direction.
Choosing a bit width that is too small for the value. The Gray code of 8 is 1100 and needs four bits, so forcing it into a 3 bit field truncates it and the result is meaningless. Pick Auto when unsure, or a width at least as wide as the largest value you will encode.
Privacy
Every conversion, the forward gray = n ^ (n >> 1) step, the reverse XOR decode, the bit width padding and the teaching table, is plain JavaScript that runs inside your browser tab. No number you enter is sent anywhere and nothing is logged. The one caveat is that the shareable URL encodes your input and direction in the query string, so a link you paste into chat will record those values in the recipient server access log. For anything sensitive use the copy button instead of sharing the URL.
FAQ
Tool combos
Folks in your role tend to reach for these alongside this tool.
- Add Line Numbers Number every line of pasted text — set start, step and separator, zero-pad to align, skip blanks, or strip numbers back off — browser-only
- AES Text Encryptor Encrypt & decrypt text with a password — AES-256-GCM + PBKDF2 via WebCrypto — 100% in your browser, nothing uploaded
- Age Difference Calculator The exact gap between two birthdays — years/months/days, percentage, and the date one person is twice the other's age — browser-only
- AI Model Comparison AI model comparison — 20+ models (GPT / Claude / Gemini / Llama / Qwen) across price, context, speed, capabilities (2026).