Four bit adder: Difference between revisions

→‎{{header|APL}}: use built-in NAND operator ⍲ instead of defining a new one.
(→‎{{header|APL}}: use built-in NAND operator ⍲ instead of defining a new one.)
 
(11 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,497 ⟶ 5,530:
if __name__ == '__main__':
test_fa4()</syntaxhighlight>
 
=={{header|Quackery}}==
 
[[File:Xor in Quackery .png|thumb]]
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 stackflow 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.
 
The first word, <code>over</code> is a stack management word, it places a copy of the second on stack on the top of the stack. The next word, <code>not</code>, takes one argument from the stack and leaves one result on the stack.
 
After this, <code>over</code> does its thing again, again working on the topmost items on the stack, and then <code>and</code> takes two arguments from the stack and returns one result to the stack. By convention, words other than stack management words consume their arguments. Words can take zero or more arguments, and leave zero or more results.
 
<code>unrot</code> is another stack management word, which moves the top of stack down below the third on stack, the third and second on stack becoming the second on stack and top of stack respectively. (The converse action is <code>rot</code>. It moves the third on stack to the top of stack.)
 
Finally <code>not</code> takes one item from the stack and returns one, <code>and</code> and <code>or</code> both take two items from the stack and return one, leaving one item. So we can see from the diagram that <code>xor</code> takes two items and returns one. The stack comment <code>( b b --> b )</code> reflects this, with the <code>b</code>'s standing for "Boolean".
 
Looking further down the code, <code>halfadder</code> uses the word <code>2dup</code>, which is equivalent to <code>over over</code>, and <code>dip</code>.
 
<code>dip</code> temporarily removes the top of stack from the stack, performs the word or nest (i.e. code wrapped in <code>[</code> and <code>]</code>; "nest" is Quackery jargon for a dynamic array) following it, then returns the top of stack. Here it is followed by the word <code>xor</code>, but it could be just as easily be followed by a stack management word, or a nest of stack management words. Quackery has a small set of stack management words, and <code>dip</code> extends their reach slightly further down the stack.
 
<code>4bitadder</code> is highly unusual in taking eight arguments from the stack and returning five. It would be better practice to group the eight arguments into two nests of four arguments, and return the four bit result as a nest, and the carry bit separately. However this is an opportunity to illustrate other ways that Quackery can deal with more items on the stack than the stack management words available can cope with.
 
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.
 
<code>swap</code> is another stack management word. It swaps the top of stack and second on stack. (The <code>0</code> that it swaps under the reversed nest is a dummy carry bit to feed to the first performance of <code>fulladder</code> within the iterative loop.
 
Finally we reverse the top five items on the stack to make the top of stack the least significant bit and so on, and therefore consistent with the bit order of the arguments.
 
In conclusion, the use of a stack and stack management words to carry data from one word to the next shows how stack based programming languages can be seeing as using structured data-flow as well as using structured control-flow. (<code>times</code> and <code>witheach</code> are two of Quackery's control-flow words.) Thank you for reading to the end. I hope you have enjoyed this brief glimpse into the paradigm of stack based programming with Quackery.
 
<syntaxhighlight lang="Quackery"> [ over not
over and
unrot not
and or ] is xor ( a b --> a^b )
 
[ 2dup and
dip xor ] is halfadder ( a b --> s c )
 
[ halfadder
dip halfadder or ] is fulladder ( a b c --> s c )
 
[ 4 pack reverse unpack
4 times [ temp put ]
4 pack reverse
0 swap witheach
[ temp take fulladder ]
5 pack reverse unpack ] is 4bitadder ( b3 b2 b1 b0 a3 a2 a1 a0 --> c s3 s2 s1 s0 )
 
say "1100 + 1100 = "
1 1 0 0 1 1 0 0 4bitadder
5 pack witheach echo
cr
say "1100 + 1101 = "
1 1 0 0 1 1 0 1 4bitadder
5 pack witheach echo
cr
say "1100 + 1110 = "
1 1 0 0 1 1 1 0 4bitadder
5 pack witheach echo
cr
say "1100 + 1111 = "
1 1 0 0 1 1 1 1 4bitadder
5 pack witheach echo
cr
say "1101 + 0000 = "
1 1 0 1 0 0 0 0 4bitadder
5 pack witheach echo
cr
say "1101 + 0001 = "
1 1 0 1 0 0 0 1 4bitadder
5 pack witheach echo
cr
say "1101 + 0010 = "
1 1 0 1 0 0 1 0 4bitadder
5 pack witheach echo
cr
say "1101 + 0011 = "
1 1 0 1 0 0 1 1 4bitadder
5 pack witheach echo
cr</syntaxhighlight>
 
{{out}}
 
<pre>1100 + 1100 = 11000
1100 + 1101 = 11001
1100 + 1110 = 11010
1100 + 1111 = 11011
1101 + 0000 = 01101
1101 + 0001 = 01110
1101 + 0010 = 01111
1101 + 0011 = 10000</pre>
 
=={{header|Racket}}==
Line 6,984 ⟶ 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,479

edits