Memorial day is 05/25/2015 – Orders will ship out on May 26th. There will not be any deliveries or shipping on Monday. Any order placed after 11am ET on Friday May 22nd will not start to ship out until Tuesday May 26th.

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

Arithmetic with binary-coded decimal (BCD)

by odometer on Wed Feb 29, 2012 8:11 pm

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)

by zener on Fri Mar 02, 2012 3:09 pm

How many bits is t2?

zener
 
Posts: 2880
Joined: Sat Feb 21, 2009 2:38 am

Re: Arithmetic with binary-coded decimal (BCD)

by odometer on Sat Mar 03, 2012 7:14 pm

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)

by Len17 on Sun Mar 04, 2012 8:04 pm

In the good old days, the compiler automatically did arithmetic on BCD variables using the CPU's BCD arithmetic instructions. :)
User avatar
Len17
 
Posts: 393
Joined: Sat Mar 14, 2009 7:20 pm

Re: Arithmetic with binary-coded decimal (BCD)

by mtbf0 on Sun Mar 11, 2012 11:17 am

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
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca