Binary coded decimal: Difference between revisions

Forth version
m (Thundergnat moved page Binary-Coded Decimal to Binary coded decimal: capitalization policy)
(Forth version)
Line 82:
29
0100</pre>
=={{header|Forth}}==
This code implements direct BCD arithmetic using notes from Douglas Jones from the University of Iowa: https://homepage.cs.uiowa.edu/~jones/bcd/bcd.html#packed
<lang Forth>
\ add two 15 digit bcd numbers
\
: bcd+ ( n1 n2 -- n3 )
0x0666666666666666 + \ offset the digits in n2
2dup xor \ add, discounting carry
-rot + swap \ add with carry (only carries have correct digit)
over xor \ bitmask of where carries occurred.
invert 0x1111111111111110 and \ invert then change digit to 6
dup 2 rshift swap 3 rshift or \ in each non-carry position
- 0x0FFFFFFFFFFFFFFF and ; \ subtract bitmask from result, discard MSD
 
: bcdneg ( n -- n ) \ reduction of 9999...9999 swap - 1 bcd+
negate 0x0FFFFFFFFFFFFFFF and dup 1-
1 xor over xor invert 0x1111111111111110 and
dup 2 rshift swap 3 rshift or - ;
 
: bcd- bcdneg bcd+ ;
</lang>
{{Out}}
<pre>
Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc.
Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
Type `bye' to exit
hex ok
19 1 bcd+ . 20 ok
30 1 bcd- . 29 ok
99 1 bcd+ . 100 ok
</pre>
 
=={{header|J}}==
 
Line 115 ⟶ 147:
25
NB. ...</lang>
 
 
=={{header|Julia}}==
357

edits