Introduction
Binary-Coded Decimal (BCD) is a class of binary encodings where each decimal digit (0 through 9) is represented by a fixed number of bits, usually four. Unlike pure binary conversion which represents the entire number as a single binary value, BCD encodes each digit independently. The most common variant is 8421 BCD, also called Natural BCD or NBCD, where each digit is simply its 4-bit binary equivalent. This tool encodes decimal numbers to BCD binary and decodes BCD binary back to decimal, supporting five BCD variants: 8421 (standard), 2421 (Aiken code), 5421 (bi-quinary), Excess-3 (Stibitz code), and Gray BCD. Both packed (two digits per byte) and unpacked (one digit per nibble) modes are supported. Enter your number or binary string, choose a variant, and the result appears instantly. Everything runs in your browser.
What this tool does
- Encodes decimal numbers to BCD binary by converting each digit to its 4-bit representation in the selected BCD variant.
- Decodes BCD binary strings back to decimal numbers by mapping each 4-bit nibble to its corresponding digit.
- Supports five BCD encoding variants: 8421 (standard), 2421 (Aiken), 5421 (bi-quinary), Excess-3 (Stibitz), and Gray BCD.
- Offers packed BCD mode where two digits are stored per byte (8 bits), matching how BCD is stored in computer memory.
- Displays a reference table showing the 4-bit binary value for each decimal digit in the selected variant.
- Validates input and reports errors for invalid BCD nibbles (bit patterns that do not correspond to any digit in the selected variant).
How this tool works
The tool has two modes. Encode mode takes a decimal number and converts each digit to its 4-bit BCD representation. Decode mode takes a binary string and converts each 4-bit nibble back to its decimal digit.
Five BCD variants are available from a dropdown. The 8421 variant (standard BCD) maps each digit to its natural 4-bit binary value: 0 is 0000, 1 is 0001, through 9 is 1001. The 2421 variant (Aiken code) uses weights of 2, 4, 2, and 1, making it self-complementing. The 5421 variant uses weights of 5, 4, 2, and 1. The Excess-3 variant (Stibitz code) adds 3 to each digit before encoding. The Gray BCD variant ensures only one bit changes between consecutive digits.
The packed mode checkbox switches between unpacked and packed BCD. In unpacked mode, each digit occupies one nibble (4 bits), and the output is a sequence of 4-bit groups separated by spaces. In packed mode, two digits are packed into each byte (8 bits), which is how BCD is typically stored in computer memory and used in electronic systems. When packing an odd number of digits, the last nibble is padded with zeros.
The reference table at the bottom shows all 10 digits and their 4-bit binary values for the selected variant, making it easy to verify the encoding by hand.
How the cipher or encoding works
Binary-Coded Decimal (BCD) is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of bits, usually four. The most common form is 8421 BCD, also called Natural BCD (NBCD) or Simple BCD, where each digit is represented by its corresponding 4-bit binary value. The name "8421" refers to the weights of the four bit positions: 8, 4, 2, and 1.
In 8421 BCD, the digit 0 is encoded as 0000, 1 as 0001, 2 as 0010, and so on through 9 as 1001. The 4-bit patterns 1010 through 1111 (which would represent 10 through 15 in pure binary) are not valid in 8421 BCD. This means that BCD uses only 10 of the 16 possible 4-bit patterns, making it slightly less space-efficient than pure binary. A number like 42 in BCD is 0100 0010 (4 = 0100, 2 = 0010), whereas in pure binary it would be 101010.
BCD is used in systems where decimal accuracy is critical. Pure binary conversion can introduce rounding errors when dealing with decimal fractions, which is unacceptable in financial calculations. BCD preserves exact decimal representation because each digit is encoded independently. For this reason, BCD was widely used in early computers and remains in use in financial systems, digital calculators, and electronic displays.
Several BCD variants exist beyond the standard 8421 encoding. The 2421 variant, also called the Aiken code after Howard Aiken who designed the Harvard Mark I computer, uses weights of 2, 4, 2, and 1. It is self-complementing, meaning the 9 complement of any digit (9 minus the digit) is the bitwise 1 complement of its encoding. This property simplifies subtraction in BCD arithmetic.
The 5421 variant uses weights of 5, 4, 2, and 1. It is sometimes used in decimal counter circuits because it allows easy cascading of counter stages. The digit 5 is encoded as 1000 (5 = 5 + 0 + 0 + 0), and digits 6 through 9 use the high bit plus the remaining 4-2-1 weights.
The Excess-3 variant, also called the Stibitz code after George Stibitz who built one of the first binary computers at Bell Labs, adds 3 to each digit before encoding it as 8421. So 0 becomes 0011 (3 in binary), 1 becomes 0100 (4), and 9 becomes 1100 (12). Excess-3 is self-complementing and simplifies BCD addition because the carry from a digit sum naturally indicates when the result exceeds 9.
Gray BCD applies the Gray code transformation to the standard 8421 encoding. In Gray code, only one bit changes between consecutive values, which reduces errors in mechanical encoders, rotational sensors, and analog-to-digital converters. Gray BCD is used in applications where bit transitions could cause transient errors.
BCD can be stored in two ways. In unpacked BCD, each digit occupies a full byte (8 bits), with the upper 4 bits unused or used for a sign indicator. In packed BCD, two digits are packed into each byte, with the upper nibble holding one digit and the lower nibble holding another. Packed BCD is more space-efficient, storing twice as many digits per byte. The IBM mainframe architecture, the x86 architecture (via the DAA and DAS instructions), and many microcontrollers support packed BCD arithmetic.
BCD is closely related to the binary to text converter, but serves a different purpose. The binary to text converter encodes the entire text as a sequence of 8-bit binary bytes, while BCD encodes only decimal digits as 4-bit values. For converting between number bases, see the base conversion tool.
How to use this tool
- Enter your decimal number (for encoding) or binary BCD string (for decoding) in the input field.
- Choose a BCD variant from the dropdown: 8421 (standard), 2421 (Aiken), 5421, Excess-3, or Gray.
- Toggle the Packed checkbox if you want two digits per byte (8 bits) instead of one digit per nibble (4 bits).
- Select Encode or Decode mode.
- Review the result. The reference table shows the encoding for each digit in the selected variant.
- Click Copy to copy the result to your clipboard.
Real-world examples
Encoding a phone number
An electronics student needs to display the number 5551234 on a seven-segment display using BCD. They enter "5551234" in the Encode tab with the 8421 variant and unpacked mode. The tool outputs "0101 0101 0101 0001 0010 0011 0100". Each digit maps to its 4-bit binary value: 5 is 0101, 1 is 0001, 2 is 0010, 3 is 0011, 4 is 0100. The student feeds these nibbles to a BCD-to-seven-segment decoder IC to drive the display.
Decoding packed BCD from a legacy system
A programmer working with a legacy IBM mainframe system receives the packed BCD value "0110000100100011" and needs to convert it to decimal. They select the 8421 variant, enable Packed mode, and paste the binary string into the Decode tab. The tool splits the 16 bits into two bytes: 01100001 and 00100011. The first byte gives digits 6 and 1, the second gives digits 2 and 3. The decoded decimal number is "6123".
Using Excess-3 for arithmetic
A digital logic designer is building a BCD adder circuit and needs to work with Excess-3 encoding. They enter "25" in the Encode tab and select the Excess-3 variant. The tool outputs "0101 1000" because 2 + 3 = 5 (0101 in binary) and 5 + 3 = 8 (1000 in binary). The designer uses this encoding in their circuit because Excess-3 makes carry detection simpler: if the sum of two Excess-3 digits exceeds 9 in decimal (which is 11001 in binary, 25 in decimal), a carry is generated.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| BCD (8421) | O(n) where n is the number of digits, 4 bits per digit | Electronic displays, calculators, financial systems, legacy computers |
| Pure binary | O(log n) bits for number n, most compact representation | General-purpose computing, arithmetic operations |
| ASCII digits | 8 bits per digit, includes zone bits | Text representation, data exchange, zoned decimal in COBOL |
| Gray code | O(n) bits, one bit change per increment | Mechanical encoders, rotational sensors, error reduction |
Limitations or considerations
BCD is less space-efficient than pure binary. A 4-bit nibble can represent 16 values (0 to 15), but BCD uses only 10 of them (0 to 9), wasting 37.5 percent of the encoding space. A number that fits in 16 bits of pure binary (up to 65535) requires 20 bits in unpacked BCD (5 digits times 4 bits) or 24 bits in packed BCD (3 bytes). BCD arithmetic is also more complex than binary arithmetic because each digit addition may produce a result greater than 9, requiring a correction step (adding 6) to produce a valid BCD digit and a carry.
The 8421 variant has invalid bit patterns (1010 through 1111) that do not correspond to any decimal digit. If these patterns appear in a BCD string, they indicate corrupted data or an encoding error. The tool detects and reports these invalid nibbles. Other variants have different sets of valid and invalid patterns.
The tool only encodes and decodes decimal digits (0 through 9). It does not handle BCD sign indicators, decimal points, or zone bits used in formats like zoned decimal (EBCDIC) or signed packed BCD. For full text encoding, use the binary to text converter.
Frequently asked questions
What is Binary-Coded Decimal (BCD)?
Binary-Coded Decimal (BCD) is a binary encoding where each decimal digit (0 through 9) is represented by a fixed number of bits, usually four. The most common variant is 8421 BCD, where each digit is its 4-bit binary value: 0 is 0000, 1 is 0001, through 9 is 1001. BCD is used in electronic displays, calculators, financial systems, and legacy computers where decimal accuracy is critical.
What is the difference between BCD and pure binary?
In pure binary, the entire number is converted to a single binary value. For example, 42 in pure binary is 101010. In BCD, each digit is encoded independently as a 4-bit value. So 42 in BCD is 0100 0010 (4 = 0100, 2 = 0010). BCD uses more bits than pure binary but preserves exact decimal representation, which is important for financial calculations where binary rounding errors are unacceptable.
What is packed BCD?
Packed BCD stores two decimal digits in each byte (8 bits), with the upper nibble (4 bits) holding one digit and the lower nibble holding another. This is twice as space-efficient as unpacked BCD, which uses a full byte for each digit. Packed BCD is the standard format for BCD storage in computer memory and is supported by IBM mainframes, x86 processors, and many microcontrollers.
What are the different BCD variants?
The most common BCD variants are 8421 (standard, where each digit is its 4-bit binary value), 2421 (Aiken code, self-complementing with weights 2-4-2-1), 5421 (bi-quinary with weights 5-4-2-1), Excess-3 (Stibitz code, adds 3 to each digit before encoding), and Gray BCD (only one bit changes between consecutive digits). Each variant has properties that make it useful in specific applications like arithmetic, counter circuits, or mechanical encoders.
Why is BCD used in financial systems?
Pure binary conversion can introduce rounding errors when dealing with decimal fractions. For example, the decimal value 0.1 cannot be represented exactly in binary floating point, leading to small errors that accumulate over millions of calculations. BCD preserves exact decimal representation because each digit is encoded independently, making it ideal for financial calculations where precision is critical. Many COBOL financial systems and database engines still use BCD or packed decimal formats.
What is Excess-3 BCD?
Excess-3 BCD, also called the Stibitz code after George Stibitz, adds 3 to each decimal digit before encoding it as 8421 binary. So 0 becomes 0011 (3 in binary), and 9 becomes 1100 (12 in binary). Excess-3 is self-complementing, meaning the 9 complement of a digit is the bitwise 1 complement of its encoding. This property simplifies BCD subtraction and carry detection in digital logic circuits.
Conclusion
Binary-Coded Decimal bridges the gap between binary computing and decimal representation. For converting full text to binary, see the binary to text converter. For converting between number bases (binary, octal, decimal, hexadecimal), see the base conversion tool. For ASCII and Unicode character codes, see the ASCII to hex converter.