Binary-coded decimal

# Binary-coded decimal

Discussion

Encyclopedia
In computing
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...

and electronic
Electronics
Electronics is the branch of science, engineering and technology that deals with electrical circuits involving active electrical components such as vacuum tubes, transistors, diodes and integrated circuits, and associated passive interconnection technologies...

systems, binary-coded decimal (BCD) is a digital encoding method for numbers using decimal
Decimal
The decimal numeral system has ten as its base. It is the numerical base most widely used by modern civilizations....

notation, with each decimal digit represented by its own binary
Binary numeral system
The binary numeral system, or base-2 number system, represents numeric values using two symbols, 0 and 1. More specifically, the usual base-2 system is a positional notation with a radix of 2...

sequence. In BCD, a numeral
Numerical digit
A digit is a symbol used in combinations to represent numbers in positional numeral systems. The name "digit" comes from the fact that the 10 digits of the hands correspond to the 10 symbols of the common base 10 number system, i.e...

is usually represented by four bit
Bit
A bit is the basic unit of information in computing and telecommunications; it is the amount of information stored by a digital device or other physical system that exists in one of two possible distinct states...

s which, in general, represent the decimal range 0 through 9. Other bit patterns are sometimes used for a sign
Sign (mathematics)
In mathematics, the word sign refers to the property of being positive or negative. Every nonzero real number is either positive or negative, and therefore has a sign. Zero itself is signless, although in some contexts it makes sense to consider a signed zero...

or for other indications (e.g., error or overflow). Uncompressed (or zoned) BCD consumes a byte
Byte
The byte is a unit of digital information in computing and telecommunications that most commonly consists of eight bits. Historically, a byte was the number of bits used to encode a single character of text in a computer and for this reason it is the basic addressable element in many computer...

for each represented numeral, whereas compressed (or packed) BCD typically carries two numerals in a single byte by taking advantage of the fact that four bits will represent the full numeral range.

BCD's main virtue is ease of conversion between machine- and human-readable formats, as well as a more precise machine-format representation of decimal quantities. As compared to typical binary formats, BCD's principal drawbacks are a small increase in the complexity of the circuits needed to implement basic mathematical operations and less efficient usage of storage facilities.

BCD was used in many early decimal computer
Decimal computer
Decimal computers, computers which have a decimal architecture, represent numbers and/or addresses in decimal, and provide instructions to operate on those numbers and/or addresses directly; examples of encoding used are BCD, Excess-3, two-out-of-five code, ASCII, and EBCDIC.Many early computers,...

s. Although BCD is not as widely used as in the past, decimal fixed-point
Fixed-point arithmetic
In computing, a fixed-point number representation is a real data type for a number that has a fixed number of digits after the radix point...

and floating-point formats are still important and continue to be used in financial, commercial, and industrial computing, where subtle conversion and rounding errors that are inherent to floating point binary representations cannot be tolerated.

## Basics

As described in the introduction, BCD takes advantage of the fact that any one decimal numeral can be represented by a four bit pattern:

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

As most computers store data in 8-bit byte
Byte
The byte is a unit of digital information in computing and telecommunications that most commonly consists of eight bits. Historically, a byte was the number of bits used to encode a single character of text in a computer and for this reason it is the basic addressable element in many computer...

s, it is possible to use one of the following methods to encode a BCD number:
• Uncompressed: each numeral is encoded into one byte, with four bits representing the numeral and the remaining bits having no significance.
• Packed: two numerals are encoded into a single byte, with one numeral in the least significant nibble
Nibble
In computing, a nibble is a four-bit aggregation, or half an octet...

(bits 0-3) and the other numeral in the most significant nibble (bits 4-7).

As an example, encoding the decimal number 91 using uncompressed BCD results in the following binary pattern of two bytes:

Decimal: 9 1
Binary : 0000 1001 0000 0001

In packed BCD, the same number would fit into a single byte:

Decimal: 9 1
Binary : 1001 0001

Hence the numerical range for one uncompressed BCD byte is zero through nine inclusive, whereas the range for one packed BCD is zero through ninety-nine inclusive.

To represent numbers larger than the range of a single byte any number of contiguous bytes may be used.  For example, to represent the decimal number 12345 in packed BCD, using big-endian format, a program would encode as follows:

Decimal: 1 2 3 4 5
Binary : 0000 0001 0010 0011 0100 0101

Note that the most significant nibble of the most significant byte is zero, implying that the number is in actuality 012345.  Also note how packed BCD is more efficient in storage usage as compared to uncompressed BCD; encoding the same number in uncompressed format would consume 67 percent more storage.

Shifting
Logical shift
In computer science, a logical shift is a bitwise operation that shifts all the bits of its operand. Unlike an arithmetic shift, a logical shift does not preserve a number's sign bit or distinguish a number's exponent from its mantissa; every bit in the operand is simply moved a given number of bit...

