Digital Codes
Not every binary encoding exists for compact arithmetic. This page covers a handful of specialized bit patterns that solve specific problems — mechanical safety, human readability, error detection — that plain positional binary doesn't address at all.
Gray code
Ordinary binary counting can flip multiple bits between adjacent values — 0011 (3) to 0100 (4) flips three bits at once. In real hardware, those bits never change at exactly the same instant, so a circuit reading the value mid-transition can briefly see a completely wrong intermediate code (0111, 0000, or several others, depending on which bit happens to settle first).
Gray code is constructed so that only one bit changes between any two consecutive values.
| Decimal | Binary | Gray |
|---|---|---|
| 0 | 000 | 000 |
| 1 | 001 | 001 |
| 2 | 010 | 011 |
| 3 | 011 | 010 |
| 4 | 100 | 110 |
| 5 | 101 | 111 |
| 6 | 110 | 101 |
| 7 | 111 | 100 |
Converting binary to Gray code is a simple rule: the most significant Gray bit equals the most significant binary bit; every other Gray bit is the XOR of that binary bit and the one to its left (Gᵢ = Bᵢ ⊕ Bᵢ₊₁).
Why it matters in real hardware: rotary position encoders (mechanical shaft-angle sensors) use Gray code on their output contacts specifically so that a reading taken mid-transition, between two adjacent positions, differs from a correct reading by at most one bit — never a wildly wrong value. The same single-bit-change property is also why Gray code sequencing shows up inside FIFO pointer designs that cross clock domains, a technique covered in more depth once sequential logic and clock-domain concepts are established later in this topic.
Binary-Coded Decimal (BCD)
BCD encodes each decimal digit individually as its own 4-bit binary group, rather than converting the whole number to binary at once.
Decimal 47 in BCD:
4 → 0100
7 → 0111
BCD: 0100 0111 (8 bits total)
Decimal 47 in pure binary:
101111 (only 6 bits)
BCD is deliberately less space-efficient than pure binary — 8 bits to represent a range pure binary covers in 6 — but that's the trade it's making: every digit maps back to its decimal meaning independently, with no base-conversion step needed to display or debug the value. That's exactly the property that makes it a natural fit for seven-segment displays, calculators, and financial hardware where exact decimal semantics (no binary rounding of decimal fractions — recall the non-terminating 0.1 case from the previous page) matter more than density.
Excess-3 code
Excess-3 encodes a decimal digit d as the binary value of d + 3.
| Decimal | Excess-3 |
|---|---|
| 0 | 0011 |
| 1 | 0100 |
| 5 | 1000 |
| 9 | 1100 |
The "+3" offset isn't arbitrary — it makes Excess-3 self-complementing: the 9's complement of a decimal digit (i.e., 9 − d) is obtained by simply inverting every bit of its Excess-3 code, with no arithmetic needed. That property made Excess-3 genuinely useful for simplifying decimal subtraction in early decimal computing hardware; it's included here mainly as a concrete example of how a code's structure can be chosen to make a specific downstream operation trivial — the same design instinct that motivates two's complement and Gray code above, just aimed at a different problem.
ASCII
ASCII (American Standard Code for Information Interchange) assigns each printable character and control code a 7-bit (originally) binary value — A is 100 0001 (0x41), a is 110 0001 (0x61), 0–9 are 011 0000–011 1001 (0x30–0x39). It's included here not because it's specific to digital logic design, but because it's the first place most engineers encounter the general idea this whole page is about: binary patterns are just agreed-upon conventions for what a bit sequence means, and that convention can be chosen to make some downstream operation convenient — here, mapping digit characters '0'–'9' to their numeric value is just a matter of masking off the top 3 bits.
Parity bits
A parity bit is a single extra bit appended to a group of data bits, chosen so that the total number of 1-bits (including the parity bit itself) is always even (even parity) or always odd (odd parity).
Data: 1011 0110 (five 1-bits)
Even parity bit: 1 (makes the total six 1-bits — even)
Odd parity bit: 0 (keeps the total five 1-bits — odd)
A parity checker on the receiving end recomputes the same count and flags a single-bit error if the parity no longer matches what's expected. It's a genuinely minimal scheme — it can't correct anything, and it's blind to any error that flips an even number of bits — but that minimalism (one extra bit, one XOR tree to compute it) is exactly why it still shows up in real hardware: simple memory interfaces, serial links, and other places where an error is rare enough that "detect and re-request" is an acceptable strategy and the cost of a full error-correcting code isn't justified.
What's next
Representation is now covered end to end — numbers, arithmetic, and specialized codes. The next section turns to the algebra that governs how logic gates combine signals: Boolean algebra, the tool for reasoning about and simplifying any digital function before it's built.