lulupedia
Jju 版本暂未收录,当前展示 English 内容。

Binary-coded decimal

5308 words·9/24/2026·English
0

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, typically four. In the most common implementation, known as the 8421 code, the binary representations of the numbers 0 through 9 are used, while the remaining six possible combinations of the four bits are considered invalid. BCD was widely used in early digital computers and remains prevalent in systems where exact decimal arithmetic and straightforward conversion to human-readable displays are required, such as financial applications, digital clocks, and calculators.

Encoding principles

The fundamental principle of BCD is the independent encoding of each decimal digit. In standard 4-bit BCD (often referred to as 8421 BCD), the four bits represent the binary weights 8, 4, 2, and 1. For example, the decimal number 45 is encoded as two separate 4-bit groups: 0100 for 4 and 0101 for 5, resulting in 0100 0101. This contrasts with pure binary encoding, where 45 is represented as a single binary value, 101101.

While 8421 is the most ubiquitous weighting, other weighted codes exist, such as 2421 and 5211, which possess self-complementing properties useful for subtraction. Non-weighted codes like Excess-3 (XS-3) are also utilized; in XS-3, the binary value is the standard 8421 BCD value plus three, which simplifies certain arithmetic operations and also provides a self-complementing feature.

Storage formats

BCD numbers can be stored in memory using several different formats, primarily categorized by how the digits are packed into bytes or words.

Unpacked BCD

In unpacked BCD, each decimal digit occupies an entire byte (8 bits). The lower four bits contain the BCD digit, while the upper four bits are typically padded with zeros or used for other control purposes. This format is simple to process but highly inefficient in terms of memory usage.

Packed BCD

Packed BCD stores two decimal digits within a single byte, with one digit in the upper nibble (4 bits) and the other in the lower nibble. For numbers with an odd number of digits, the most significant nibble of the leading byte is usually padded with zeros. Packed BCD significantly improves storage efficiency compared to unpacked BCD and is the standard format for hardware decimal arithmetic.

Zoned decimal

Used primarily in IBM mainframe environments with EBCDIC character encoding, zoned decimal stores each digit in a single byte. The lower four bits hold the numeric value, while the upper four bits (the "zone") contain a specific pattern, typically hexadecimal 'F' (1111 in binary). The zone of the rightmost byte may also encode the sign of the number.

BCD arithmetic

Performing arithmetic on BCD numbers requires specialized algorithms or hardware logic to ensure the results remain valid BCD. Because a 4-bit group can represent 16 states but only 10 are valid in BCD, standard binary addition can produce invalid results.

When adding two BCD digits, if the sum exceeds 9 or if a binary carry is generated out of the 4-bit group, the result must be corrected. This correction is typically achieved by adding 6 (0110 in binary) to the sum. Adding 6 skips the six invalid states (10 through 15) and generates a carry to the next significant digit. Microprocessors designed to support BCD often include a specific instruction, such as Decimal Adjust Accumulator (DAA), which automatically applies this correction after a standard binary addition.

Subtraction in BCD is similarly handled, often by using the 9's complement or 10's complement method, or by subtracting 6 when a borrow occurs during binary subtraction.

Advantages and disadvantages

The use of BCD offers several distinct advantages, particularly in specific application domains. The primary benefit is the exact representation of decimal fractions. Numbers like 0.1, which have infinite, repeating representations in pure binary floating-point, can be represented exactly in BCD. This makes BCD indispensable for financial, commercial, and accounting calculations where rounding errors are unacceptable. Additionally, scaling by powers of 10 is trivial, requiring only a shift of the digit boundaries, and converting BCD to character formats for display or printing is computationally inexpensive.

However, BCD also presents notable disadvantages. The most prominent is storage inefficiency; a 4-bit BCD digit utilizes only 10 of the 16 possible states, resulting in a roughly 20% waste of storage capacity compared to pure binary. Furthermore, arithmetic circuitry for BCD is inherently more complex and requires more logic gates than pure binary arithmetic, which can lead to slower processing speeds and higher power consumption in hardware implementations.

Modern applications and standards

Although pure binary arithmetic dominates general-purpose computing, BCD remains highly relevant in specialized fields. In financial systems and databases, decimal data types are frequently implemented using BCD or similar dense decimal encodings to ensure absolute precision in monetary calculations. Real-time clocks (RTCs) in computers and embedded systems almost universally use BCD to store time and date values, simplifying the extraction of individual digits for display.

The IEEE 754-2008 standard for floating-point arithmetic formally introduced decimal floating-point formats, which support both BCD (specifically Densely Packed Decimal) and binary integer significands. This standardization underscores the ongoing importance of exact decimal arithmetic in modern computing, particularly in enterprise and financial software.

Variations and dense encodings

To mitigate the storage inefficiency of traditional packed BCD, researchers have developed denser encoding schemes. Chen-Ho encoding and Densely Packed Decimal (DPD) are prominent examples. DPD, which is utilized in the IEEE 754-2008 decimal floating-point standard, compresses three decimal digits into 10 bits, achieving an efficiency of approximately 99.9% compared to the theoretical maximum. These dense encodings allow systems to retain the exactness of decimal arithmetic while minimizing the memory and bandwidth penalties historically associated with BCD.

Comments (0)

U

No comments yet. Be the first to comment!

You May Be Interested In

Related Articles