Four bit adder: Difference between revisions

→‎{{header|APL}}: mark carry explicitly in demo output
(→‎{{header|APL}}: Make GNU-compatible.)
(→‎{{header|APL}}: mark carry explicitly in demo output)
Line 513:
{{works with|Dyalog APL}}
{{works with|GNU APL}}
<lang apl>⍝ Our primitive "gates" are built-in, but letslet's give them descriptive names
not ← { ~ ⍵ }
and ← { ⍺ ∧ ⍵ }
Line 522:
xor ← { (⍺ and not ⍵) or (⍵ and not ⍺) }
 
⍝ And the multigate components. Our bit vectors are MSB first, so for consistency
⍝ the carry bit is returned as the leftmost element as well.
half_adder ← { (⍺ xorand ⍵), ⍺ andxor ⍵ } ⍝ returns sumcarry, carrysum
 
⍝ GNU APL dfns can't have multiple statements, so the other adders are defined as tradfns
∇result ← c_in full_adder args ; c_in; a; b; s0; c0; s1; c1
(a b) ← args
(s0 c0 s0) ← c_in half_adder a
(s1 c1 s1) ← s0 half_adder b
result ← (c0 or c1), s1
Line 545 ⟶ 546:
 
⍝ A demo function
demo ← { ⎕ ← ⍺⎕←⍺,'+',⍵,'=',{ 1↓⍵,' with carry ',1↑⍵ } ⍺ adder4 ⍵ }
 
⍝ A way to generate some random numbers for our demo
Line 556 ⟶ 557:
{{Out}}
<pre>
01 1 1 01 + 10 0 1 0 = 1 = 0 0 0 0 with carry 1
1 01 0 0 + 1 10 0 0 1 = 1 1 0 1 0 with carry 0
0 1 1 0 1 + 0 1 1 1 = 1 01 1 0 with carry 0
1 1 0 1 0 + 01 0 01 01 = 1 0 10 1 0 with carry 1
0 1 0 0 1 + 10 10 1 10 = 0 1 1 0 0 with carry 0
0 1 0 01 1 + 0 1 0 1 0 = 0 1 0 0 1 with carry 1
1 01 01 1 + 0 1 0 1 1 = 0 1 10 1 0 with carry 1
10 1 1 10 + 0 10 1 10 = 1 0 10 0 with carry 1 0
1 1 0 1 + 0 1 10 0 = 10 0 0 1 with carry 1
1 0 1 1 0 + 10 10 1 1 = 1 0 1 0 1 with carry 0
1 1 1 1 + 0 0 0 1 = 1 0 0 0 0 with carry 1
1 0 1 1 1 + 1 0 1 1 1 = 1 0 0 1 0 with carry 1
0 1 10 0 1 + 1 1 10 0 = 1 1 0 1 0 with carry 0
0 0 1 0 + 1 +1 01 1 = 0 0 = 0 1 0 0with carry 1
1 0 0 1 0 + 0 1 0 0 1 = 0 1 01 0 1 with carry 0
01 1 0 1 0 + 1 0 1 0 = 1 =0 0 10 1 1with 0carry 1
1 10 0 0 + 1 0 0 0 1 = 0 10 10 0 with carry 1
1 0 1 0 + 10 10 10 10 = 1 0 1 0 0 1with carry 0
1 0 1 1 0 + 1 1 1 01 = 1 0 1 0 0 with carry 1
1 1 0 1 1 + 1 0 10 1 = 1 0 1 1 0 with carry 1</pre>
 
=={{header|AutoHotkey}}==
1,480

edits