ForHosting KIT · Unit converters

Gray code converter

Binary 1011 becomes Gray 1110 here, and Gray converts back to binary just as exactly, with leading zeros kept so a fixed-width answer stays fixed. Useful for encoder tables, Karnaugh maps and logic homework. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Value
1011
Conversion direction: binary_to_gray
Binary to gray
You get
Bits in
1011
Bits out
1110
Integer in
11
Integer out
14

Binary-reflected Gray code labels consecutive integers so that successive codes differ in exactly one bit. That property matters in rotary encoders, Karnaugh maps, error-tolerant state machines, digital communications demos, and classroom exercises where students must map a binary word to its Gray form or recover binary from Gray. This gray code converter accepts a binary bit string or a non-negative whole number, a conversion direction, and returns a deterministic bit string plus structured fields you can pin in automated tests. Non-binary input fails with a clear invalid input error instead of silently truncating garbage digits.

What reflected Gray code is and which inputs are valid

Standard binary is the ordinary base-two representation of a non-negative integer, with place values that are powers of two. Binary-reflected Gray code is a different labeling of the same integers: you still use bits made of zero and one, but consecutive values flip only a single bit.

The classic encoding from binary B to Gray G is the bitwise rule G equals B XOR B shifted right by one bit; decoding accumulates XOR from the most significant bit toward the least significant bit so each binary bit is the parity of the Gray prefix. Send the required field value as a pure bit string of zeros and ones, optionally with a zero-b prefix, or as a non-negative whole integer or integer digit string when you prefer a decimal magnitude.

Leading zeros in a pure bit string are preserved so homework and hardware examples keep a fixed width.

Encoder and embedded tooling notes

Developers documenting rotary encoders, state machines, or sensor position codes generate stable Gray bit strings from binary source values without hand-computing XOR tables.

What happens if I enter a non-binary value?

Bit strings must use only digits 0 and 1 (optional 0b prefix); floats, mixed text, and other junk are rejected and not charged.

Is the gray code converter free?

The browser tool runs free on your device.

Which Gray code variant does this implement?

Binary-reflected Gray code only: encode with XOR of the value and its right shift by one; decode by cumulative XOR from the most significant bit. Other Gray families are out of scope.

How do I convert binary to Gray code by hand?

Shift the binary number one place to the right and combine it with the original bit by bit using exclusive OR. For binary 1011 that gives Gray 1110, which is exactly what the converter returns.

How do I turn Gray code back into binary?

Work from the leftmost bit: each binary bit is the exclusive OR of all Gray bits up to that position. Choose the Gray-to-binary direction and the tool does this for you, keeping the same number of bits.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/fmt/gray-code

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/fmt/gray-code \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"value":"1011","direction":"binary_to_gray"}'
{
  "value": "1011",
  "direction": "binary_to_gray"
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "fmt.gray_code",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

What reflected Gray code is and which inputs are valid

Aliases bits, binary, and n are accepted when your form or SDK already labels the field that way. Direction must be binary_to_gray or to_gray to encode, and gray_to_binary or from_gray to decode; aliases dir and mode work the same. Floating-point values, negative numbers, empty strings, and mixed text that is not binary digits fail with invalid_input because this product promises an exact whole-number conversion, not an approximation. Identical inputs always produce identical JSON: there is no network call, random seed, or wall clock on the path.

Reading the response fields and checking common results by hand

Quick checks build trust: binary one zero one one encodes to Gray one one one zero; Gray zero one one one decodes back to binary zero one zero one; binary zero maps to Gray zero; a three-bit binary one zero zero becomes Gray one one zero. Because every step is XOR on discrete bits, results never drift the way floating-point tricks would for large inputs. On success the response echoes value as the original bit string or safe integer you sent, direction as the canonical binary_to_gray or gray_to_binary label, bits_in as the normalized most-significant-bit-first input string, and bits_out as the converted string you typically want for display, graders, and encoder documentation. integer_in and integer_out interpret those bit strings as non-negative integers when the magnitude fits a safe JSON number, or as decimal digit strings when larger, so pipelines can assert either form. bit_width reports the length of bits_in, and method is always reflected_gray so downstream code can branch without sniffing shapes. Missing value, missing direction, non-binary strings, unsupported direction labels, and oversized bit widths all fail with invalid_input and are not charged after validation rejects the request. Use bits_out alone when a human or report needs a single line; use the full object when automated checkers must assert direction, width, and method as well as the digit string.

API usage, pricing, classroom workflows, and practical limits

Call the free in-browser gray code converter for homework checks, lecture demos, rotary-encoder labs, and quick verification without writing a throwaway script. Prefer classroom-scale widths for graded oracles, and keep bit length inside the published cap for isolate safety. Send a JSON body with the required value and direction fields, or use the documented aliases when your client already names them differently. Use the prepaid API when a tutoring platform, coding-assessment sandbox, firmware toolchain, or documentation generator needs a stable contract under KIT balance at $0.002 per successful item; validation failures never consume balance. Pair this endpoint with binary or hexadecimal formatters when a lesson walks through several encodings, or with truth-table and Karnaugh-map tools when students connect Gray adjacency to single-bit transitions. Authors shipping SDKs can map bits_out directly into UI labels, CLI flags, or generated comments that show the Gray form next to the original binary constant.

Homework and exam verification

Students convert binary words to reflected Gray code and back, compare bit strings, and receive a clear invalid_input error when a non-binary value is submitted by mistake.

Tutoring platforms and auto-graders

Lesson builders and coding sandboxes call a prepaid JSON contract to check binary↔Gray answers at scale without reimplementing reflected Gray logic in every stack.

How do I convert binary to Gray code with this tool?

Send JSON with value set to a bit string or non-negative integer (for example value: "1011") and direction: "binary_to_gray". The bits_out field is the reflected Gray code string.

How do I convert Gray code back to binary?

Use direction "gray_to_binary" (or from_gray) with the Gray bit string in value. The response bits_out is the recovered standard binary string.

Read the full KIT documentation →