About the Gray Code and BCD Converter
Type a whole number in decimal, binary, octal, hexadecimal, Gray code or BCD and see it written in all six forms at once. Gray code is a way of counting in binary where only one bit changes from one number to the next, which prevents false readings in rotary encoders and Karnaugh maps. BCD, binary-coded decimal, stores each decimal digit as its own four bits, which suits clocks, meters and calculators. The page accepts numbers up to 300 digits, pads binary and Gray output to 4, 8, 16 or 32 bits when you ask, and includes a Gray code table for up to five bits. It handles whole numbers only: there are no negative numbers, fractions or packed BCD with sign nibbles. The BCD form is plain 8421.
Updated September 2026
How to use the Gray Code and BCD Converter
- 1Choose the form your number is in from Your number is in, for example Decimal or Gray code.
- 2Type the number. Spaces, 0x and 0b prefixes are accepted where they make sense.
- 3Read the same value in decimal, binary, octal, hexadecimal, Gray code and BCD.
- 4Use Pad binary and Gray to for a fixed width such as 8 bits.
- 5Press Copy beside any row, or open the Gray code table to see the whole sequence.
What Gray code is for
In ordinary binary, counting from 7 to 8 changes four bits at once, from 0111 to 1000. If the bits are read by separate sensors that do not switch at the exact same moment, a wrong value can appear for a split second. Gray code avoids this: any two neighbouring numbers differ in a single bit, so the worst case is a reading that is one step out.
That is why absolute rotary encoders, position sensors and some analogue-to-digital converters output Gray code. Digital designers also use it to label Karnaugh maps and to pass a counter safely between two clocks.
BCD and everyday numbers
Binary-coded decimal keeps each decimal digit separate. Because each digit is its own nibble, displays, clocks, meters and cash registers can show a digit with almost no arithmetic. The price is a little more storage: a two-digit number needs eight bits in BCD but only seven in binary.
- Digits are 0 to 9, written 0000 to 1001
- Bit patterns 1010 to 1111 are not valid BCD digits
- 429 in BCD is 0100 0010 1001
- This page uses plain 8421 BCD, not Excess-3 or packed signed forms
Working between number systems
Choose the form you have, and the page fills all the others, which is handy for lab exercises and digital logic homework. For other bases or a custom base up to 36, use the Number Base Converter. For arithmetic, bitwise operations and two's complement, try the Binary and Hex Calculator.
Because the tool uses exact big-number arithmetic, long values stay precise instead of rounding to the last digits as ordinary floating-point numbers do. Plain text can go to bits with the Binary Translator, and the Roman Numeral Converter is the one for numerals written in letters.
Features
- Six forms at once: decimal, binary, octal, hex, Gray code and BCD
- Works both ways: Gray to binary, BCD to decimal and so on
- Numbers up to 300 digits, exact with no rounding
- Padding to 4, 8, 16 or 32 bits
- Binary and Gray shown in groups of four bits
- Gray code table for 1 to 5 bits
- Plain messages for invalid BCD groups or wrong digits
- Runs in your browser with nothing sent anywhere
Frequently asked questions
How do I convert binary to Gray code?
Keep the first bit, then XOR each bit with the bit to its left. For binary 1010 the Gray code is 1111. In code it is the number XOR the number shifted right by one, which is what this page calculates.
How do I convert Gray code back to binary?
Keep the first bit, then XOR each new Gray bit with the binary bit you just found. Gray 1111 becomes 1010. Choose Gray code in the first list, type the bits and the binary row shows the answer.
What is BCD and how is it different from binary?
BCD writes each decimal digit as its own four bits, so 429 is 0100 0010 1001. Ordinary binary writes the whole number as one value, so 429 is 110101101. BCD wastes some bit patterns, 1010 to 1111, but it converts to and from decimal digits very easily.
Why is Gray code used?
Only one bit changes between neighbours, so a sensor caught between two positions gives a value that is off by one rather than wildly wrong. Rotary encoders and shaft position sensors use it for this reason, and Karnaugh maps are labelled in Gray order for the same property.
Why does my BCD input give an error?
Each group of four bits must be between 0000 and 1001, the digits 0 to 9, and the total length must be a multiple of four. A group such as 1010 is not a decimal digit, so the tool names it in the message instead of guessing.
Does the converter handle negative numbers or fractions?
No. It converts whole, non-negative numbers only. Signed BCD and fractional Gray codes are defined differently in different systems, so this page does not guess at them.
What does padding to 8 bits do?
It adds leading zeros to the binary and Gray results so they are at least 8 bits long, such as 00001010. The value does not change. Numbers that need more bits than you chose keep their full length.