Four bit adder: Difference between revisions

→‎{{header|APL}}: use built-in NAND operator ⍲ instead of defining a new one.
m (→‎{{header|Quackery}}: tweaked commentary)
(→‎{{header|APL}}: use built-in NAND operator ⍲ instead of defining a new one.)
 
(6 intermediate revisions by 4 users not shown)
Line 517:
and ← { ⍺ ∧ ⍵ }
or ← { ⍺ ∨ ⍵ }
nand ← { ⍺ ⍲ ⍵ }
 
⍝ Build the complex gates
nand ← { not ⍺ and ⍵ } ⍝ similarly this can be built with composition as "nand ← not and"
xor ← { (⍺ and not ⍵) or (⍵ and not ⍺) }
 
Line 2,460:
1111 + 1111 = 1110 c=1
</pre>
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
proc xor a b . r .
na = bitand bitnot a 1
nb = bitand bitnot b 1
r = bitor bitand a nb bitand b na
.
proc half_add a b . s c .
xor a b s
c = bitand a b
.
proc full_add a b c . s g .
half_add a c x y
half_add x b s z
g = bitor y z
.
proc bit4add a4 a3 a2 a1 b4 b3 b2 b1 . s4 s3 s2 s1 c .
full_add a1 b1 0 s1 c
full_add a2 b2 c s2 c
full_add a3 b3 c s3 c
full_add a4 b4 c s4 c
.
write "1101 + 1011 = "
bit4add 1 1 0 1 1 0 1 1 s4 s3 s2 s1 c
print c & s4 & s3 & s2 & s1
</syntaxhighlight>
{{out}}
<pre>
1101 + 1011 = 11000
</pre>
 
=={{header|Elixir}}==
{{works with|Elixir|1.1}}
Line 2,783 ⟶ 2,814:
 
[[File:Fōrmulæ - Four bit adder 06.png]]
 
<span>&nbsp;&nbsp;:</span>
 
[[File:Fōrmulæ - Four bit adder 07.png]]
Line 5,500 ⟶ 5,533:
=={{header|Quackery}}==
 
[[File:Xor illustratedin Quackery .png|thumb|right]]
Stack based languages such as Quackery have a simple correspondence between the words that constitute the language and logic gates and their wiring. This is illustrated in the imagestackflow diagram on the right, which shows the mapping between gates and wiring, and Quackery words in the definition of <code>xor</code>.
 
The wiring on the left hand side corresponds to the Quackery stack, which by convention builds up from left to right, so the rightmost item is the top of stack.
Line 5,521 ⟶ 5,554:
The first is <code>4 pack reverse unpack</code>. <code>4 pack</code>, takes the top 4 arguments off the stack and puts them into a nest. <code>reverse</code> reverses the order of the items in a nest, and <code>unpack</code> does the opposite of <code>4 pack</code>. If the stack contained <code>1 2 3 4</code> before performing <code>4 pack reverse unpack</code>, it would contain <code>4 3 2 1</code> afterwards.
 
This is necessary, because moving four items from the stack to the ancillary stack <code>temp</code> using <code>4 times [ temp put ]</code> and then bringing them back one at a time using <code>temp take</code> will reverse their order, so we preemptively reverse it to counteract that. It is desirable for the task for arguments to <code>4bitadder</code> to be in most significant bit first order so that the intent of, for example, <code>1 1 0 0   1 1 0 0 4bitadder</code> is immediately obvious.
 
The same reasoning applies to the second instance of <code>4 pack reverse</code>; <code>witheach</code> iterates over a nest, placing each item in the nest (from left to right) on the stack before performing the word or nest that follows it. The <code>fulladder</code> within the nest needs to operate from least to most significant bit, as per the diagram in the task description.
Line 7,079 ⟶ 7,112:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var xor = Fn.new { |a, b| a&(~b) | b&(~a) }
 
var ha = Fn.new { |a, b| [xor.call(a, b), a & b] }
1,480

edits