In computing and electronic systems, binary-coded decimal ( BCD ) (sometimes called natural binary-coded decimal , NBCD ) is an encoding for decimal numbers in which each digit is represented by its own binary sequence. Its main virtue is that it allows easy conversion to decimal digits for printing or display, and allows faster decimal calculations. Its drawbacks are a small increase in the complexity of circuits needed to implement mathematical operations. Uncompressed BCD is also a relatively inefficient encoding—it occupies more space than a purely binary representation.

In BCD, a digit is usually represented by four bits which, in general, represent the values/digits/characters 0–9. Other bit combinations are sometimes used for a sign or other indications.

Although uncompressed BCD is not as widely used as it once was, decimal fixed-point and floating-point are still important and continue to be used in financial, commercial, and industrial computing.

Recent decimal floating-point representations use base-10 exponents, but not BCD encodings. Current hardware implementations, however, convert the compressed decimal encodings to BCD internally before carrying out computations. Software implementations of decimal arithmetic typically use BCD or some other 10 n base, depending on the operation.

Basics

To BCD-encode decimal number using the common encoding, each decimal digit is stored in a four-bit nibble.

                        Decimal: 0 1 2 3 4 5 6 7 8 9BCD: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
                      

Thus, the BCD encoding for the number 127 would be:

                        0001 0010 0111
                      

Since most computers store data in eight-bit bytes, there are two common ways of storing four-bit BCD digits in those bytes:

  • each digit is stored in one nibble of a byte, with the other nibble being set to all zeros, all ones (as in the EBCDIC code), or to 0011 (as in the ASCII code)
  • two digits are stored in each byte.

Unlike binary-encoded numbers, BCD-encoded numbers can easily be displayed by mapping each of the nibbles to a different character. Converting a binary-encoded number to decimal for display is much harder, as this generally involves integer multiplication or divide operations. BCD also avoids problems where fractions that can be represented exactly in decimal cannot be in binary (eg one-tenth).

BCD in Electronics

BCD is very common in electronic systems where a numeric value is to be displayed, especially in systems consisting solely of digital logic, and not containing a microprocessor. By utilizing BCD, the manipulation of numerical data for display can be greatly simplified by treating each digit as a separate single sub-circuit. This matches much more closely the physical reality of display hardware—a designer might choose to use a series of separate identical 7-segment displays to build a metering circuit, for example. If the numeric quantity were stored and manipulated as pure binary, interfacing to such a display would require complex circuitry. Therefore, in cases where the calculations are relatively simple working throughout with BCD can lead to a simpler overall system than converting to binary.

The same argument applies when hardware of this type uses an embedded microcontroller or other small processor. Often, smaller code results when representing numbers internally in BCD format, since a conversion from or to binary representation can be expensive on such limited processors. For these applications, some small processors feature BCD arithmetic modes, which assist when writing routines that manipulate BCD quantities.

Packed BCD

A widely used variation of the two-digits-per-byte encoding is called packed BCD (or simply packed decimal ). All of the upper bytes of a multi-byte word plus the upper four bits (nibble) of the lowest byte are used to store decimal integers. The lower four bits of the lowest byte are used as the sign flag. As an example, a 32 bit word contains 4 bytes or 8 nibbles. Packed BCD uses the upper 7 nibbles to store the integers of a 7-digit decimal value and uses the lowest nibble to indicate the sign of those integers.

Standard sign values are 1100 (hex C) for positive (+) and 1101 (D) for negative (−). This convention was derived from abbreviations for accounting terms (Credit and Debit), as packed decimal coding was widely used in accounting systems. Other allowed signs are 1010 (A) and 1110 (E) for positive and 1011 (B) for negative. Some implementations also provide unsigned BCD values with a sign nibble of 1111 (F). In packed BCD, the number 127 is represented by 0001 0010 0111 1100 (127C) and −127 is represented by 0001 0010 0111 1101 (127D).

No matter how many bytes wide a word is, there are always an even number of nibbles because each byte has two of them. Therefore, a word of n bytes can contain up to (2 n )−1 decimal digits, which is always an odd number of digits. A decimal number with d digits requires ½( d +1) bytes of storage space.

For example, a four-byte (32bit) word can hold seven decimal digits plus a sign, and can represent values ranging from ±9,999,999. Thus the number −1,234,567 is 7 digits wide and is encoded as:

                        0001 0010 0011 0100 0101 0110 0111 1101
                        
                          1 2 3 4 5 6 7 −
                        
                      

(Note that, like character strings, the first byte of the packed decimal — with the most significant two digits — is usually stored in the lowest address in memory, independent of the endianness of the machine).

In contrast, a four-byte binary two's complement integer can represent values from −2,147,483,648 to +2,147,483,647.

While packed BCD does not make optimal use of storage (about 1 / 6 of the memory used is wasted), conversion to ASCII, EBCDIC, or the various encodings of Unicode is still trivial, as no arithmetic operations are required. The extra storage requirements are usually offset by the need for the accuracy that fixed-point decimal arithmetic provides. Denser packings of BCD exist which avoid the storage penalty and also need no arithmetic operations for common conversions.

