Programming
Encoding a BCD Number
Definition
BCD represents each of the digits of an unsigned decimal
as the 4-bit binary equivalents.
Unpacked BCD
Unpacked BCD representation contains only one decimal
digit per byte. The digit is stored in the least significant 4 bits; the most
significant 4 bits are not relevant to the value of the represented number.
Packed BCD
Packed BCD representation packs two decimal digits into a
single byte.
Invalid BCD Numbers
These binary numbers are not allowed in the BCD
code: 1010, 1011, 1100, 1101, 1110, 1111
Packing a Two-Byte BCD
To pack a two-byte unpacked BCD number into a single byte
creating a packed BCD number, shift the upper byte left four times,
then OR the results with the lower byte.
For example,
Converting between Decimal and BCD
From Decimal to Unpacked BCD: To convert
a decimal number into an unpacked BCD number, assign each decimal digit its
8-bit binary equivalent.
For example,
From Decimal to Packed BCD: To convert a
decimal number into a packed BCD number, assign each digit of the decimal to
its 4-bit equivalent, padding the upper nybble with zeroes if necessary.
For example,
From BCD to Decimal: To convert a BCD
into a decimal number, just reverse the appropriate process from above;
beginning with the LSB, group the binary digits by either 4 or 8 bits for
packed and unpacked, respectively, then convert each set into its decimal
equivalent.
Converting between Binary and BCD
From Binary to Unpacked BCD: To convert a
binary number into an unpacked BCD, divide the binary number by decimal 10
and place the quotient in the most significant byte and the remainder in the
least significant byte.
For example,
From Two-Byte Unpacked BCD to Binary: To
convert from a two-byte unpacked BCD to a binary number, multiply the most
significant byte of the BCD by decimal ten, then add the product to the least
significant byte.
For example,
00001001 00000010(unpacked BCD) = 01011100(base 2)
BCD Addition
Either packed or unpacked BCD numbers can be summed. BCD
addition follows the same rules as binary addition. However, if the
addition produces a carry and/or creates an invalid BCD number, an adjustment
is required to correct the sum. The correction method is to add 6 to the sum
in any digit position that has caused an error.
For example,
BCD Subtraction
Either packed or unpacked BCD numbers can be subtracted.
BCD subtraction follows the same rules as binary subtraction. However,
if the subtraction causes a borrow and/or creates an invalid BCD number, an
adjustment is required to correct the answer. The correction method is to
subtract 6 from the difference in any digit position that has caused an
error.
For example,
Unpacked BCD Multiplication
Multiplication cannot be performed on packed BCD; the 4
most significant bits must be zeroed for the adjustment to work. Multiply the
two unpacked BCD numbers using the rules for binary multiplication. To
adjust the product divide it by decimal 10, then place the quotient in the
most significant byte and the remainder in the least significant byte (convert
the binary answer to unpacked BCD).
For example,
Unpacked BCD Division
BCD division also cannot be performed on packed numbers.
Before dividing an unpacked BCD number, the division adjustment is made
by converting the BCD numbers to binary. Adjust the two-byte BCD number
by multiplying the upper byte by decimal 10 and adding the product to the
lower byte. After the adjustment, divide the two binary numbers using the
rules of binary arithmetic. Finally, convert the binary quotient
into an unpacked BCD number if necessary.
For example,
Other Decimal Codes
ASCII Representation of Digits
ASCII digits are examples of unpacked binary coded
decimals. A digit is represented as its 4-bit binary equivalent, which is
stored in the lower nybble. The upper nybble contains 011 and has
no bearing on the value of the represented number.
EBCDIC Representation of Digits
EBCDIC digits are similiar to ASCII digits. The
lower nybble still contains the 4-bit binary equivalent, but the upper nybble
is padded with 1111 instead of 011.
Excess-3 (XS3)
In excess-3 representation, each decimal digit is the
4-bit binary equivalent with 3 (0011) added.
4221
For 4221 coded-decimals, each digit also is represented as
a 4-bit group. Instead of each bit representing a successive power of two (8,
4, 2, and 1, from the left) as in binary, the bits represent 4, 2, 2, and 1.
|