Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Does anyone know of a BCD (Binary coded decimal) library available for C?

The Binary Coded Decimal, or BCD code, is used as the direct code to communicate decimal numbers using

a binary code. Recall that the decimal numbers are 0 to 9. BCD code is used to directly translate these

decimal numbers using a 4-bit code.

Here are the decimal equivalent 4-bit BCD codes;

Decimal BCD

0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

Notice that each single decimal digit has an equivalent 4-bit BCD code.

Converting Decimal to BCD

This conversion simply requires you to represent each decimal digit with its 4-bit BCD code.

Eg. 38210 = (310 = 0011BCD), (810 = 1000BCD) and (210 = 0010BCD) so;

the result is 001110000010BCD and dropping the leading zeros = 1110000010BCD

Converting BCD to Decimal

This is just grouping every 4 bits of BDC code (starting at the LSD) and representing each 4-bit code

with its equivalent decimal number.

Eg. 100100101000BCD = (1000 = 8), (0010 = 2) and (1001 = 9) so;

the decimal equivalent is 92810

Nevertheless, check out this one: http://www.1cplusplusstreet.com/vb/scripts/ShowCode.asp?txtCodeId=625&lngWId=3 ("BCD math")

True, google gives you scores of links -- but 'quantity' is spelled differently from 'quality'.

@ philsmicronet:

Basically, any x86-processor supports BCD's. I didn't find a link with a BCD math library. However, this might be useful to you:

http://www.intel.com/design/intarch/techinfo/Pentium/instsum.htm

Search for "Packed BCD Adjustment Instructions"/"Unpacked BCD Adjustment Instructions" -- it deals with double/single-digit BCD's only, but can easily extended to multi-digit-BCD's. It is 16-bit code so don't expect any high-performance-operation

Addition [using aaa = "ascii adjust after addition"]:

mov ax,9 ;ax = 0x0009

mov bx,3 ;bx = 0x0003

add al,bl ;ax = 0x000c = 12d

aaa ;ax = 0x0102

Multiply [using aam = "ascii adjust after multiply"]:

mov al,5 ;al = 0x0005

mov bl,7 ;bl = 0x0007

mul bl ;ax = al * bl = 0x0023 = 35d

aam ;ax = 0x0305

Hope that provides at least some help to you. Regards,

.f

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

1st of all - is this homework?

If not, and if I can find the code, would you be interested in it?

Jim