Fixed-point packed decimal

Fixed-point decimal numbers are supported by some programming languages (such as COBOL and PL/I), and provide an implicit decimal point in front of one of the digits. For example, a packed decimal value encoded with the bytes 12 34 56 7C represents the fixed-point value +1,234.567 when the implied decimal point is located between the 4th and 5th digits.

                        12 34 56 7C
                        
                          12 34.56 7+
                        
                      

Higher-density encodings

If a decimal digit requires four bits, then three decimal digits require 12 bits. However, since 2 10 (1,024) is greater than 10 3 (1,000), if three decimal digits are encoded together, only 10 bits are needed. Two such encodings are Chen-Ho encoding and Densely Packed Decimal . The latter has the advantage that subsets of the encoding encode two digits in the optimal 7 bits and one digit in 4 bits, as in regular BCD.

Zoned decimal

Some implementations (notably IBM mainframe systems) support zoned decimal numeric representations. Each decimal digit is stored in one byte, with the lower four bits encoding the digit in BCD form. The upper four bits, called the "zone" bits, are usually set to a fixed value so that the byte holds a character value corresponding to the digit. EBCDIC systems use a zone value of 1111 (hex F); this yields bytes in the range F0 to F9 (hex), which are the EBCDIC codes for the characters "0" through "9". Similarly, ASCII systems use a zone value of 0011 (hex 3), giving character codes 30 to 39 (hex).

For signed zoned decimal values, the rightmost (least significant) zone nibble holds the sign digit, which is the same set of values that are used for signed packed decimal numbers (see above). Thus a zoned decimal value encoded as the hex bytes F1 F2 D3 represents the signed decimal value −123:

                        F1 F2 D3
                        
                          1 2 −3
                        
                      

EBCDIC zoned decimal conversion table

(*) Note: These characters vary depending on the local character code page.

Fixed-point zoned decimal

Some languages (such as COBOL and PL/I) directly support fixed-point zoned decimal values, assigning an implicit decimal point at some location between the decimal digits of a number. For example, given a six-byte signed zoned decimal value with an implied decimal point to the right of the fourth digit, the hex bytes F1 F2 F7 F9 F5 C0 represent the value +1,279.50:

                        F1 F2 F7 F9 F5 C0
                        
                          1 2 7 9. 5 +0
                        
                      

IBM and BCD

IBM used the terms binary-coded decimal and BCD for 6-bit alphameric codes that represented numbers, upper-case letters and special characters. Some variation of BCD alphamerics was used in most early IBM computers, including the IBM 1620, IBM 1400 series, and non-Decimal Architecture members of the IBM 700/7000 series.

Bit positions in BCD alphamerics were usually labelled B, A, 8, 4, 2 and 1. For encoding digits, B and A were zero. The letter A was encoded (B,A,1).

In the IBM 1620, BCD alphamerics were encoded using digit pairs, with the "zone" in the even digit and the "digit" in the odd digit. Input/Output translation hardware converted between the intern

Binary numeral system - Wikipedia, the free encyclopedia

Since the binary numeral 100 is equal to the decimal value four, it would be confusing to refer to the numeral as one hundred. [edit] Counting in binary

...

Binary-coded decimal - Wikipedia, the free encyclopedia

In computing and electronic systems, binary-coded decimal (BCD) (sometimes called natural binary-coded decimal, NBCD) is an encoding for decimal numbers in which each digit is ...

...

Binary Decimal Converter Calculator

Binary / Decimal Converter Calculator

...

Online Binary-Decimal Converter

Online binary converter. Supports all types of variables, including single and double precision IEEE754 numbers

...

Hex Binary Decimal Octal Converter with Scientific Calculator

Hex Binary Decimal Octal Converter with Scientific Calculator Powerful and flexible multipurpose Scientific calculator and Hex/Binary/Decimal/Octal Cconversions.

...

Decimal and Binary Conversion Tool

This is a tool to practice converting between decimal and binary representations. After you have practiced for a while and feel that you know how to do the conversions, take the ...

...

Binary Decimal Conversion Tools | Free Binary & Decimal Converters

Convert binary to decimal numbers or convert decimal to binary numbers. Use our free online conversion tools and practice exercises. Answers are available too.

...

Binary and Decimal Number Converter

Use this interactive to calculator to work with binary, decimal and hexadecimal numbers. Calculate, translate and convert between binary, decimal and hexadecimal values as needed ...

...

binary coded decimal definition - Dictionary - MSN Encarta

bi·na·ry cod·ed dec·i·mal (plural bi·na·ry cod·ed dec·i·mals) noun : Definition: system using binary numbers for decimals: a numbering system in which each digit of a ...

...

Convert from Binary to Decimal - wikiHow

wikiHow article about How to Convert from Binary to Decimal.

...