In computer science, a mask is data that is used for bitwise operations.Using a mask, multiple bits in a byte, nibble, word can be set either on, off or inverted from on to off in a single bitwise operation.-Masking bits to 1:...

operations are used to pack or unpack a packed BCD digit.  Other logical operation
Bitwise operation
A bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. This is used directly at the digital hardware level as well as in microcode, machine code and certain kinds of high level languages...

s are used to convert a numeral to its equivalent bit pattern or reverse the process.

## 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 seven-segment display
Seven-segment display
A seven-segment display , or seven-segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot-matrix displays...

s 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 common variation of the two-digits-per-byte encoding is called packed BCD (or simply packed decimal), which has been in use since the 1960s or earlier and implemented in all IBM mainframe hardware since then. In most representations, one or more bytes hold a decimal integer, where each of the two nibble
Nibble
In computing, a nibble is a four-bit aggregation, or half an octet...

s of each byte represent a decimal digit, with the more significant digit in the upper half of each byte, and with leftmost byte (residing at the lowest memory address) containing the most significant digits of the packed decimal value. The lower nibble of the rightmost byte is usually used as the sign flag (although in some representations this nibble may be used as the least significant digit if the packed decimal value does not have a sign at all, i.e., is purely unsigned). As an example, a 4-byte value consists of 8 nibbles, wherein the upper 7 nibbles store the digits of a 7-digit decimal value and the lowest nibble indicates the sign of the decimal integer value.

Standard sign values are 1100 (hex
In mathematics and computer science, hexadecimal is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F to represent values ten to fifteen...

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). ILE RPG uses 1111 (F) for positive and 1101 (D) for negative. In packed BCD, the number 127 is represented by 0001 0010 0111 1100 (127C) and −127 is represented by 0001 0010 0111 1101 (127D). Burroughs systems used 1101 (D) for negative, and any other value was considered a positive sign value (the processors would normalize a positive sign to 1100 (C)).
Sign
Digit
BCD
8 4 2 1
Sign Notes
A 1 0 1 0 +
B 1 0 1 1
C 1 1 0 0 + Preferred
D 1 1 0 1 Preferred
E 1 1 1 0 +
F 1 1 1 1 + Unsigned

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 (2n)−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 4-byte (32-bit) 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
Endianness
In computing, the term endian or endianness refers to the ordering of individually addressable sub-components within the representation of a larger data item as stored in external memory . Each sub-component in the representation has a unique degree of significance, like the place value of digits...

of the machine.)

In contrast, a 4-byte binary two's complement
Two's complement
The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two...

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
ASCII
The American Standard Code for Information Interchange is a character-encoding scheme based on the ordering of the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text...

, EBCDIC
EBCDIC
Extended Binary Coded Decimal Interchange Code is an 8-bit character encoding used mainly on IBM mainframe and IBM midrange computer operating systems....

, or the various encodings of Unicode
Unicode
Unicode is a computing industry standard for the consistent encoding, representation and handling of text expressed in most of the world's writing systems...

is still trivial, as no arithmetic operations are required. The extra storage requirements are usually offset by the need for the accuracy and compatibility with calculator or hand calculation that fixed-point decimal arithmetic provides. Denser packings of BCD
BCD
BCD may refer to:* Binary coded decimal, representation of individual decimal digits in binary.* Bad Conduct Discharge, a form of discharge from US military service* Barrels per calendar day, a unit for measuring output of oil refineries...

exist which avoid the storage penalty and also need no arithmetic operations for common conversions.

Packed BCD is supported in the COBOL
COBOL
COBOL is one of the oldest programming languages. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....

programming language as the "COMPUTATIONAL-3" (an IBM extension adopted by many other compiler vendors) or "PACKED-DECIMAL" (part of the 1985 COBOL standard) data type. Besides the IBM System/360 and later compatible mainframes, packed BCD was implemented in the native instruction set of the original VAX
VAX
VAX was an instruction set architecture developed by Digital Equipment Corporation in the mid-1970s. A 32-bit complex instruction set computer ISA, it was designed to extend or replace DEC's various Programmed Data Processor ISAs...

processors from Digital Equipment Corporation
Digital Equipment Corporation
Digital Equipment Corporation was a major American company in the computer industry and a leading vendor of computer systems, software and peripherals from the 1960s to the 1990s...

