Binary-coded decimal arithmetic

Anyone have a good set of functions for binary-coded decimal arithmetic?

One could increment a 2-digit BCD integer as follows:

x += ((x&15)==9?7:1); // does not check for overflow

or increase a 2-digit BCD integer by a given value as follows:

x += y;
if (((x&15)>9) || ((x&15)<(y&15))) x+=6;

but I want to know if there are faster or more efficient functions than these.

At this point, I am really only interested in 2-digit BCD numbers, though.

http://www.divms.uiowa.edu/~jones/bcd/bcd.html

Yes, but his algorithms seem a bit involved, at least for 2-digit BCD numbers.

The code I gave was just written off the top of my head.

Well, why do you want to do this? You could add them as "normal" numbers and then convert them.

Normally you do everything in binary and only convert to BCD if you have to display a number.

Why do you need to work with BCD?


Rob