Computers operate on numeric data using binary representations of the number. An alternative method of transmitting numbers to the processor is to use Binary Coded Decimal (BCD). BCD encodes a number by translating each individual digit of the number into a separate byte. This method has no upper limit on the size of number transmitted. Due to the extreme value of storage in a computer system, using BCD wastes the upper nibble (4-bits) of every byte. A system referred to as packed BCD solves this problem. In packed BCD, each nibble of a byte contains one digit of the number encoded into binary notation.
Things You'll Need
- Decimal number
Break the decimal number you wish to encode into packed BCD notation into individual digits. Do not mix up the order of the digits as that will change the number. For example, write 9341 in packed BCD format. Break the number down into its digits, retaining the same ordering: 9, 3, 4 and 1.
Convert the first digit from the right into binary code. The digits from 0 to 9 are represented by the binary numbers 0000 to 1001. This becomes the first four bits of the byte. Continuing the example, convert the decimal 1 to binary, 00000001.
Convert the next digit in line from the right into binary. Replace the first four bits of the byte, all of which are zeros, with the rightmost four bits of the binary number. You now have a single byte that holds the information of a two-digit number. One byte coded as a packed BCD can represent decimals from 00 to 99. Continuing the example, the next digit is 4. Converted to binary it is 00000100. Replace the last four bits with the first four bits of the byte containing the first number. Place 0100 into the first byte in the four leftmost positions resulting in 01000001. This byte represents 41 in packed BCD format.
Repeat the conversion of digits and construction of bytes in the same manner for the other digits in the decimal number. Your result will be a series of bytes that represent any decimal number. Completing the example, the last two digits are 3 and 9. Their binary equivalents are 00000011 and 00001001. Putting these together into a single byte in the correct order results in 10010011. The packed BCD for decimal 9341 is 10010011 01000001.
- Photo Credit Jupiterimages/Photos.com/Getty Images