Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Binary-coded decimal
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Packed BCD== <!-- Section header used in redirects --> Some computers whose words are multiples of an [[Octet (computing)|octet]] (8-bit byte), for example contemporary IBM mainframe systems, support '''packed BCD''' (or '''packed decimal'''<ref name="Dewar-Smosna_1990"/>) numeric representations, in which each [[nibble]] represents either a decimal digit or a sign.<ref group="nb" name="Packed_chars"/> 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 ([[hexadecimal|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).<ref name="IBM_1980_POP"/><ref name="DEC_1983_PDP11"/><ref name="DEC_1985_VAX11"/> ILE RPG uses 1111 (F) for positive and 1101 (D) for negative.<ref name="ILE-RPG"/> 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)). {| class="wikitable" style="margin:auto; width:40%;" |- ! style="background:#e0e0e0; width:20%;"|Sign<br />digit ! style="background:#e0e0e0; width:20%;"|BCD<br />8 4 2 1 ! style="background:#e0e0e0; width:20%;"|Sign ! style="background:#e0e0e0; width:40%;"|Notes |- style="text-align:center;" ! style="background:#f0f0f0;"|A | 1 0 1 0 | '''+''' | |- style="text-align:center;" ! style="background:#f0f0f0;"|B | 1 0 1 1 | '''β''' | |- style="text-align:center;" ! style="background:#f0f0f0;"|C | 1 1 0 0 | '''+''' | Preferred |- style="text-align:center;" ! style="background:#f0f0f0;"|D | 1 1 0 1 | '''β''' | Preferred |- style="text-align:center;" ! style="background:#f0f0f0;"|E | 1 1 1 0 | '''+''' | |- style="text-align:center;" ! style="background:#f0f0f0;"|F | 1 1 1 1 | '''+''' | Unsigned |} No matter how many bytes wide a [[Word (computer architecture)|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 (2''n'')β1 decimal digits, which is always an odd number of digits. A decimal number with ''d'' digits requires {{sfrac|1|2}}(''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 β Like character strings, the first byte of the packed decimal{{snd}} that with the most significant two digits{{snd}} 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 (character encoding)|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 [[Burroughs 205|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 β50,000,000 to +49,999,999, 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 arithmetic|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 +1,234.567 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 2<sup>10</sup> (1,024) is greater than 10<sup>3</sup> (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]]'' (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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)