Jump to content

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]
 
<lang nim>proc ha(a, b: bool): autoSumCarry = [(a xor b, a and b] # sum, carry)
 
proc fa(a, b, ci: bool): autoSumCarry =
let a = ha(ci, a)
let b = ha(a[0], b)
[result = (b[0], a[1] or b[1]] # sum, carry)
 
proc fa4(a, b: Bools[4]): arrayBools[5, bool] =
var co, s: arrayBools[4, bool]
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): arrayBools[4, bool] =
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
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.