and was the native format for the Burroughs Corporation Medium Systems line of mainframes (descended from the 1950's Electrodata 200 series).

### Fixed-point packed decimal

Fixed-point
Fixed-point arithmetic
In computing, a fixed-point number representation is a real data type for a number that has a fixed number of digits after the radix point...

decimal numbers are supported by some programming languages (such as COBOL
COBOL
COBOL is one of the oldest programming languages. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....

and PL/I
PL/I
PL/I is a procedural, imperative computer programming language designed for scientific, engineering, business and systems programming applications...

). These languages allow the programmer to specify 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+

The decimal point is not actually stored in memory, as the packed BCD storage format does not provide for it. Its location is simply known to the compiler and the generated code acts accordingly for the various arithmetic operations.

### Higher-density encodings

If a decimal digit requires four bits, then three decimal digits require 12 bits. However, since 210 (1,024) is greater than 103 (1,000), if three decimal digits are encoded together, only 10 bits are needed. Two such encodings are Chen-Ho encoding
Chen-Ho encoding
Chen-Ho encoding is an alternate system of binary encoding for decimal digits.The traditional system of binary encoding for decimal digits, known as Binary-coded decimal , uses four bits to encode each digit, resulting in significant wastage of binary data bandwidth .The concepts behind Chen-Ho...

and Densely Packed Decimal
Densely Packed Decimal
Densely packed decimal is a system of binary encoding for decimal digits.The traditional system of binary encoding for decimal digits, known as Binary-coded decimal , uses four bits to encode each digit, resulting in significant wastage of binary data bandwidth...

. The latter has the advantage that subsets of the encoding encode two digits in the optimal seven bits and one digit in four bits, as in regular BCD.

## Zoned decimal

Some implementations, for example IBM
IBM
International Business Machines Corporation or IBM is an American multinational technology and consulting corporation headquartered in Armonk, New York, United States. IBM manufactures and sells computer hardware and software, and it offers infrastructure, hosting and consulting services in areas...

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
EBCDIC
Extended Binary Coded Decimal Interchange Code is an 8-bit character encoding used mainly on IBM mainframe and IBM midrange computer operating systems....

codes for the characters "0" through "9". Similarly, ASCII
ASCII
The American Standard Code for Information Interchange is a character-encoding scheme based on the ordering of the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text...

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

`0+` `C0` `A0` `E0` `F0` `{` (*)   `\` (*) `0`
`1+` `C1` `A1` `E1` `F1` `A` `~` (*)   `1`
`2+` `C2` `A2` `E2` `F2` `B` `s` `S` `2`
`3+` `C3` `A3` `E3` `F3` `C` `t` `T` `3`
`4+` `C4` `A4` `E4` `F4` `D` `u` `U` `4`
`5+` `C5` `A5` `E5` `F5` `E` `v` `V` `5`
`6+` `C6` `A6` `E6` `F6` `F` `w` `W` `6`
`7+` `C7` `A7` `E7` `F7` `G` `x` `X` `7`
`8+` `C8` `A8` `E8` `F8` `H` `y` `Y` `8`
`9+` `C9` `A9` `E9` `F9` `I` `z` `Z` `9`
`0−` `D0` `B0`     `}`  (*) `^`  (*)
`1−` `D1` `B1`     `J`
`2−` `D2` `B2`     `K`
`3−` `D3` `B3`     `L`
`4−` `D4` `B4`     `M`
`5−` `D5` `B5`     `N`
`6−` `D6` `B6`     `O`
`7−` `D7` `B7`     `P`
`8−` `D8` `B8`     `Q`
`9−` `D9` `B9`     `R`

(*) Note: These characters vary depending on the local character code page
Code page
Code page is another term for character encoding. It consists of a table of values that describes the character set for a particular language. The term code page originated from IBM's EBCDIC-based mainframe systems, but many vendors use this term including Microsoft, SAP, and Oracle Corporation...

setting.

### Fixed-point zoned decimal

Some languages (such as COBOL
COBOL
COBOL is one of the oldest programming languages. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....

and PL/I
PL/I
PL/I is a procedural, imperative computer programming language designed for scientific, engineering, business and systems programming applications...

) 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
IBM
International Business Machines Corporation or IBM is an American multinational technology and consulting corporation headquartered in Armonk, New York, United States. IBM manufactures and sells computer hardware and software, and it offers infrastructure, hosting and consulting services in areas...

used the terms binary-coded decimal and BCD for 6-bit alphameric
Alphanumeric
Alphanumeric is a combination of alphabetic and numeric characters, and is used to describe the collection of Latin letters and Arabic digits or a text constructed from this collection. There are either 36 or 62 alphanumeric characters. The alphanumeric character set consists of the numbers 0 to...

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 1620
The IBM 1620 was announced by IBM on October 21, 1959, and marketed as an inexpensive "scientific computer". After a total production of about two thousand machines, it was withdrawn on November 19, 1970...

, IBM 1400 series
IBM 1400 series
The IBM 1400 series were second generation mid-range business decimal computers that IBM sold in the early 1960s. They could be operated as an independent system, in conjunction with IBM punched card equipment, or as auxiliary equipment to other computer systems.1400-series machines stored...

, and non-Decimal Architecture members of the IBM 700/7000 series
IBM 700/7000 series
The IBM 700/7000 series was a series of large-scale computer systems made by IBM through the 1950s and early 1960s. The series included several different, incompatible processor architectures. The 700s used vacuum tube logic and were made obsolete by the introduction of the transistorized 7000s...

.

The IBM 1400 series
IBM 1400 series
The IBM 1400 series were second generation mid-range business decimal computers that IBM sold in the early 1960s. They could be operated as an independent system, in conjunction with IBM punched card equipment, or as auxiliary equipment to other computer systems.1400-series machines stored...

were character-addressable machines, each location being six bits labeled B, A, 8, 4, 2 and 1,
plus an odd parity check bit (C) and a word mark bit (M).
For encoding digits 1 through 9, B and A were zero and the digit value represented by standard 4-bit BCD in bits 8 through 1.
For most other characters bits B and A were derived simply from the "12", "11", and "0" "zone punches" in the punched card character code, and bits 8 through 1 from the 1 through 9 punches.
A "12 zone" punch set both B and A, an "11 zone" set B, and a "0 zone" (a 0 punch combined with any others) set A.
Thus the letter A, (12,1) in the punched card format, was encoded (B,A,1) and the currency symbol \$, (11,8,3) in the punched card, as (B,8,3).
This allowed the circuitry to convert between the punched card format and the internal storage format to be very simple with only a few special cases.
One important special case was digit 0, represented by a lone 0 punch in the card, and (8,2) in core memory.
The memory of the IBM 1620
IBM 1620
The IBM 1620 was announced by IBM on October 21, 1959, and marketed as an inexpensive "scientific computer". After a total production of about two thousand machines, it was withdrawn on November 19, 1970...

was organized into 6-bit addressable digits, the usual 8, 4, 2, 1 plus F, used as a flag bit and C, an odd parity check bit. BCD alphamerics were encoded using digit pairs, with the "zone" in the even-addressed digit and the "digit" in the odd-addressed digit, the "zone" being related to the 12, 11, and 0 "zone punches" as in the 1400 series. Input/Output translation hardware converted between the internal digit pairs and the external standard 6-bit BCD codes.

In the Decimal Architecture IBM 7070
IBM 7070
IBM 7070 was a decimal architecture intermediate data processing system that was introduced by IBM in June 1960. It was part of the IBM 700/7000 series, and was based on discrete transistors rather than the vacuum tubes of the 1950s. It was the company's first transistorized stored-program...

, IBM 7072, and IBM 7074 alphamerics were encoded using digit pairs (using two-out-of-five code
Two-out-of-five code
In telecommunication, a two-out-of-five code is an m of n code that provides exactly ten possible combinations, and thus is popular for representing decimal digits using five bits...

in the digits, not BCD) of the 10-digit word, with the "zone" in the left digit and the "digit" in the right digit. Input/Output translation hardware converted between the internal digit pairs and the external standard 6-bit BCD codes.

With the introduction of System/360
System/360
The IBM System/360 was a mainframe computer system family first announced by IBM on April 7, 1964, and sold between 1964 and 1978. It was the first family of computers designed to cover the complete range of applications, from small to large, both commercial and scientific...

, IBM expanded 6-bit BCD alphamerics to 8-bit EBCDIC
EBCDIC
Extended Binary Coded Decimal Interchange Code is an 8-bit character encoding used mainly on IBM mainframe and IBM midrange computer operating systems....

, allowing the addition of many more characters (e.g., lowercase letters). A variable length Packed BCD numeric data type was also implemented, providing machine instructions that performed arithmetic directly on packed decimal data.

On the IBM 1130
IBM 1130
The IBM 1130 Computing System was introduced in 1965. It was IBM's least-expensive computer to date, and was aimed at price-sensitive, computing-intensive technical markets like education and engineering. It succeeded the IBM 1620 in that market segment. The IBM 1800 was a process control variant...

and 1800
IBM 1800
The IBM 1800 Data Acquisition and Control System was a process control variant of the IBM 1130 with two extra instructions , extra I/O capabilities, 'selector channel like' cycle-stealing capability and three hardware index registers....

, packed BCD was supported in software by IBM's Commercial Subroutine Package.

Today, BCD data is still heavily used in IBM processors and databases, such as IBM DB2
IBM DB2
The IBM DB2 Enterprise Server Edition is a relational model database server developed by IBM. It primarily runs on Unix , Linux, IBM i , z/OS and Windows servers. DB2 also powers the different IBM InfoSphere Warehouse editions...

, mainframes, and Power6
POWER6
The POWER6 is a microprocessor developed by IBM that implemented the Power ISA v.2.03. When it became available in systems in 2007, it succeeded the POWER5+ as IBM's flagship Power microprocessor...

. In these products, the BCD is usually zoned BCD (as in EBCDIC or ASCII), Packed BCD (two decimal digits per byte
Byte
The byte is a unit of digital information in computing and telecommunications that most commonly consists of eight bits. Historically, a byte was the number of bits used to encode a single character of text in a computer and for this reason it is the basic addressable element in many computer...

), or "pure" BCD encoding (one decimal digit stored as BCD in the low four bits of each byte). All of these are used within hardware registers and processing units, and in software.

## Other computers and BCD

The Digital Equipment Corporation
Digital Equipment Corporation
Digital Equipment Corporation was a major American company in the computer industry and a leading vendor of computer systems, software and peripherals from the 1960s to the 1990s...

VAX-11
VAX
VAX was an instruction set architecture developed by Digital Equipment Corporation in the mid-1970s. A 32-bit complex instruction set computer ISA, it was designed to extend or replace DEC's various Programmed Data Processor ISAs...

series included instructions
Instruction set
An instruction set, or instruction set architecture , is the part of the computer architecture related to programming, including the native data types, instructions, registers, addressing modes, memory architecture, interrupt and exception handling, and external I/O...

that could perform arithmetic directly on packed BCD data and convert between packed BCD data and other integer representations. The VAX's packed BCD format was compatible with that on IBM System/360 and IBM's later compatible processors. The MicroVAX and later VAX implementations dropped this ability from the CPU but retained code compatibility with earlier machines by implementing the missing instructions in an operating system-supplied software library. This was invoked automatically via exception handling when the no longer implemented instructions were encountered, so that programs using them could execute without modification on the newer machines.

In more recent computers such capabilities are almost always implemented in software rather than the CPU's instruction set, but BCD numeric data is still extremely common in commercial and financial applications.

It is possible to perform addition
Addition is a mathematical operation that represents combining collections of objects together into a larger collection. It is signified by the plus sign . For example, in the picture on the right, there are 3 + 2 apples—meaning three apples and two other apples—which is the same as five apples....

in BCD by first adding in binary, and then converting to BCD afterwards. Conversion of the simple sum of two digits can be done by adding 6 (that is, 16 – 10) when the result has a value greater than 9. For example:

1001 + 1000 = 10001 = 0001 0001
9 + 8 = 17 = 1 1

In BCD, there cannot exist a value greater than 9 (1001) per nibble
Nibble
In computing, a nibble is a four-bit aggregation, or half an octet...

. To correct this, 6 (0110) is added to that sum to get the correct first two digits:

0001 0001 + 0000 0110 = 0001 0111
1 1 + 0 6 = 1 7

which gives two nibbles, 0001 and 0111, which correspond to the digits "1" and "7". This yields "17" in BCD, which is the correct result. This technique can be extended to adding multiple digits, by adding in groups from right to left, propagating the second digit as a carry, always comparing the 5-bit result of each digit-pair sum to 9.

## Subtraction with BCD

Subtraction is done by adding the ten's complement of the subtrahend. To represent the sign of a number in BCD, the number 0000 is used to represent a positive number, and 1001 is used to represent a negative number. The remaining 14 combinations are invalid signs. To illustrate signed BCD subtraction, consider the following problem: 357 − 432.

In signed BCD, 357 is 0000 0011 0101 0111. The ten's complement of 432 can be obtained by taking the nine's complement of 432, and then adding one. So, 999 − 432 = 567, and 567 + 1 = 568. By preceding 568 in BCD by the negative sign code, the number −432 can be represented. So, −568 in signed BCD is 1001 0101 0110 1000.

Now that both numbers are represented in signed BCD, they can be added together:

0000 0011 0101 0111 + 1001 0101 0110 1000 = 1001 1000 1011 1111
0 3 5 7 + 9 5 6 8 = 9 8 11 15

Since BCD is a form of decimal representation, several of the digit sums above are invalid. In the event that an invalid entry (any BCD digit greater than 1001) exists, 6 is added to generate a carry bit and cause the sum to become a valid entry. The reason for adding 6 is that there are 16 possible 4-bit BCD values (since 24 = 16), but only 10 values are valid (0000 through 1001). So adding 6 to the invalid entries results in the following:

1001 1000 1011 1111 + 0000 0000 0110 0110 = 1001 1001 0010 0101
9 8 11 15 + 0 0 6 6 = 9 9 2 5

Thus the result of the subtraction is 1001 1001 0010 0101 (-925). To check the answer, note that the first bit is the sign bit, which is negative. This seems to be correct, since 357 − 432 should result in a negative number. To check the rest of the digits, represent them in decimal. 1001 0010 0101 is 925. The ten's complement of 925 is 1000 − 925 = 999 − 925 + 1 = 074 + 1 = 75, so the calculated answer is −75. To check, perform standard subtraction to verify that 357 − 432 is −75.

Note that in the event that there are a different number of nibbles being added together (such as 1053 − 122), the number with the fewest number of digits must first be padded with zeros before taking the ten's complement or subtracting. So, with 1053 − 122, 122 would have to first be represented as 0122, and the ten's complement of 0122 would have to be calculated.

## Background

The binary-coded decimal scheme described in this article is the most common encoding, but there are many others. The method here can be referred to as Simple Binary-Coded Decimal (SBCD) or BCD 8421.

In the headers to the table, the '8 4 2 1', etc., indicates the weight of each bit shown; note that in the fifth column two of the weights are negative. Both ASCII and EBCDIC character codes for the digits are examples of zoned BCD, and are also shown in the table.

The following table represents decimal
Decimal
The decimal numeral system has ten as its base. It is the numerical base most widely used by modern civilizations....

digits from 0 to 9 in various BCD systems:

Digit
BCD
8 4 2 1
Excess-3
Excess-3
Excess-3 binary-coded decimal ' or Stibitz code, also called biased representation or Excess-N, is a complementary BCD code and numeral system it is used on some older computers that uses a pre-specified number N as a biasing value. It is a way to represent values with a balanced number of positive...

or Stibitz
George Stibitz
George Robert Stibitz is internationally recognized as one of the fathers of the modern digital computer...

Code
BCD 2 4 2 1
or Aiken
Howard Aiken
Howard Hathaway Aiken was a pioneer in computing, being the original conceptual designer behind IBM's Harvard Mark I computer....

Code
BCD
8 4 −2 −1
IBM 702
IBM 702
The IBM 702 was IBM's response to the UNIVAC—the first mainframe computer using magnetic tapes. Because these machines had less computational power than the IBM 701 and ERA 1103, which were favored for scientific computing, the 702 was aimed at business computing.The system used electrostatic...

IBM 705
IBM 7080
IBM 7080
The IBM 7080 was a variable word length BCD transistor computer in the IBM 700/7000 series commercial architecture line, introduced in August 1961, that provided an upgrade path from the vacuum tube IBM 705 computer....

IBM 1401
IBM 1401
The IBM 1401 was a variable wordlength decimal computer that was announced by IBM on October 5, 1959. The first member of the highly successful IBM 1400 series, it was aimed at replacing electromechanical unit record equipment for processing data stored on punched cards...

8 4 2 1
ASCII
ASCII
The American Standard Code for Information Interchange is a character-encoding scheme based on the ordering of the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text...

0000 8421
EBCDIC
EBCDIC
Extended Binary Coded Decimal Interchange Code is an 8-bit character encoding used mainly on IBM mainframe and IBM midrange computer operating systems....

0000 8421
0 0000 0011 0000 0000 1010 0011 0000 1111 0000
1 0001 0100 0001 0111 0001 0011 0001 1111 0001
2 0010 0101 0010 0110 0010 0011 0010 1111 0010
3 0011 0110 0011 0101 0011 0011 0011 1111 0011
4 0100 0111 0100 0100 0100 0011 0100 1111 0100
5 0101 1000 1011 1011 0101 0011 0101 1111 0101
6 0110 1001 1100 1010 0110 0011 0110 1111 0110
7 0111 1010 1101 1001 0111 0011 0111 1111 0111
8 1000 1011 1110 1000 1000 0011 1000 1111 1000
9 1001 1100 1111 1111 1001 0011 1001 1111 1001

## Legal history

In the 1972 case Gottschalk v. Benson
Gottschalk v. Benson
Gottschalk v. Benson, was a United States Supreme Court case in which the Court ruled that a process claim directed to a numerical algorithm, as such, was not patentable because "the patent would wholly pre-empt the mathematical formula and in practical effect would be a patent on the algorithm...

, the U.S. Supreme Court overturned a lower court decision which had allowed a patent for converting BCD encoded numbers to binary on a computer. This was an important case in determining the patentability of software and algorithms.

• Many non-integral values, such as decimal 0.2, have an infinite place-value representation in binary (.001100110011...) but have a finite place-value in binary-coded decimal (0.0010). Consequently a system based on binary-coded decimal representations of decimal fractions avoids errors representing and calculating such values.
• Scaling by a factor of 10 (or a power of 10) is simple; this is useful when a decimal scaling factor is needed to represent a non-integer quantity (e.g., in financial calculations)
• Rounding
Rounding
Rounding a numerical value means replacing it by another value that is approximately equal but has a shorter, simpler, or more explicit representation; for example, replacing \$23.4476 with \$23.45, or the fraction 312/937 with 1/3, or the expression √2 with 1.414.Rounding is often done on purpose to...

at a decimal digit boundary is simpler. Addition and subtraction in decimal does not require rounding.
• Alignment of two decimal numbers (for example 1.3 + 27.08) is a simple, exact, shift.
• Conversion to a character form or for display (e.g., to a text-based format such as XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....

, or to drive signals for a seven-segment display
Seven-segment display
A seven-segment display , or seven-segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot-matrix displays...

) is a simple per-digit mapping, and can be done in linear (O(n)) time. Conversion from pure binary
Binary numeral system
The binary numeral system, or base-2 number system, represents numeric values using two symbols, 0 and 1. More specifically, the usual base-2 system is a positional notation with a radix of 2...

involves relatively complex logic that spans digits, and for large numbers no linear-time conversion algorithm is known (see Binary numeral system).

• Some operations are more complex to implement. Adder
In electronics, an adder or summer is a digital circuit that performs addition of numbers.In many computers and other kinds of processors, adders are used not only in the arithmetic logic unit, but also in other parts of the processor, where they are used to calculate addresses, table indices, and...

s require extra logic to cause them to wrap and generate a carry early. 15–20 percent more circuitry is needed for BCD add compared to pure binary. Multiplication requires the use of algorithms that are somewhat more complex than shift-mask-add (a binary multiplication, requiring binary shifts and adds or the equivalent, per-digit or group of digits is required)
• Standard BCD requires four bits per digit, roughly 20 percent more space than a binary encoding (the ratio of 4 bits to log210 bits is 1.204). When packed so that three digits are encoded in ten bits, the storage overhead is greatly reduced, at the expense of an encoding that is unaligned with the 8-bit byte boundaries common on existing hardware, resulting in slower implementations on these systems.

• Practical existing implementations of BCD are typically slower than operations on binary representations, especially on embedded systems, due to limited processor support for native BCD operations.

## Application

The BIOS
BIOS
In IBM PC compatible computers, the basic input/output system , also known as the System BIOS or ROM BIOS , is a de facto standard defining a firmware interface....

in many personal computer
Personal computer
A personal computer is any general-purpose computer whose size, capabilities, and original sales price make it useful for individuals, and which is intended to be operated directly by an end-user with no intervening computer operator...

s stores the date and time in BCD because the MC6818 real-time clock chip used in the original IBM PC AT motherboard provided the time encoded in BCD. This form is easily converted into ASCII for display.

The Atari 8-bit family
Atari 8-bit family
The Atari 8-bit family is a series of 8-bit home computers manufactured from 1979 to 1992. All are based on the MOS Technology 6502 CPU and were the first home computers designed with custom coprocessor chips...

of computers used BCD to implement floating-point algorithms. The MOS 6502
MOS Technology 6502
The MOS Technology 6502 is an 8-bit microprocessor that was designed by Chuck Peddle and Bill Mensch for MOS Technology in 1975. When it was introduced, it was the least expensive full-featured microprocessor on the market by a considerable margin, costing less than one-sixth the price of...

processor used has a BCD mode that affects the addition and subtraction instructions.

Early models of the PlayStation 3
PlayStation 3
The is the third home video game console produced by Sony Computer Entertainment and the successor to the PlayStation 2 as part of the PlayStation series. The PlayStation 3 competes with Microsoft's Xbox 360 and Nintendo's Wii as part of the seventh generation of video game consoles...

store the date and time in BCD. This led to a worldwide outage of the console on 1 March 2010. The last two digits of the year stored as BCD were misinterpreted
Time formatting and storage bugs
In computer science, time formatting and storage bugs are a class of software bugs which may cause time and date calculation or display to be improperly handled. These are most commonly manifestations of arithmetic overflow, but can also be the result of other issues...

as 16 causing a paradox in the unit's date, rendering most functionalities inoperable.

## Representational variations

Various BCD implementations exist that employ other representations for numbers. Programmable calculator
Programmable calculator
Programmable calculators are calculators that can automatically carry out a sequence of operations under control of a stored program, much like a computer. The first programmable calculators such as the IBM CPC used punched cards or other media for program storage...

s manufactured by Texas Instruments
Texas Instruments
Texas Instruments Inc. , widely known as TI, is an American company based in Dallas, Texas, United States, which develops and commercializes semiconductor and computer technology...

, Hewlett-Packard
Hewlett-Packard
Hewlett-Packard Company or HP is an American multinational information technology corporation headquartered in Palo Alto, California, USA that provides products, technologies, softwares, solutions and services to consumers, small- and medium-sized businesses and large enterprises, including...

, and others typically employ a floating-point BCD format, typically with two or three digits for the (decimal) exponent. The extra bits of the sign digit may be used to indicate special numeric values, such as infinity
Infinity
Infinity is a concept in many fields, most predominantly mathematics and physics, that refers to a quantity without bound or end. People have developed various ideas throughout history about the nature of infinity...

, underflow
Arithmetic underflow
The term arithmetic underflow is a condition in a computer program that can occur when the true result of afloating point operation is smaller in magnitude...

/overflow
Arithmetic overflow
The term arithmetic overflow or simply overflow has the following meanings.# In a computer, the condition that occurs when a calculation produces a result that is greater in magnitude than that which a given register or storage location can store or represent.# In a computer, the amount by which a...

, and error (a blinking display).

### Signed variations

Signed decimal values may be represented in several ways. The COBOL
COBOL
COBOL is one of the oldest programming languages. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....

programming language, for example, supports a total of five zoned decimal formats, each one encoding the numeric sign in a different way:
Type Description Example
Unsigned No sign nibble `F1 F2 F3`
Signed trailing (canonical format) Sign nibble in the last (least significant) byte `F1 F2 C3`
Signed leading (overpunch) Sign nibble in the first (most significant) byte `C1 F2 F3`
Signed trailing separate Separate sign character byte (`'+'` or `'−'`) following the digit bytes `F1 F2 F3 2B`
Signed leading separate Separate sign character byte (`'+'` or `'−'`) preceding the digit bytes `2B F1 F2 F3`

## Alternative encodings

If errors in representation and computation are more important than the speed of conversion to and from display, a scaled binary representation may be used, which stores a decimal number as a binary-encoded integer and a binary-encoded signed decimal exponent. For example, 0.2 can be represented as 2.

This representation allows rapid multiplication and division, but may require shifting by a power of 10 during addition and subtraction to align the decimal points. It is appropriate for applications with a fixed number of decimal places that do not then require this adjustment— particularly financial applications where 2 or 4 digits after the decimal point are usually enough. Indeed this is almost a form of fixed point arithmetic since the position of the radix point
In mathematics and computing, a radix point is the symbol used in numerical representations to separate the integer part of a number from its fractional part . "Radix point" is a general term that applies to all number bases...

is implied.

Chen-Ho encoding
Chen-Ho encoding
Chen-Ho encoding is an alternate system of binary encoding for decimal digits.The traditional system of binary encoding for decimal digits, known as Binary-coded decimal , uses four bits to encode each digit, resulting in significant wastage of binary data bandwidth .The concepts behind Chen-Ho...

provides a boolean transformation for converting groups of three BCD-encoded digits to and from 10-bit values that can be efficiently encoded in hardware with only 2 or 3 gate delays. Densely Packed Decimal
Densely Packed Decimal
Densely packed decimal is a system of binary encoding for decimal digits.The traditional system of binary encoding for decimal digits, known as Binary-coded decimal , uses four bits to encode each digit, resulting in significant wastage of binary data bandwidth...

is a similar scheme that is used for most of the significand
Significand
The significand is part of a floating-point number, consisting of its significant digits. Depending on the interpretation of the exponent, the significand may represent an integer or a fraction.-Examples:...

, except the lead digit, for one of the two alternative decimal encodings specified in the IEEE 754-2008 standard.

• Bi-quinary coded decimal
Bi-quinary coded decimal
Bi-quinary coded decimal is a numeral encoding scheme used in many abacuses and in some early computers, including the Colossus. The term bi-quinary indicates that the code comprises both a two-state and a five-state component...

• Chen-Ho encoding
Chen-Ho encoding
Chen-Ho encoding is an alternate system of binary encoding for decimal digits.The traditional system of binary encoding for decimal digits, known as Binary-coded decimal , uses four bits to encode each digit, resulting in significant wastage of binary data bandwidth .The concepts behind Chen-Ho...

• Densely packed decimal
Densely Packed Decimal
Densely packed decimal is a system of binary encoding for decimal digits.The traditional system of binary encoding for decimal digits, known as Binary-coded decimal , uses four bits to encode each digit, resulting in significant wastage of binary data bandwidth...

• Double dabble
Double dabble
In computer science, the double dabble algorithm is used to convert binary numbers into decimal . The algorithm operates as follows:...

, an algorithm for converting binary numbers to BCD
• Gray code
Gray code
The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only one bit. It is a non-weighted code....

• Year 2000 problem
Year 2000 problem
The Year 2000 problem was a problem for both digital and non-digital documentation and data storage situations which resulted from the practice of abbreviating a four-digit year to two digits.In computer programs, the practice of representing the year with two...

• Decimal computer
Decimal computer
Decimal computers, computers which have a decimal architecture, represent numbers and/or addresses in decimal, and provide instructions to operate on those numbers and/or addresses directly; examples of encoding used are BCD, Excess-3, two-out-of-five code, ASCII, and EBCDIC.Many early computers,...