Anonymous user
Four bit adder: Difference between revisions
Added parameter types. Some formatting changes. Added some types.
(Added Wren) |
(Added parameter types. Some formatting changes. Added some types.) |
||
Line 3,931:
=={{header|Nim}}==
{{trans|Python}}
<lang nim>type
<lang nim>proc ha(a, b): auto = [a xor b, a and b] # sum, carry▼
Bools[N: static int] = array[N, bool]
proc fa(a, b, ci): auto =▼
SumCarry = tuple[sum, carry: bool]
let a = ha(ci, a)
let b = ha(a[0], b)
proc fa4(a, b: Bools[4]):
var co, s:
for i in 0..3:
let r = fa(a[i], b[i], if i > 0: co[i-1] else: false)
Line 3,947 ⟶ 3,952:
result[4] = co[3]
proc int2bus(n: int):
var n = n
for i in 0..result.high:
Line 3,953 ⟶ 3,958:
n = n shr 1
proc bus2int(b: Bools): int =
for i, x in b:
result += (if x: 1 else: 0) shl i
|