## Arithmetic with binary-coded decimal (BCD) Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Arithmetic with binary-coded decimal (BCD)

I was thinking about binary-coded decimal, and here is something I found when googling "BCD arithmetic":
http://www.divms.uiowa.edu/~jones/bcd/bcd.html

I am not quite sure how to (mathematically) prove that some of them work, though. But I can still play with them.

Here is my own contribution (untested):
Code: Select all | TOGGLE FULL SIZE
half(a)
t1 = a >> 1
t2 = t1 & 0x08888888
t3 = (t2 >> 2) | (t2 >> 3)
return t1 - t3
odometer

Posts: 98
Joined: Sun Aug 21, 2011 11:01 pm

Re: Arithmetic with binary-coded decimal (BCD)

How many bits is t2?

zener

Posts: 2951
Joined: Sat Feb 21, 2009 2:38 am

Re: Arithmetic with binary-coded decimal (BCD)

Zener wrote:How many bits is t2?

These are all 32-bit variables. Please see the link; this piece of pseudocode here is in the same format as the ones on the linked page.
odometer

Posts: 98
Joined: Sun Aug 21, 2011 11:01 pm

Re: Arithmetic with binary-coded decimal (BCD)

In the good old days, the compiler automatically did arithmetic on BCD variables using the CPU's BCD arithmetic instructions. :)

Len17

Posts: 393
Joined: Sat Mar 14, 2009 7:20 pm

Re: Arithmetic with binary-coded decimal (BCD)

Len17 wrote:In the good old days, the compiler automatically did arithmetic on BCD variables using the CPU's BCD arithmetic instructions. :)

risc fixed that.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more

mtbf0

Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Please be positive and constructive with your questions and comments.