Arithmetic with binary-coded decimal (BCD)

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Arithmetic with binary-coded decimal (BCD)

Postby odometer » Wed Feb 29, 2012 7: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
half(a)
   t1 = a >> 1
   t2 = t1 & 0x08888888
   t3 = (t2 >> 2) | (t2 >> 3)
   return t1 - t3
odometer
 
Posts: 97
Joined: Sun Aug 21, 2011 10:01 pm

Re: Arithmetic with binary-coded decimal (BCD)

Postby Zener » Fri Mar 02, 2012 2:09 pm

How many bits is t2?
Zener
 
Posts: 2029
Joined: Sat Feb 21, 2009 1:38 am

Re: Arithmetic with binary-coded decimal (BCD)

Postby odometer » Sat Mar 03, 2012 6: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: 97
Joined: Sun Aug 21, 2011 10:01 pm

Re: Arithmetic with binary-coded decimal (BCD)

Postby Len17 » Sun Mar 04, 2012 7: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 6:20 pm

Re: Arithmetic with binary-coded decimal (BCD)

Postby mtbf0 » Sun Mar 11, 2012 10: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: 1642
Joined: Fri Nov 09, 2007 11:59 pm
Location: oakland ca


Return to Microcontrollers

Who is online

Users browsing this forum: No registered users and 4 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [114]

Raspberry Pi[82]
 
FLORA[24]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[12]
Arduino[60]
 
NETduino[14]
 
BeagleBone[23]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[39]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[9]


 
Breakout Boards[35]
LCDs & Displays[49]
Components & Parts[70]
Batteries & Power[54]
EL Wire/Tape/Panel[52]
LEDs[112]
 
Wireless[16]
Cables[66]
 
Lasers[6]
Sensors/Parts[147]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[41]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[25]


 
Stickers[41]
 
Skill badges[55]
 
Books[26]
 
Circuit Playground[7]
 
Gift Certificates[4]