Binary coded decimal: Difference between revisions
Content added Content deleted
m (→{{header|RPL}}) |
(Created Nim solution.) |
||
Line 642: | Line 642: | ||
BCD 30 (UInt8[0x30]) - BCD 1 ((UInt8[0x01], 1)[1]) = BCD 29 ((UInt8[0x29], 1)) |
BCD 30 (UInt8[0x30]) - BCD 1 ((UInt8[0x01], 1)[1]) = BCD 29 ((UInt8[0x29], 1)) |
||
BCD 99 (UInt8[0x99]) + BCD 1 ((UInt8[0x01], 1)[1]) = BCD 100 ((UInt8[0x01, 0x00], 1)) |
BCD 99 (UInt8[0x99]) + BCD 1 ((UInt8[0x01], 1)[1]) = BCD 100 ((UInt8[0x01, 0x00], 1)) |
||
</pre> |
|||
=={{header|Nim}}== |
|||
{{trans|Rust}} |
|||
We define a type <code>Bcd64</code> as derived but distinct of <code>uint64</code> and operators and functions working on this type. |
|||
<syntaxhighlight lang="Nim">import std/strutils |
|||
type Bcd64 = distinct uint64 |
|||
func `+`(a, b: Bcd64): Bcd64 = |
|||
let t1 = a.uint64 + 0x0666_6666_6666_6666u64 |
|||
let t2 = t1 + b.uint64 |
|||
let t3 = t1 xor b.uint64 |
|||
let t4 = not(t2 xor t3) and 0x1111_1111_1111_1110u64 |
|||
let t5 = (t4 shr 2) or (t4 shr 3) |
|||
result = Bcd64(t2 - t5) |
|||
func `-`(a: Bcd64): Bcd64 = |
|||
## Return 10's complement. |
|||
let t1 = cast[uint64](-cast[int64](a)) |
|||
let t2 = t1 + 0xFFFF_FFFF_FFFF_FFFFu64 |
|||
let t3 = t2 xor 1 |
|||
let t4 = not(t2 xor t3) and 0x1111_1111_1111_1110u64 |
|||
let t5 = (t4 shr 2) or (t4 shr 3) |
|||
result = Bcd64(t1 - t5) |
|||
func `-`(a, b: Bcd64): Bcd64 = |
|||
a + (-b) |
|||
func `$`(n: Bcd64): string = |
|||
var s = n.uint64.toHex |
|||
var i = 0 |
|||
while i < s.len - 1 and s[i] == '0': |
|||
inc i |
|||
result = "0x" & s[i..^1] |
|||
const One = Bcd64(0x01u64) |
|||
echo "$1 + $2 = $3".format(Bcd64(0x19), One, Bcd64(0x19) + One) |
|||
echo "$1 - $2 = $3".format(Bcd64(0x30), One, Bcd64(0x30) - One) |
|||
echo "$1 + $2 = $3".format(Bcd64(0x99), One, Bcd64(0x99) + One) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>0x19 + 0x1 = 0x20 |
|||
0x30 - 0x1 = 0x29 |
|||
0x99 + 0x1 = 0x100 |
|||
</pre> |
</pre> |
||