Convert between decimal, binary, BCD and Gray code in either direction.
Converted locally in your browser.
What is the difference between BCD and binary?
BCD gives each decimal digit its own 4-bit group, so 1990 is 0001 1001 1001 0000, whereas plain binary is 11111000110. BCD uses about 20% more bits but makes driving decimal displays trivial. Gray code differs again: consecutive values change by exactly one bit, computed as n XOR (n >> 1).
Understanding your result
BCD and binary are frequently confused but solve different problems. BCD wastes space — four bits per digit, and six of the sixteen possible patterns are unused — but it makes converting to a decimal display trivial and avoids the rounding surprises of binary fractions, which is why it persists in calculators, digital clocks and financial hardware. Gray code exists for a completely different reason: consecutive values differ by exactly one bit, so a sensor read midway through a transition can only ever be off by one step. With plain binary, moving from 7 (0111) to 8 (1000) flips every bit at once, and a reading caught mid-change could return almost any value — which is why rotary encoders and position sensors use Gray code instead.
Formula and method
BCD encodes each decimal digit separately as 4 bits, so 1990 becomes 0001 1001 1001 0000. Gray code is computed as g = n XOR (n >> 1); converting back repeatedly XORs the value with itself shifted right until the shift exceeds the bit width.
Assumptions and limitations
Handles non-negative whole numbers up to the 32-bit signed limit of 2,147,483,647. Negative numbers, fractional values and packed formats such as signed BCD or excess-3 are not supported. BCD here means the standard 8421 weighting; other BCD variants encode digits differently and will not match.
Worked example
Decimal 1990 is binary 11111000110, BCD 0001 1001 1001 0000, and Gray code 10000100101 — note that BCD needs 16 bits where plain binary needs only 11.
How to use this tool
- Choose which representation you are entering.
- Type the value — spaces between bit groups are fine.
- Read all four representations in the result table.
- Check the worked steps to see how the Gray conversion was done.
Common mistakes to avoid
- Assuming BCD and binary are the same encoding.
- Writing BCD groups containing 1010–1111, which are invalid.
- Splitting BCD into groups of anything other than four bits.
- Reading Gray code as if it were plain binary.
About the BCD & Gray Code Converter
This converter moves a value between four representations: decimal, plain binary, binary-coded decimal (BCD) and Gray code. Enter a value in any of them and see all the others, along with the hexadecimal and octal forms and the number of bits required.
Who should use this tool
Electronics and digital-logic students, embedded developers and anyone working with encoders or seven-segment displays.
Benefits
- Converts in all four directions, not just from decimal.
- Rejects invalid BCD groups (1010–1111) instead of producing nonsense.
- Shows the worked XOR step behind the Gray code conversion.
- Gives hexadecimal, octal and bit width in the same result.
Practical use cases
- Checking coursework on digital logic encodings.
- Decoding a value read from a Gray-coded rotary encoder.
- Preparing BCD values for a seven-segment display driver.
- Debugging firmware that stores numbers in BCD.
Explore all Developer Tools tools
Frequently asked questions
What is BCD used for?
Driving seven-segment displays and doing exact decimal arithmetic. Because each digit is stored separately, showing a number needs no binary-to-decimal conversion and no rounding error creeps in.
Why does Gray code change only one bit at a time?
That is its defining property, and the reason it exists. A sensor read during a transition can only be off by one step, whereas plain binary can flip several bits at once and produce a wildly wrong reading.
Is BCD less efficient than binary?
Yes. Four bits encode a digit but six of the sixteen patterns go unused, so BCD needs roughly 20% more bits than plain binary for the same value.
Which BCD codes are invalid?
The groups 1010 through 1111, which would represent 10 to 15. A decimal digit only goes up to 9, so those patterns are rejected as errors.
Where is Gray code used in practice?
Rotary and linear position encoders, Karnaugh maps in logic minimisation, and error-correction schemes — anywhere a value is read while it might be changing.