Gray code, BCD and Excess-3 converters with full tables – the classic codes of digital circuits, encoders and decimal hardware. Everything runs locally in your browser.
- Binary to Gray Code Converter
Convert weighted binary to reflected Gray code with g = b XOR (b >> 1) – in a Gray sequence, consecutive values differ in exactly one bit, which kills glitches in rotary encoders.
- Gray Code to Binary Converter
Convert reflected Gray code back to weighted binary by cascading XOR from the top bit down – each binary bit is the XOR of the previous binary bit with the current Gray bit.
- Decimal to Gray Code Converter
Convert a decimal number straight to Gray code – first to binary, then one XOR pass.
- Gray Code Table Generator
Generate the full decimal-binary-Gray table for 2 to 6 bits.
- BCD to Decimal Converter
Decode binary-coded decimal: each 4-bit group is one decimal digit 0-9.
- Decimal to BCD Converter
Encode a decimal number as BCD – each digit becomes its own 4-bit group, so 429 becomes 0100 0010 1001.
- Excess-3 Code Converter
Convert between decimal and Excess-3 code, where each digit is stored as digit + 3 in four bits – a self-complementing code from the era of decimal arithmetic hardware.
Digital design speaks in codes tuned for hardware: Gray code for glitch-free counting, BCD for digit-exact decimal, Excess-3 for complement-friendly arithmetic. The converters and table generator below implement each precisely, with validity checking that names any illegal group.
Useful for coursework, encoder work and reading datasheets that assume the codes.
Codes built for hardware, not arithmetic
The codes in this family exist because circuits have constraints that pure binary ignores. Gray code reorders the counting sequence so that consecutive values differ in exactly one bit. That matters on the shaft encoder of a motor or a rotary knob: if several bits changed at once, tiny timing differences between the bit sensors would produce brief garbage readings, but with Gray code the worst possible misread during a transition is one position off. The converters here show the XOR chain that maps between binary and Gray in both directions.
BCD takes the opposite trade. Instead of encoding the whole number in binary, it encodes each decimal digit separately in four bits, so 429 is stored as 0100 0010 1001. That wastes six of the sixteen patterns per digit, but it makes decimal display drivers and exact decimal arithmetic trivial — which is why calculators and seven-segment displays used it for decades. Excess-3 is BCD’s self-complementing cousin: storing each digit as digit-plus-three means inverting the bits gives the nine’s complement, a genuinely useful trick in the era of decimal arithmetic hardware.
Two details round out the family. The reflected Gray sequence is cyclic: the last code and the first also differ in just one bit, which is what lets a rotary encoder cross zero cleanly. And BCD needs its own arithmetic rule — when adding two BCD digits produces a result above nine, hardware adds six to skip the ten unused patterns, the correction the classic decimal-adjust instructions performed. The table generator here prints the full decimal, binary and Gray columns so both properties can be checked by eye.