Some computers whose words are multiples of an
octet (8-bit byte), for example contemporary IBM mainframe systems, support
packed BCD (or
packed decimal) numeric representations, in which each
nibble represents either a decimal digit or a sign. Packed BCD has been in use since at least the 1960s and is implemented in all IBM mainframe hardware since then. Most implementations are
big endian, i.e. with the more significant digit in the upper half of each byte, and with the 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 some unsigned representations lack a sign flag. 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 C) for positive (+) and 1101 (D) for negative (−). This convention comes from the zone field for
EBCDIC characters and the
signed overpunch representation. Other allowed signs are 1010 (A) and 1110 (E) for positive and 1011 (B) for negative. IBM System/360 processors will use the 1010 (A) and 1011 (B) signs if the A bit is set in the PSW, for the ASCII-8 standard that never passed. Most 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. These match the EBCDIC zone for digits without a sign overpunch. 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 is considered a positive sign value (the processors will normalize a positive sign to 1100 (C)). No matter how many bytes wide a
word is, there is always an even number of nibbles because each byte has two of them. Therefore, a word of
n bytes can contain up to decimal digits, which is always an odd number of digits. A decimal number with
d digits requires 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 − Like character strings, the first byte of the packed decimal that 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 4-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 (using about 20% more memory than
binary notation to store the same numbers), conversion to
ASCII, EBCDIC, or the various encodings of
Unicode is made 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 exist which avoid the storage penalty and also need no arithmetic operations for common conversions. Packed BCD is supported in the
COBOL 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. It is supported in
PL/I as "FIXED DECIMAL". Beside the IBM System/360 and later compatible mainframes, packed BCD is implemented in the native instruction set of the original
VAX processors from
Digital Equipment Corporation and some models of the
SDS Sigma series mainframes, and is the native format for the
Burroughs Medium Systems line of mainframes (descended from the 1950s
Electrodata 200 series).
Ten's complement representations for negative numbers offer an alternative approach to encoding the sign of packed (and other) BCD numbers. In this case, positive numbers always have a most significant digit between 0 and 4 (inclusive), while negative numbers are represented by the 10's complement of the corresponding positive number. As a result, this system allows for 32-bit packed BCD numbers to range from to , and −1 is represented as 99999999. (As with two's complement binary numbers, the range is not symmetric about zero.)
Fixed-point packed decimal Fixed-point decimal numbers are supported by some programming languages (such as COBOL and PL/I). 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 when the implied decimal point is located between the fourth and fifth 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 (1024) is greater than 103 (1000), if three decimal digits are encoded together, only 10 bits are needed. Two such encodings are
Chen–Ho encoding and
densely packed decimal (DPD). 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 ==