microEngineering Labs, Inc. 7195205323 

BCD (Binary Coded Decimal) Number SystemYou should now be familiar with the Binary, Decimal and Hexadecimal Number System. If we view single digit values for hex, the numbers 0  F, they represent the values 0  15 in decimal, and occupy a nibble. Often, we wish to use a binary equivalent of the decimal system. This system is called Binary Coded Decimal or BCD which also occupies a nibble. In BCD, the binary patterns 1010 through 1111 do not represent valid BCD numbers, and cannot be used. Conversion from Decimal to BCD is straightforward. You merely assign each digit of the decimal number to a byte and convert 0 through 9 to 00000000 through 00001001, but you cannot perform the repeated division by 2 as you did to convert decimal to binary. Let us see how this works. Determine the BCD value for the decimal number 5,319. Since there are four digits in our decimal number, there are four bytes in our BCD number. They are:
Since computer storage requires the minimum of 1 byte, you can see that the upper nibble of each BCD number is wasted storage. BCD is still a weighted position number system so you may perform mathematics, but we must use special techniques in order to obtain a correct answer. 
PACKED BCDSince storage in RAM is so valuable, we would like to eliminate this wasted storage. This may be accomplished by packing the BCD numbers. In a packed BCD number, each nibble has a weighted position starting from the decimal point. Therefore, instead of requiring 4 bytes to store the BCD number 5319, we would only require 2 bytes, half the storage. The upper nibble of the upper byte of our number would store the THOUSANDS value while the lower nibble of the upper byte would store the HUNDREDS value. Likewise, the lower byte would store the TENS value in the upper nibble and the UNITS digit in the lower nibble. Therefore, our previous example would be:
Here is some PBP code to pack 4 digits of BCD data into 2 bytes of memory. The 4 digits of BCD are stored in the byte variables: BCD1000, BCD100, BCD10, BCD1. Use the following to pack into the byte variables: PACKED_HIGH and PACKED_LOW. BCD1000 = $5 BCD100 = $3 BCD10 = $1 BCD1 = $9 PACKED_HIGH = (BCD1000<<4) + BCD100 PACKED_LOW = (BCD10<<4) + BCD1

Copyright 2011 microEngineering Labs, Inc. 2845 Ore Mill Road, STE 4 Colorado Springs CO 80904 (719) 5205323 (719) 5201867 fax email: support@melabs.com 
Home 
Products 
Support 
Resources 
Purchase 
Contact
