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.)
 
(19 intermediate revisions by 8 users not shown)
Line 43:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F xor(a, b)
R (a & !b) | (b & !a)
 
Line 77:
tot[i] = ta[i]
tot[width] = tlast
assert(a + b == bus2int(tot), ‘totals don't match: #. + #. != #.’.format(a, b, String(tot)))</langsyntaxhighlight>
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE Bit="BYTE"
 
TYPE FourBit=[Bit b0,b1,b2,b3]
Line 141:
PrintBE(c)
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Four_bit_adder.png Screenshot from Atari 8-bit computer]
Line 168:
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
<lang Ada>
type Four_Bits is array (1..4) of Boolean;
Line 192:
Full_Adder (A (1), B (1), C (1), Carry);
end Four_Bits_Adder;
</syntaxhighlight>
</lang>
A test program with the above definitions
<syntaxhighlight lang="ada">
<lang Ada>
with Ada.Text_IO; use Ada.Text_IO;
 
Line 247:
end loop;
end Test_4_Bit_Adder;
</syntaxhighlight>
</lang>
{{out}}
<div style="height: 320px;overflow:scroll">
Line 513:
{{works with|Dyalog APL}}
{{works with|GNU APL}}
<langsyntaxhighlight lang="apl">⍝ Our primitive "gates" are built-in, but let's give them names
not ← { ~ ⍵ } ⍝ in Dyalog these assignments can be simplified to "not ← ~", "and ← ∧", etc.
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 545:
 
⍝ Add one pair of numbers and print as equation
⍝ A demo function
demo ← { ⎕←⍺0⍴⎕←⍺,'+',⍵,'=',{ 1↓⍵,' with carry ',1↑⍵ } ⍺ adder4 ⍵ }
 
⍝ A way to generate some random numbers for our demo
Line 553:
⍝ And go
{ (randbits 4) demo randbits 4 ⊣ ⍵ } ¨ ⍳20
</syntaxhighlight>
</lang>
 
{{Out}}
Line 577:
1 0 1 1 + 1 1 1 1 = 1 0 1 0 with carry 1
1 1 0 1 + 1 0 0 1 = 0 1 1 0 with carry 1</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">binStringToBits: function [x][
result: map reverse x 'i -> to :integer to :string i
result: result ++ repeat 0 4-size result
return result
]
 
bitsToBinString: function [x][
join reverse map x 'i -> to :string i
]
 
fullAdder: function [a,b,c0][
[s,c]: halfAdder c0 a
[s,c1]: halfAdder s b
return @[s, or c c1]
]
 
halfAdder: function [a,b][
return @[xor a b, and a b]
]
 
fourBitAdder: function [a,b][
aBits: binStringToBits a
bBits: binStringToBits b
 
[s0,c0]: fullAdder aBits\0 bBits\0 0
[s1,c1]: fullAdder aBits\1 bBits\1 c0
[s2,c2]: fullAdder aBits\2 bBits\2 c1
[s3,c3]: fullAdder aBits\3 bBits\3 c2
 
return @[
bitsToBinString @[s0,s1,s2,s3]
to :string c3
]
]
 
loop 0..15 'a [
loop 0..15 'b [
binA: (as.binary a) ++ join to [:string] repeat 0 4-size as.binary a
binB: (as.binary b) ++ join to [:string] repeat 0 4-size as.binary b
[sm,carry]: fourBitAdder binA binB
print [pad to :string a 2 "+" pad to :string b 2 "=" binA "+" binB "=" "("++carry++")" sm "=" from.binary carry ++ sm]
]
]</syntaxhighlight>
 
{{out}}
 
<pre> 0 + 0 = 0000 + 0000 = (0) 0000 = 0
0 + 1 = 0000 + 1000 = (0) 1000 = 8
0 + 2 = 0000 + 1000 = (0) 1000 = 8
0 + 3 = 0000 + 1100 = (0) 1100 = 12
0 + 4 = 0000 + 1000 = (0) 1000 = 8
0 + 5 = 0000 + 1010 = (0) 1010 = 10
0 + 6 = 0000 + 1100 = (0) 1100 = 12
0 + 7 = 0000 + 1110 = (0) 1110 = 14
0 + 8 = 0000 + 1000 = (0) 1000 = 8
0 + 9 = 0000 + 1001 = (0) 1001 = 9
0 + 10 = 0000 + 1010 = (0) 1010 = 10
0 + 11 = 0000 + 1011 = (0) 1011 = 11
0 + 12 = 0000 + 1100 = (0) 1100 = 12
0 + 13 = 0000 + 1101 = (0) 1101 = 13
0 + 14 = 0000 + 1110 = (0) 1110 = 14
0 + 15 = 0000 + 1111 = (0) 1111 = 15
1 + 0 = 1000 + 0000 = (0) 1000 = 8
1 + 1 = 1000 + 1000 = (1) 0000 = 16
1 + 2 = 1000 + 1000 = (1) 0000 = 16
1 + 3 = 1000 + 1100 = (1) 0100 = 20
1 + 4 = 1000 + 1000 = (1) 0000 = 16
1 + 5 = 1000 + 1010 = (1) 0010 = 18
1 + 6 = 1000 + 1100 = (1) 0100 = 20
1 + 7 = 1000 + 1110 = (1) 0110 = 22
1 + 8 = 1000 + 1000 = (1) 0000 = 16
1 + 9 = 1000 + 1001 = (1) 0001 = 17
1 + 10 = 1000 + 1010 = (1) 0010 = 18
1 + 11 = 1000 + 1011 = (1) 0011 = 19
1 + 12 = 1000 + 1100 = (1) 0100 = 20
1 + 13 = 1000 + 1101 = (1) 0101 = 21
1 + 14 = 1000 + 1110 = (1) 0110 = 22
1 + 15 = 1000 + 1111 = (1) 0111 = 23
2 + 0 = 1000 + 0000 = (0) 1000 = 8
2 + 1 = 1000 + 1000 = (1) 0000 = 16
2 + 2 = 1000 + 1000 = (1) 0000 = 16
2 + 3 = 1000 + 1100 = (1) 0100 = 20
2 + 4 = 1000 + 1000 = (1) 0000 = 16
2 + 5 = 1000 + 1010 = (1) 0010 = 18
2 + 6 = 1000 + 1100 = (1) 0100 = 20
2 + 7 = 1000 + 1110 = (1) 0110 = 22
2 + 8 = 1000 + 1000 = (1) 0000 = 16
2 + 9 = 1000 + 1001 = (1) 0001 = 17
2 + 10 = 1000 + 1010 = (1) 0010 = 18
2 + 11 = 1000 + 1011 = (1) 0011 = 19
2 + 12 = 1000 + 1100 = (1) 0100 = 20
2 + 13 = 1000 + 1101 = (1) 0101 = 21
2 + 14 = 1000 + 1110 = (1) 0110 = 22
2 + 15 = 1000 + 1111 = (1) 0111 = 23
3 + 0 = 1100 + 0000 = (0) 1100 = 12
3 + 1 = 1100 + 1000 = (1) 0100 = 20
3 + 2 = 1100 + 1000 = (1) 0100 = 20
3 + 3 = 1100 + 1100 = (1) 1000 = 24
3 + 4 = 1100 + 1000 = (1) 0100 = 20
3 + 5 = 1100 + 1010 = (1) 0110 = 22
3 + 6 = 1100 + 1100 = (1) 1000 = 24
3 + 7 = 1100 + 1110 = (1) 1010 = 26
3 + 8 = 1100 + 1000 = (1) 0100 = 20
3 + 9 = 1100 + 1001 = (1) 0101 = 21
3 + 10 = 1100 + 1010 = (1) 0110 = 22
3 + 11 = 1100 + 1011 = (1) 0111 = 23
3 + 12 = 1100 + 1100 = (1) 1000 = 24
3 + 13 = 1100 + 1101 = (1) 1001 = 25
3 + 14 = 1100 + 1110 = (1) 1010 = 26
3 + 15 = 1100 + 1111 = (1) 1011 = 27
4 + 0 = 1000 + 0000 = (0) 1000 = 8
4 + 1 = 1000 + 1000 = (1) 0000 = 16
4 + 2 = 1000 + 1000 = (1) 0000 = 16
4 + 3 = 1000 + 1100 = (1) 0100 = 20
4 + 4 = 1000 + 1000 = (1) 0000 = 16
4 + 5 = 1000 + 1010 = (1) 0010 = 18
4 + 6 = 1000 + 1100 = (1) 0100 = 20
4 + 7 = 1000 + 1110 = (1) 0110 = 22
4 + 8 = 1000 + 1000 = (1) 0000 = 16
4 + 9 = 1000 + 1001 = (1) 0001 = 17
4 + 10 = 1000 + 1010 = (1) 0010 = 18
4 + 11 = 1000 + 1011 = (1) 0011 = 19
4 + 12 = 1000 + 1100 = (1) 0100 = 20
4 + 13 = 1000 + 1101 = (1) 0101 = 21
4 + 14 = 1000 + 1110 = (1) 0110 = 22
4 + 15 = 1000 + 1111 = (1) 0111 = 23
5 + 0 = 1010 + 0000 = (0) 1010 = 10
5 + 1 = 1010 + 1000 = (1) 0010 = 18
5 + 2 = 1010 + 1000 = (1) 0010 = 18
5 + 3 = 1010 + 1100 = (1) 0110 = 22
5 + 4 = 1010 + 1000 = (1) 0010 = 18
5 + 5 = 1010 + 1010 = (1) 0100 = 20
5 + 6 = 1010 + 1100 = (1) 0110 = 22
5 + 7 = 1010 + 1110 = (1) 1000 = 24
5 + 8 = 1010 + 1000 = (1) 0010 = 18
5 + 9 = 1010 + 1001 = (1) 0011 = 19
5 + 10 = 1010 + 1010 = (1) 0100 = 20
5 + 11 = 1010 + 1011 = (1) 0101 = 21
5 + 12 = 1010 + 1100 = (1) 0110 = 22
5 + 13 = 1010 + 1101 = (1) 0111 = 23
5 + 14 = 1010 + 1110 = (1) 1000 = 24
5 + 15 = 1010 + 1111 = (1) 1001 = 25
6 + 0 = 1100 + 0000 = (0) 1100 = 12
6 + 1 = 1100 + 1000 = (1) 0100 = 20
6 + 2 = 1100 + 1000 = (1) 0100 = 20
6 + 3 = 1100 + 1100 = (1) 1000 = 24
6 + 4 = 1100 + 1000 = (1) 0100 = 20
6 + 5 = 1100 + 1010 = (1) 0110 = 22
6 + 6 = 1100 + 1100 = (1) 1000 = 24
6 + 7 = 1100 + 1110 = (1) 1010 = 26
6 + 8 = 1100 + 1000 = (1) 0100 = 20
6 + 9 = 1100 + 1001 = (1) 0101 = 21
6 + 10 = 1100 + 1010 = (1) 0110 = 22
6 + 11 = 1100 + 1011 = (1) 0111 = 23
6 + 12 = 1100 + 1100 = (1) 1000 = 24
6 + 13 = 1100 + 1101 = (1) 1001 = 25
6 + 14 = 1100 + 1110 = (1) 1010 = 26
6 + 15 = 1100 + 1111 = (1) 1011 = 27
7 + 0 = 1110 + 0000 = (0) 1110 = 14
7 + 1 = 1110 + 1000 = (1) 0110 = 22
7 + 2 = 1110 + 1000 = (1) 0110 = 22
7 + 3 = 1110 + 1100 = (1) 1010 = 26
7 + 4 = 1110 + 1000 = (1) 0110 = 22
7 + 5 = 1110 + 1010 = (1) 1000 = 24
7 + 6 = 1110 + 1100 = (1) 1010 = 26
7 + 7 = 1110 + 1110 = (1) 1100 = 28
7 + 8 = 1110 + 1000 = (1) 0110 = 22
7 + 9 = 1110 + 1001 = (1) 0111 = 23
7 + 10 = 1110 + 1010 = (1) 1000 = 24
7 + 11 = 1110 + 1011 = (1) 1001 = 25
7 + 12 = 1110 + 1100 = (1) 1010 = 26
7 + 13 = 1110 + 1101 = (1) 1011 = 27
7 + 14 = 1110 + 1110 = (1) 1100 = 28
7 + 15 = 1110 + 1111 = (1) 1101 = 29
8 + 0 = 1000 + 0000 = (0) 1000 = 8
8 + 1 = 1000 + 1000 = (1) 0000 = 16
8 + 2 = 1000 + 1000 = (1) 0000 = 16
8 + 3 = 1000 + 1100 = (1) 0100 = 20
8 + 4 = 1000 + 1000 = (1) 0000 = 16
8 + 5 = 1000 + 1010 = (1) 0010 = 18
8 + 6 = 1000 + 1100 = (1) 0100 = 20
8 + 7 = 1000 + 1110 = (1) 0110 = 22
8 + 8 = 1000 + 1000 = (1) 0000 = 16
8 + 9 = 1000 + 1001 = (1) 0001 = 17
8 + 10 = 1000 + 1010 = (1) 0010 = 18
8 + 11 = 1000 + 1011 = (1) 0011 = 19
8 + 12 = 1000 + 1100 = (1) 0100 = 20
8 + 13 = 1000 + 1101 = (1) 0101 = 21
8 + 14 = 1000 + 1110 = (1) 0110 = 22
8 + 15 = 1000 + 1111 = (1) 0111 = 23
9 + 0 = 1001 + 0000 = (0) 1001 = 9
9 + 1 = 1001 + 1000 = (1) 0001 = 17
9 + 2 = 1001 + 1000 = (1) 0001 = 17
9 + 3 = 1001 + 1100 = (1) 0101 = 21
9 + 4 = 1001 + 1000 = (1) 0001 = 17
9 + 5 = 1001 + 1010 = (1) 0011 = 19
9 + 6 = 1001 + 1100 = (1) 0101 = 21
9 + 7 = 1001 + 1110 = (1) 0111 = 23
9 + 8 = 1001 + 1000 = (1) 0001 = 17
9 + 9 = 1001 + 1001 = (1) 0010 = 18
9 + 10 = 1001 + 1010 = (1) 0011 = 19
9 + 11 = 1001 + 1011 = (1) 0100 = 20
9 + 12 = 1001 + 1100 = (1) 0101 = 21
9 + 13 = 1001 + 1101 = (1) 0110 = 22
9 + 14 = 1001 + 1110 = (1) 0111 = 23
9 + 15 = 1001 + 1111 = (1) 1000 = 24
10 + 0 = 1010 + 0000 = (0) 1010 = 10
10 + 1 = 1010 + 1000 = (1) 0010 = 18
10 + 2 = 1010 + 1000 = (1) 0010 = 18
10 + 3 = 1010 + 1100 = (1) 0110 = 22
10 + 4 = 1010 + 1000 = (1) 0010 = 18
10 + 5 = 1010 + 1010 = (1) 0100 = 20
10 + 6 = 1010 + 1100 = (1) 0110 = 22
10 + 7 = 1010 + 1110 = (1) 1000 = 24
10 + 8 = 1010 + 1000 = (1) 0010 = 18
10 + 9 = 1010 + 1001 = (1) 0011 = 19
10 + 10 = 1010 + 1010 = (1) 0100 = 20
10 + 11 = 1010 + 1011 = (1) 0101 = 21
10 + 12 = 1010 + 1100 = (1) 0110 = 22
10 + 13 = 1010 + 1101 = (1) 0111 = 23
10 + 14 = 1010 + 1110 = (1) 1000 = 24
10 + 15 = 1010 + 1111 = (1) 1001 = 25
11 + 0 = 1011 + 0000 = (0) 1011 = 11
11 + 1 = 1011 + 1000 = (1) 0011 = 19
11 + 2 = 1011 + 1000 = (1) 0011 = 19
11 + 3 = 1011 + 1100 = (1) 0111 = 23
11 + 4 = 1011 + 1000 = (1) 0011 = 19
11 + 5 = 1011 + 1010 = (1) 0101 = 21
11 + 6 = 1011 + 1100 = (1) 0111 = 23
11 + 7 = 1011 + 1110 = (1) 1001 = 25
11 + 8 = 1011 + 1000 = (1) 0011 = 19
11 + 9 = 1011 + 1001 = (1) 0100 = 20
11 + 10 = 1011 + 1010 = (1) 0101 = 21
11 + 11 = 1011 + 1011 = (1) 0110 = 22
11 + 12 = 1011 + 1100 = (1) 0111 = 23
11 + 13 = 1011 + 1101 = (1) 1000 = 24
11 + 14 = 1011 + 1110 = (1) 1001 = 25
11 + 15 = 1011 + 1111 = (1) 1010 = 26
12 + 0 = 1100 + 0000 = (0) 1100 = 12
12 + 1 = 1100 + 1000 = (1) 0100 = 20
12 + 2 = 1100 + 1000 = (1) 0100 = 20
12 + 3 = 1100 + 1100 = (1) 1000 = 24
12 + 4 = 1100 + 1000 = (1) 0100 = 20
12 + 5 = 1100 + 1010 = (1) 0110 = 22
12 + 6 = 1100 + 1100 = (1) 1000 = 24
12 + 7 = 1100 + 1110 = (1) 1010 = 26
12 + 8 = 1100 + 1000 = (1) 0100 = 20
12 + 9 = 1100 + 1001 = (1) 0101 = 21
12 + 10 = 1100 + 1010 = (1) 0110 = 22
12 + 11 = 1100 + 1011 = (1) 0111 = 23
12 + 12 = 1100 + 1100 = (1) 1000 = 24
12 + 13 = 1100 + 1101 = (1) 1001 = 25
12 + 14 = 1100 + 1110 = (1) 1010 = 26
12 + 15 = 1100 + 1111 = (1) 1011 = 27
13 + 0 = 1101 + 0000 = (0) 1101 = 13
13 + 1 = 1101 + 1000 = (1) 0101 = 21
13 + 2 = 1101 + 1000 = (1) 0101 = 21
13 + 3 = 1101 + 1100 = (1) 1001 = 25
13 + 4 = 1101 + 1000 = (1) 0101 = 21
13 + 5 = 1101 + 1010 = (1) 0111 = 23
13 + 6 = 1101 + 1100 = (1) 1001 = 25
13 + 7 = 1101 + 1110 = (1) 1011 = 27
13 + 8 = 1101 + 1000 = (1) 0101 = 21
13 + 9 = 1101 + 1001 = (1) 0110 = 22
13 + 10 = 1101 + 1010 = (1) 0111 = 23
13 + 11 = 1101 + 1011 = (1) 1000 = 24
13 + 12 = 1101 + 1100 = (1) 1001 = 25
13 + 13 = 1101 + 1101 = (1) 1010 = 26
13 + 14 = 1101 + 1110 = (1) 1011 = 27
13 + 15 = 1101 + 1111 = (1) 1100 = 28
14 + 0 = 1110 + 0000 = (0) 1110 = 14
14 + 1 = 1110 + 1000 = (1) 0110 = 22
14 + 2 = 1110 + 1000 = (1) 0110 = 22
14 + 3 = 1110 + 1100 = (1) 1010 = 26
14 + 4 = 1110 + 1000 = (1) 0110 = 22
14 + 5 = 1110 + 1010 = (1) 1000 = 24
14 + 6 = 1110 + 1100 = (1) 1010 = 26
14 + 7 = 1110 + 1110 = (1) 1100 = 28
14 + 8 = 1110 + 1000 = (1) 0110 = 22
14 + 9 = 1110 + 1001 = (1) 0111 = 23
14 + 10 = 1110 + 1010 = (1) 1000 = 24
14 + 11 = 1110 + 1011 = (1) 1001 = 25
14 + 12 = 1110 + 1100 = (1) 1010 = 26
14 + 13 = 1110 + 1101 = (1) 1011 = 27
14 + 14 = 1110 + 1110 = (1) 1100 = 28
14 + 15 = 1110 + 1111 = (1) 1101 = 29
15 + 0 = 1111 + 0000 = (0) 1111 = 15
15 + 1 = 1111 + 1000 = (1) 0111 = 23
15 + 2 = 1111 + 1000 = (1) 0111 = 23
15 + 3 = 1111 + 1100 = (1) 1011 = 27
15 + 4 = 1111 + 1000 = (1) 0111 = 23
15 + 5 = 1111 + 1010 = (1) 1001 = 25
15 + 6 = 1111 + 1100 = (1) 1011 = 27
15 + 7 = 1111 + 1110 = (1) 1101 = 29
15 + 8 = 1111 + 1000 = (1) 0111 = 23
15 + 9 = 1111 + 1001 = (1) 1000 = 24
15 + 10 = 1111 + 1010 = (1) 1001 = 25
15 + 11 = 1111 + 1011 = (1) 1010 = 26
15 + 12 = 1111 + 1100 = (1) 1011 = 27
15 + 13 = 1111 + 1101 = (1) 1100 = 28
15 + 14 = 1111 + 1110 = (1) 1101 = 29
15 + 15 = 1111 + 1111 = (1) 1110 = 30</pre>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">A := 13
B := 9
N := FourBitAdd(A, B)
Line 621 ⟶ 925:
Res := Mod(N, 2) Res, N := N >> 1
return, Res
}</langsyntaxhighlight>
{{out}}
<pre>13 + 9:
Line 628 ⟶ 932:
=={{header|AutoIt}}==
===Functions===
<syntaxhighlight lang="autoit">
<lang AutoIt>
Func _NOT($_A)
Return (Not $_A) *1
Line 669 ⟶ 973:
Return $Q4 & $Q3 & $Q2 & $Q1
EndFunc ;==>_4BitAdder
</syntaxhighlight>
</lang>
===Example===
<syntaxhighlight lang="autoit">
<lang AutoIt>
Local $CarryOut, $sResult
$sResult = _4BitAdder(0, 0, 1, 1, 0, 1, 1, 1, 0, $CarryOut) ; adds 3 + 7
Line 678 ⟶ 982:
$sResult = _4BitAdder(1, 0, 1, 1, 1, 0, 0, 0, 0, $CarryOut) ; adds 11 + 8
ConsoleWrite('result: ' & $sResult & ' ==> carry out: ' & $CarryOut & @LF)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 690 ⟶ 994:
==={{header|Applesoft BASIC}}===
 
<langsyntaxhighlight lang="basic">100 S$ = "1100 + 1100 = " : GOSUB 400
110 S$ = "1100 + 1101 = " : GOSUB 400
120 S$ = "1100 + 1110 = " : GOSUB 400
Line 749 ⟶ 1,053:
910 D = B AND NOT A
920 C = C OR D
930 RETURN</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> @% = 2
PRINT "1100 + 1100 = ";
PROC4bitadd(1,1,0,0, 1,1,0,0, e,d,c,b,a) : PRINT e,d,c,b,a
Line 797 ⟶ 1,101:
c& = a& AND NOT b&
d& = b& AND NOT a&
= c& OR d&</langsyntaxhighlight>
{{out}}
<pre>
Line 811 ⟶ 1,115:
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">
@echo off
setlocal enabledelayedexpansion
Line 948 ⟶ 1,252:
if %1==1 if %2==1 exit /b 1
exit /b 0
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 957 ⟶ 1,261:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
typedef char pin_t;
Line 1,026 ⟶ 1,330:
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
 
{{works with|C sharp|C#|3+}}
<langsyntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
Line 1,160 ⟶ 1,464:
}
 
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
Line 1,166 ⟶ 1,470:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">
(ns rosettacode.adder
(:use clojure.test))
Line 1,201 ⟶ 1,505:
(n-bit-adder [true true true true true true] [true true true true true true])
=> (false true true true true true true)
</syntaxhighlight>
</lang>
 
===Second Clojure solution===
<langsyntaxhighlight lang="clojure">(ns rosetta.fourbit)
 
;; a bit is represented as a boolean (true/false)
Line 1,256 ⟶ 1,560:
)
 
</syntaxhighlight>
</lang>
 
===Using Bitwise Operators===
<langsyntaxhighlight lang="clojure">
(defn to-binary-seq [^long x]
(map #(- (int %) (int \0))
Line 1,293 ⟶ 1,597:
(is (= (Long/parseLong (apply str (ripple-carry-adder (to-binary-seq 130) (to-binary-seq 250))) 2)
(+ 130 250))))
</syntaxhighlight>
</lang>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
<lang COBOL>
program-id. test-add.
environment division.
Line 1,405 ⟶ 1,709:
end program add-4b.
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,420 ⟶ 1,724:
This code models gates as functions. The connection of gates is done via custom logic, which doesn't involve any cheating, but a really good solution would be more constructive, i.e. it would show more of a notion of "connecting" up gates, using some kind of graph data structure.
 
<langsyntaxhighlight lang="coffeescript">
# ATOMIC GATES
not_gate = (bit) ->
Line 1,460 ⟶ 1,764:
adder = n_bit_adder(4)
console.log adder [1, 0, 1, 0], [0, 1, 1, 0]
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">;; returns a list of bits: '(sum carry)
(defun half-adder (a b)
(list (logxor a b) (logand a b)))
Line 1,495 ⟶ 1,799:
 
(main)
</syntaxhighlight>
</lang>
output:
<pre>
Line 1,508 ⟶ 1,812:
=={{header|D}}==
From the C version. An example of SWAR (SIMD Within A Register) code, that performs 32 (or 64) 4-bit adds in parallel.
<langsyntaxhighlight lang="d">import std.stdio, std.traits;
 
void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3,
Line 1,574 ⟶ 1,878:
writefln(" s0 %032b", s0);
writefln("overflow %032b", overflow);
}</langsyntaxhighlight>
{{out}}
<pre> a3 00000000000000000000000000000000
Line 1,592 ⟶ 1,896:
overflow 11111111111111111111111111111111</pre>
128 4-bit adds in parallel:
<langsyntaxhighlight lang="d">import std.stdio, std.traits, core.simd;
 
void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3,
Line 1,657 ⟶ 1,961:
writefln(" s0 %(%08b%)", s0.array);
writefln("overflow %(%08b%)", overflow.array);
}</langsyntaxhighlight>
{{out}}
<pre> a3 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Line 1,675 ⟶ 1,979:
overflow 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</pre>
Compiled by the ldc2 compiler to (where T = ubyte32, 256 adds using AVX2):
<langsyntaxhighlight lang="asm">fourBitsAdder:
pushl %ebp
movl %esp, %ebp
Line 1,713 ⟶ 2,017:
popl %ebp
vzeroupper
ret $160</langsyntaxhighlight>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Four_bit_adder;
 
Line 1,896 ⟶ 2,200:
Readln;
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,156 ⟶ 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}}
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule RC do
use Bitwise
@bit_size 4
Line 2,205 ⟶ 2,540:
end
 
RC.task</langsyntaxhighlight>
 
{{out}}
Line 2,231 ⟶ 2,566:
=={{header|Erlang}}==
Yes, it is misleading to have a "choose your own number of bits" adder in the four_bit_adder module. But it does make it easier to find the module from the Rosettacode task name.
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( four_bit_adder ).
 
Line 2,302 ⟶ 2,637:
%% xor exists, this is another implementation.
z_xor( A, B ) -> (A band (2+bnot B)) bor ((2+bnot A) band B).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,310 ⟶ 2,645:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
type Bit =
| Zero
Line 2,347 ⟶ 2,682:
printfn "0001 + 0111 ="
b4A (Zero, Zero, Zero, One) (Zero, One, One, One) |> printfn "%A"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,355 ⟶ 2,690:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: "NOT" invert 1 and ;
: "XOR" over over "NOT" and >r swap "NOT" and r> or ;
: halfadder over over and >r "XOR" r> ;
Line 2,366 ⟶ 2,701:
;
 
: .add4 4bitadder 0 .r 4 0 do i 3 - abs roll 0 .r loop cr ;</langsyntaxhighlight>
{{out}}
<pre>1 1 0 0 0 0 1 1 .add4 01111
Line 2,373 ⟶ 2,708:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">module logic
implicit none
 
Line 2,449 ⟶ 2,784:
end do
end do
end program</langsyntaxhighlight>
{{out}} (selected)
<pre>1100 + 1100 = 11000
Line 2,462 ⟶ 2,797:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Four_bit_adder}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
 
[[File:Fōrmulæ - Four bit adder 01.png]]
 
[[File:Fōrmulæ - Four bit adder 02.png]]
 
[[File:Fōrmulæ - Four bit adder 03.png]]
 
[[File:Fōrmulæ - Four bit adder 04.png]]
 
'''Testing with all the (256) possible combinations:'''
 
[[File:Fōrmulæ - Four bit adder 05.png]]
 
[[File:Fōrmulæ - Four bit adder 06.png]]
 
<span>&nbsp;&nbsp;:</span>
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Four bit adder 07.png]]
In '''[https://formulae.org/?example=Four_bit_adder this]''' page you can see the program(s) related to this task and their results.
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">sub half_add( byval a as ubyte, byval b as ubyte,_
byref s as ubyte, byref c as ubyte)
s = a xor b
Line 2,506 ⟶ 2,857:
print "1111 + 1111 = ";
fourbit_add( 1, 1, 1, 1, 1, 1, 1, 1, s3, s2, s1, s0, carry )
print carry;s3;s2;s1;s0</langsyntaxhighlight>
{{out}}<pre>
1100 + 0011 = 01111
Line 2,517 ⟶ 2,868:
Go does not have a bit type, so byte is used.
This is the straightforward solution using bytes and functions.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 2,547 ⟶ 2,898:
// add 10+9 result should be 1 0 0 1 1
fmt.Println(add4(1, 0, 1, 0, 1, 0, 0, 1))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,555 ⟶ 2,906:
===Channels===
Alternative solution is a little more like a simulation.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 2,680 ⟶ 3,031:
B[<-r4], B[<-r3], B[<-r2], B[<-r1], B[<-carry])
}
</syntaxhighlight>
</lang>
 
Mini reference:
Line 2,689 ⟶ 3,040:
 
=={{header|Groovy}}==
<langsyntaxhighlight Groovylang="groovy">class Main {
static void main(args) {
 
Line 2,915 ⟶ 3,266:
adder4.setBit2 input
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
Basic gates:
<langsyntaxhighlight lang="haskell">import Control.Arrow
import Data.List (mapAccumR)
 
Line 2,926 ⟶ 3,277:
band = min
bnot :: Int -> Int
bnot = (1-)</langsyntaxhighlight>
Gates built with basic ones:
<langsyntaxhighlight lang="haskell">nand, xor :: Int -> Int -> Int
nand = (bnot.).band
xor a b = uncurry nand. (nand a &&& nand b) $ nand a b</langsyntaxhighlight>
Adder circuits:
<langsyntaxhighlight lang="haskell">halfAdder = uncurry band &&& uncurry xor
fullAdder (c, a, b) = (\(cy,s) -> first (bor cy) $ halfAdder (b, s)) $ halfAdder (c, a)
 
adder4 as = mapAccumR (\cy (f,a,b) -> f (cy,a,b)) 0 . zip3 (replicate 4 fullAdder) as</langsyntaxhighlight>
 
Example using adder4
<langsyntaxhighlight lang="haskell">*Main> adder4 [1,0,1,0] [1,1,1,1]
(1,[1,0,0,1])</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 2,945 ⟶ 3,296:
Based on the algorithms shown in the Fortran entry, but Unicon does not allow pass by reference for immutable types, so a small <code>carry</code> record is used instead.
 
<langsyntaxhighlight lang="unicon">#
# 4bitadder.icn, emulate a 4 bit adder. Using only and, or, not
#
Line 3,047 ⟶ 3,398:
# cr.c is the overflow carry
return s
end</langsyntaxhighlight>
 
{{out}}
Line 3,075 ⟶ 3,426:
 
===Implementation===
<langsyntaxhighlight lang="j">and=: *.
or=: +.
not=: -.
xor=: (and not) or (and not)~
hadd=: and ,"0 xor
add=: ((({.,0:)@[ or {:@[ hadd {.@]), }.@])/@hadd</langsyntaxhighlight>
 
===Example use===
<langsyntaxhighlight lang="j"> 1 1 1 1 add 0 1 1 1
1 0 1 1 0</langsyntaxhighlight>
 
To produce all results:
<langsyntaxhighlight lang="j"> add"1/~#:i.16</langsyntaxhighlight>
 
This will produce a 16 by 16 by 5 array, the first axis being the left argument (representing values 0..15), the second axis the right argument and the final axis being the bit indices (carry, 8, 4, 2, 1). In other words, the result is something like:
 
<langsyntaxhighlight lang="j"> ,"2 ' ',"1 -.&' '@":"1 add"1/~#:i.16
00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 01010 01011 01100 01101 01110 01111
00001 00010 00011 00100 00101 00110 00111 01000 01001 01010 01011 01100 01101 01110 01111 10000
Line 3,107 ⟶ 3,458:
01101 01110 01111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100
01110 01111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101
01111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110</langsyntaxhighlight>
 
Alternatively, the fact that add was designed to operate on lists of bits could have been incorporated into its definition:
 
<langsyntaxhighlight lang="j">add=: ((({.,0:)@[ or {:@[ hadd {.@]), }.@])/@hadd"1</langsyntaxhighlight>
 
Then to get all results you could use:
<langsyntaxhighlight lang="j"> add/~#:i.16</langsyntaxhighlight>
 
Compare this to a regular addition table:
<syntaxhighlight lang ="j"> +/~i.10</langsyntaxhighlight>
(this produces a 10 by 10 array -- the results have no further internal array structure, though of course in the machine implementation integers can be thought of as being represented as fixed width lists of bits.)
 
Line 3,157 ⟶ 3,508:
=={{header|Java}}==
 
<langsyntaxhighlight lang="java">public class GateLogic
{
// Basic gate interfaces
Line 3,303 ⟶ 3,654:
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,320 ⟶ 3,671:
0s and 1s. To enforce this, we'll first create a couple of helper functions.
 
<syntaxhighlight lang="javascript">
<lang JavaScript>
function acceptedBinFormat(bin) {
if (bin == 1 || bin === 0 || bin === '0')
Line 3,335 ⟶ 3,686:
return true;
}
</syntaxhighlight>
</lang>
 
===Implementation===
Line 3,342 ⟶ 3,693:
and, finally, the four bit adder.
 
<syntaxhighlight lang="javascript">
<lang JavaScript>
// basic building blocks allowed by the rules are ~, &, and |, we'll fake these
// in a way that makes what they do (at least when you use them) more obvious
Line 3,415 ⟶ 3,766:
return out.join('');
}
</syntaxhighlight>
</lang>
===Example Use===
<langsyntaxhighlight JavaScriptlang="javascript">fourBitAdder('1010', '0101'); // 1111 (15)</langsyntaxhighlight>
 
all results:
 
<syntaxhighlight lang="javascript">
<lang JavaScript>
// run this in your browsers console
var outer = inner = 16, a, b;
Line 3,433 ⟶ 3,784:
inner = outer;
}
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
Line 3,440 ⟶ 3,791:
All the operations except fourBitAdder(a,b) assume the inputs are presented as 0 or 1 (i.e. integers).
 
<langsyntaxhighlight lang="jq"># Start with the 'not' and 'and' building blocks.
# These allow us to construct 'nand', 'or', and 'xor',
# and so on.
Line 3,481 ⟶ 3,832:
| fullAdder($inA[0]; $inB[0]; $pass.carry) as $pass
| .[0] = $pass.sum
| map(tostring) | join("") ;</langsyntaxhighlight>
'''Example:'''
<langsyntaxhighlight lang="jq">fourBitAdder("0111"; "0001")</langsyntaxhighlight>
{{out}}
$ jq -n -f Four_bit_adder.jq
Line 3,490 ⟶ 3,841:
=={{header|Jsish}}==
Based on Javascript entry.
<langsyntaxhighlight lang="javascript">#!/usr/bin/env jsish
/* 4 bit adder simulation, in Jsish */
function not(a) { return a == 1 ? 0 : 1; }
Line 3,570 ⟶ 3,921:
PASS!: err = bad bit at a[3] of "2"
=!EXPECTEND!=
*/</langsyntaxhighlight>
{{out}}
<pre>prompt$ jsish --U fourBitAdder.jsi
Line 3,594 ⟶ 3,945:
 
'''Functions'''
<langsyntaxhighlight Julialang="julia">using Printf
 
xor{T<:Bool}(a::T, b::T) = (a&~b)|(~a&b)
Line 3,624 ⟶ 3,975:
 
Base.bits(n::BitArray{1}) = join(reverse(int(n)), "")
</syntaxhighlight>
</lang>
 
'''Main'''
<syntaxhighlight lang="julia">
<lang Julia>
xavail = trues(15,15)
xcnt = 0
Line 3,646 ⟶ 3,997:
bits(s), oflow))
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,664 ⟶ 4,015:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
val Boolean.I get() = if (this) 1 else 0
Line 3,715 ⟶ 4,066:
for (j in i..minOf(i + 1, 15)) test(i, j)
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,771 ⟶ 4,122:
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
<lang Scheme>
{def xor
{lambda {:a :b}
Line 3,862 ⟶ 4,213:
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">-- Build XOR from AND, OR and NOT
function xor (a, b) return (a and not b) or (b and not a) end
Line 3,921 ⟶ 4,272:
print(add(0101, 1010)) -- 5 + 10 = 15
print(add(0000, 1111)) -- 0 + 15 = 15
print(add(0001, 1111)) -- 1 + 15 = 16 (causes overflow)</langsyntaxhighlight>
Output:
<pre>SUM OVERFLOW
Line 3,931 ⟶ 4,282:
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module FourBitAdder {
Flush
Line 3,991 ⟶ 4,342:
}
FourBitAdder
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="text">and[a_, b_] := Max[a, b];
or[a_, b_] := Min[a, b];
not[a_] := 1 - a;
Line 4,009 ⟶ 4,360:
{s2, c2} = fulladder[a2, b2, c1];
{s3, c3} = fulladder[a3, b3, c2];
{{s3, s2, s1, s0}, c3}];</langsyntaxhighlight>
Example:
<syntaxhighlight lang="text">fourbitadder[{1, 0, 1, 0}, {1, 1, 1, 1}]</langsyntaxhighlight>
Output:
<pre>{{1, 0, 0, 1}, 1}</pre>
Line 4,018 ⟶ 4,369:
The four bit adder presented can work on matricies of 1's and 0's, which are stored as characters, doubles, or booleans. MATLAB has functions to convert decimal numbers to binary, but these functions convert a decimal number not to binary but a string data type of 1's and 0's. So, this four bit adder is written to be compatible with MATLAB's representation of binary. Also, because this is MATLAB, and you might want to add arrays of 4-bit binary numbers together, this implementation will add two column vectors of 4-bit binary numbers together.
 
<langsyntaxhighlight MATLABlang="matlab">function [S,v] = fourBitAdder(input1,input2)
 
%Make sure that only 4-Bit numbers are being added. This assumes that
Line 4,081 ⟶ 4,432:
v = num2str(v);
end
end %fourBitAdder</langsyntaxhighlight>
 
Sample Usage:
<langsyntaxhighlight MATLABlang="matlab">>> [S,V] = fourBitAdder([0 0 0 1],[1 1 1 1])
 
S =
Line 4,137 ⟶ 4,488:
 
11
12</langsyntaxhighlight>
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">XOR(Y,Z) ;Uses logicals - i.e., 0 is false, anything else is true (1 is used if setting a value)
QUIT (Y&'Z)!('Y&Z)
HALF(W,X)
Line 4,153 ⟶ 4,504:
FOR I=4:-1:1 SET T=$$FULL($E(Y,I),$E(Z,I),C4),$E(S,I)=$P(T,"^",1),C4=$P(T,"^",2)
K I,T
QUIT S_"^"_C4</langsyntaxhighlight>
Usage:<pre>USER>S N1="0110",N2="0010",C=0,T=$$FOUR^ADDER(N1,N2,C)
Line 4,170 ⟶ 4,521:
The test code simulates the adder and exports trace wave file for debug support. Verilog and VHDL files are exported for hardware synthesis.
 
<langsyntaxhighlight lang="python">"""
To run:
python3 Four_bit_adder_011.py
Line 4,311 ⟶ 4,662:
if __name__ == '__main__':
main()
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">type
 
Bools[N: static int] = array[N, bool]
Line 4,348 ⟶ 4,699:
for a in 0..7:
for b in 0..7:
assert a + b == bus2int fa4(int2bus(a), int2bus(b))</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">
(* File blocks.ml
 
Line 4,554 ⟶ 4,905:
(eval add4_io 4 4 (Array.map Int64.of_int [| a; b |])) in
v.(0), v.(1);;
</syntaxhighlight>
</lang>
 
Testing
 
<langsyntaxhighlight lang="ocaml">
# open Blocks;;
 
Line 4,572 ⟶ 4,923:
# plus 0 0;;
- : int * int = (0, 0)
</syntaxhighlight>
</lang>
 
An extension : n-bit adder, for n <= 64 (n could be greater, but we use Int64 for I/O)
 
<langsyntaxhighlight lang="ocaml">
(* general adder (n bits with n <= 64) *)
let gen_adder n = block_array serial [|
Line 4,591 ⟶ 4,942:
(eval (gadd_io n) n n (Array.map Int64.of_int [| a; b |])) in
v.(0), v.(1);;
</syntaxhighlight>
</lang>
 
And a test
 
<langsyntaxhighlight lang="ocaml">
# gen_plus 7 100 100;;
- : int * int = (72, 1)
# gen_plus 8 100 100;;
- : int * int = (200, 0)
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">xor(a,b)=(!a&b)||(a&!b);
halfadd(a,b)=[a&&b,xor(a,b)];
fulladd(a,b,c)=my(t=halfadd(a,c),s=halfadd(t[2],b));[t[1]||s[1],s[2]];
Line 4,614 ⟶ 4,965:
[s3[1],s3[2],s2[2],s1[2],s0[2]]
};
add4(0,0,0,0,0,0,0,0)</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub dec2bin { sprintf "%04b", shift }
sub bin2dec { oct "0b".shift }
sub bin2bits { reverse split(//, substr(shift,0,shift)); }
Line 4,659 ⟶ 5,010:
$a, $b, $abin, $bbin, $c, $s, bin2dec($c.$s);
}
}</langsyntaxhighlight>
 
Output matches the [[Four bit adder#Ruby|Ruby]] output.
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">xor_gate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
Line 4,714 ⟶ 5,065:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b0010</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1101</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b0011</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 4,733 ⟶ 5,084:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de halfAdder (A B) #> (Carry . Sum)
(cons
(and A B)
Line 4,755 ⟶ 5,106:
(cdr Fa3)
(cdr Fa2)
(cdr Fa1) ) ) )</langsyntaxhighlight>
{{out}}
<pre>: (4bitsAdder NIL NIL NIL T NIL NIL NIL T)
Line 4,770 ⟶ 5,121:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* 4-BIT ADDER */
 
Line 4,806 ⟶ 5,157:
 
END TEST;
</syntaxhighlight>
</lang>
 
=={{header|PowerShell}}==
===Using Bytes as Inputs===
<langsyntaxhighlight Powershelllang="powershell">function bxor2 ( [byte] $a, [byte] $b )
{
$out1 = $a -band ( -bnot $b )
Line 4,861 ⟶ 5,212:
FourBitAdder 0xC 0xB
 
[Convert]::ToByte((FourBitAdder 0xC 0xB)["S"],2)</langsyntaxhighlight>
 
===Translation of C# code===
The well-written C# code on this page can be translated without any modification into a .NET type by PowerShell.
<syntaxhighlight lang="powershell">
<lang PowerShell>
$source = @'
using System;
Line 4,997 ⟶ 5,348:
 
Add-Type -TypeDefinition $source -Language CSharpVersion3
</syntaxhighlight>
</lang>
<syntaxhighlight lang="powershell">
<lang PowerShell>
[RosettaCodeTasks.FourBitAdder.ConstructiveBlocks]::Test()
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 5,017 ⟶ 5,368:
=={{header|Prolog}}==
Using hi/lo symbols to represent binary. As this is a simulation, there is no real "arithmetic" happening.
<langsyntaxhighlight lang="prolog">% binary 4 bit adder chip simulation
 
b_not(in(hi), out(lo)) :- !. % not(1) = 0
Line 5,058 ⟶ 5,409:
test_add(in(hi,hi,lo,hi), in(hi,lo,lo,hi), '11 + 9 = 20'),
test_add(in(lo,lo,lo,hi), in(lo,lo,lo,hi), '8 + 8 = 16'),
test_add(in(hi,hi,hi,hi), in(hi,lo,lo,lo), '15 + 1 = 16').</langsyntaxhighlight>
<pre>?- go.
in(hi,lo,lo,lo) + in(hi,lo,lo,lo) is out(lo,hi,lo,lo) c(lo) (1 + 1 = 2)
Line 5,069 ⟶ 5,420:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">;Because no representation for a solitary bit is present, bits are stored as bytes.
;Output values from the constructive building blocks is done using pointers (i.e. '*').
 
Line 5,122 ⟶ 5,473:
Input()
CloseConsole()
EndIf</langsyntaxhighlight>
{{out}}
<pre>0110 + 1110 = 0100 overflow 1</pre>
Line 5,144 ⟶ 5,495:
between the normal Python values and those of the simulation.
<langsyntaxhighlight lang="python">def xor(a, b): return (a and not b) or (b and not a)
 
def ha(a, b): return xor(a, b), a and b # sum, carry
Line 5,178 ⟶ 5,529:
 
if __name__ == '__main__':
test_fa4()</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define (adder-and a b)
Line 5,226 ⟶ 5,672:
(cons v 4s))))
 
(n-bit-adder '(1 0 1 0) '(0 1 1 1)) ;-> '(1 0 0 0 1)</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub xor ($a, $b) { (($a and not $b) or (not $a and $b)) ?? 1 !! 0 }
 
sub half-adder ($a, $b) {
Line 5,257 ⟶ 5,703:
is four-bit-adder(1, 0, 1, 0, 1, 0, 1, 0), (0, 1, 0, 1, 0), '5 + 5 == 10';
is four-bit-adder(1, 0, 0, 1, 1, 1, 1, 0)[4], 1, '7 + 9 == overflow';
}</langsyntaxhighlight>
 
{{out}}
Line 5,271 ⟶ 5,717:
::::* the &nbsp; &nbsp; '''|''' &nbsp; &nbsp; &nbsp; symbol is a logical '''OR'''.
::::* the &nbsp; &nbsp; '''&amp;''' &nbsp; &nbsp; symbol is a logical '''AND'''.
<langsyntaxhighlight lang="rexx">/*REXX program displays (all) the sums of a full 4─bit adder (with carry). */
call hdr1; call hdr2 /*note the order of headers & trailers.*/
/* [↓] traipse thru all possibilities.*/
Line 5,301 ⟶ 5,747:
do j=0 for 4; n= j - 1
s.j= fullAdder(a.j, b.j, carry.n); carry.j= c
end /*j*/; return c</langsyntaxhighlight>
{{out|output|text=&nbsp; &nbsp; (most lines have been elided):}}
<pre style="height:63ex">
Line 5,374 ⟶ 5,820:
{{Full One-Bit-Adder function is made up of XOR OR AND gates}}
 
<langsyntaxhighlight lang="ring">
 
###---------------------------
Line 5,446 ⟶ 5,892:
###------------------------
 
</syntaxhighlight>
</lang>
Output:
<pre>
Line 5,458 ⟶ 5,904:
Sum...: 1 00110011001100110011001100110010
 
</pre>
 
=={{header|RPL}}==
{{works with|HP|28}}
To make a long piece of code short, on a 4-bit machine equipped with an interpreter capable to handle 64-bit integers, we reduce their size to 1 bit so that we can simulate the working of the 4-bit CPU at a very high level of software abstraction.
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ → nibble
≪ { } 1 nibble SIZE '''FOR''' j
nibble j DUP SUB "1" == R→B + '''NEXT'''
≫ ≫ ‘-<span style="color:blue">→LOAD</span>’ STO
≪ → nibble carry
≪ "" 1 nibble SIZE '''FOR''' bit
nibble bit GET B→R →STR + '''NEXT'''
"→" + carry B→R →STR +
≫ ≫ ‘<span style="color:blue">→DISP</span>’ STO
≪ → a b
≪ a b NOT AND b a NOT AND OR
≫ ≫ ‘<span style="color:blue">→XOR</span>’ STO
≪ → a b
≪ a b <span style="color:blue">→XOR</span> a b AND
≫ ≫ ‘<span style="color:blue">→HALF</span>’ STO
≪ → a b c
≪ c a <span style="color:blue">→HALF</span> SWAP b <span style="color:blue">→HALF</span> ROT OR
≫ ≫ ‘<span style="color:blue">→FULL</span>’ STO
1 STWS
<span style="color:blue">→LOAD</span> SWAP <span style="color:blue">→LOAD</span> → n2 n1
≪ { } #0h 4 1 '''FOR''' bit
n1 bit GET n2 bit GET
ROT <span style="color:blue">→FULL</span>
SWAP ROT + SWAP
-1 '''STEP''' <span style="color:blue">→DISP</span>
≫ ≫ ‘<span style="color:blue">→ADD</span>’ STO
|
<span style="color:blue">→LOAD</span> ''( "nibble" → { #bits } ) ''
turn the input format into a list of bits,
easier to handle
<span style="color:blue">→DISP</span> ''( { #bits } c → "nibble→c" ) ''
convert the bit list to a string
<span style="color:blue">→XOR</span> ''( a b → xor(a,b) ) ''
= (not a and b) or (not b and a)
<span style="color:blue">→HALF</span> ''( a b → a+b carry ) ''
s = (a xor b), c = (a and b)
<span style="color:blue">→FULL</span> ''( a b c → a+b+c carry ) ''
<span style="color:blue">→ADD</span> ''( "nibble" "nibble" → "nibble→c" ) ''
set unsigned integer size to 1 bit
convert strings into lists
from lower to higher bit
read bits
add them
store result, keep carry
show result
|}
"0101" "0011" <span style="color:blue">→ADD</span>
"1111" "1111" <span style="color:blue">→ADD</span>
{{out}}
<pre>
2: "1000→0"
1: "1110→1"
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby"># returns pair [sum, carry]
def four_bit_adder(a, b)
a_bits = binary_string_to_bits(a,4)
Line 5,516 ⟶ 6,044:
[a, b, bin_a, bin_b, carry, sum, (carry + sum).to_i(2)]
end
end</langsyntaxhighlight>
 
{{out}}
Line 5,540 ⟶ 6,068:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
// half adder with XOR and AND
// SUM = A XOR B
Line 5,605 ⟶ 6,133:
}
 
</syntaxhighlight>
</lang>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">-- a "pin" can be connected only to one component
-- that "sets" it to 0 or 1, while it can be "read"
-- ad libitum. (Tristate logic is not taken into account)
Line 5,745 ⟶ 6,273:
", overflow = " + fba.v.val + "\n";
end;
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object FourBitAdder {
type Nibble=(Boolean, Boolean, Boolean, Boolean)
 
Line 5,773 ⟶ 6,301:
((s3, s2, s1, s0), cOut)
}
}</langsyntaxhighlight>
 
A test program using the object above.
<langsyntaxhighlight lang="scala">object FourBitAdderTest {
import FourBitAdder._
def main(args: Array[String]): Unit = {
Line 5,790 ⟶ 6,318:
implicit def intToNibble(i:Int):Nibble=((i>>>3)&1, (i>>>2)&1, (i>>>1)&1, i&1)
def nibbleToString(n:Nibble):String="%d%d%d%d".format(n._1.toInt, n._2.toInt, n._3.toInt, n._4.toInt)
}</langsyntaxhighlight>
{{out}}
<pre> A B S C
Line 5,809 ⟶ 6,337:
{{trans|Common Lisp}}
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme write)
Line 5,843 ⟶ 6,371:
(show-eg (list 1 1 1 1) (list 1 1 1 1))
(show-eg (list 1 0 1 0) (list 0 1 0 1))
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,858 ⟶ 6,386:
This is full adder that means it takes arbitrary number of bits (think of it as infinite stack of 2 bit adders, which is btw how it's internally made).
I took it from https://github.com/emsi/SedScripts
<langsyntaxhighlight lang="sed">
#!/bin/sed -f
# (C) 2005,2014 by Mariusz Woloszyn :)
Line 5,910 ⟶ 6,438:
}
 
b LOOP</langsyntaxhighlight>
 
Example usage:
 
<langsyntaxhighlight lang="sed">
./binAdder.sed
1111110111
Line 5,929 ⟶ 6,457:
0 0 0 1
111
</syntaxhighlight>
</lang>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">func bxor(a, b) {
(~a & b) | (a & ~b)
}
Line 5,963 ⟶ 6,491:
a, b, abin.join, bbin.join, c, s, "#{c}#{s}".bin)
}
}</langsyntaxhighlight>
 
{{out}}
Line 5,989 ⟶ 6,517:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">typealias FourBit = (Int, Int, Int, Int)
 
func halfAdder(_ a: Int, _ b: Int) -> (Int, Int) {
Line 6,016 ⟶ 6,544:
print("\(a) + \(b) = \(fourBitAdder(a, b))")
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 6,024 ⟶ 6,552:
=={{header|SystemVerilog}}==
In SystemVerilog we can define a multibit adder as a parameterized module, that instantiates the components:
<syntaxhighlight lang="systemverilog">
<lang SystemVerilog>
module Half_Adder( input a, b, output s, c );
assign s = a ^ b;
Line 6,058 ⟶ 6,586:
 
endmodule
</syntaxhighlight>
</lang>
 
And then a testbench to test it -- here I use random stimulus with an assertion (it's aften good to separate the stimulus generation from the results-checking):
 
<syntaxhighlight lang="systemverilog">
<lang SystemVerilog>
module simTop();
 
Line 6,094 ⟶ 6,622:
endprogram
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 6,128 ⟶ 6,656:
=={{header|Tcl}}==
This example shows how you can make little languages in Tcl that describe the problem space.
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
# Create our little language
Line 6,192 ⟶ 6,720:
fulladd a2 b2 c2 s2 c3
fulladd a3 b3 c3 s3 v
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="tcl"># Simple driver for the circuit
proc 4add_driver {a b} {
lassign [split $a {}] a3 a2 a1 a0
Line 6,206 ⟶ 6,734:
set a 1011
set b 0110
puts $a+$b=[4add_driver $a $b]</langsyntaxhighlight>
{{out}}
<pre>
Line 6,216 ⟶ 6,744:
 
=={{header|TorqueScript}}==
<langsyntaxhighlight Torquelang="torque">function XOR(%a, %b)
{
return (!%a && %b) || (%a && !%b);
Line 6,244 ⟶ 6,772:
%r3 = FullAdd(%a3, %b3, getWord(%r2, 0));
return getWord(%r0,1) SPC getWord(%r1,1) SPC getWord(%r2,1) SPC getWord(%r3,1) SPC getWord(%r3,0);
}</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 6,254 ⟶ 6,782:
Bash and zsh allow the snake_case function names to be replaced with kebab-case; ksh does not. The use of the <tt>typeset</tt> synonym for <tt>local</tt> is also in order to achieve ksh compatibility.
 
<langsyntaxhighlight lang="sh">xor() {
typeset -i a=$1 b=$2
printf '%d\n' $(( (a || b) && ! (a && b) ))
Line 6,272 ⟶ 6,800:
}
 
four_bit_addrfour_bit_adder() {
typeset -i a0=$1 a1=$2 a2=$3 a3=$4 b0=$5 b1=$6 b2=$7 b3=$8
typeset -i fa0_s fa0_c fa1_s fa1_c fa2_s fa2_c fa3_s fa3_c
Line 6,295 ⟶ 6,823:
}
 
is "1 + 1 = 2" "[[ '$(four_bit_addrfour_bit_adder 1 0 0 0 1 0 0 0)' == '0 1 0 0 0' ]]"
is "5 + 5 = 10" "[[ '$(four_bit_addrfour_bit_adder 1 0 1 0 1 0 1 0)' == '0 1 0 1 0' ]]"
is "7 + 9 = overflow" "a=($(four_bit_addrfour_bit_adder 1 0 0 1 1 1 1 0)); (( \${a[-1]}==1 ))"
 
</lang>
</syntaxhighlight>
 
{{Out}}
Line 6,307 ⟶ 6,836:
=={{header|Verilog}}==
In Verilog we can also define a multibit adder as a component with multiple instances:
<syntaxhighlight lang="verilog">
<lang Verilog>
module Half_Adder( output c, s, input a, b );
xor xor01 (s, a, b);
Line 6,356 ⟶ 6,885:
 
endmodule // test_Full_Adder
</syntaxhighlight>
</lang>
 
{{out}}
Line 6,371 ⟶ 6,900:
=={{header|VHDL}}==
The following is a direct implementation of the proposed schematic:
<langsyntaxhighlight VHDLlang="vhdl">LIBRARY ieee;
USE ieee.std_logic_1164.all;
 
Line 6,531 ⟶ 7,060:
begin
x <= (a and not b) or (b and not a);
end architecture rtl;</langsyntaxhighlight>
 
 
An exhaustive testbench:
<langsyntaxhighlight VHDLlang="vhdl">LIBRARY ieee;
USE ieee.std_logic_1164.all;
use ieee.NUMERIC_STD.all;
Line 6,579 ⟶ 7,108:
);
 
end struct;</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight ecmascriptlang="wren">var xor = Fn.new { |a, b| a&(~b) | b&(~a) }
 
var ha = Fn.new { |a, b| [xor.call(a, b), a & b] }
Line 6,609 ⟶ 7,138:
}
 
System.print(add4.call(1, 0, 1, 0, 1, 0, 0, 1))</langsyntaxhighlight>
 
{{out}}
Line 6,617 ⟶ 7,146:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code CrLf=9, IntOut=11;
 
func Not(A);
Line 6,675 ⟶ 7,204:
Add4Bits(1, 1, 1, 1, 1, 1, 1, 1, @S0, @S1, @S2, @S3, @C); \1111 + 1111 = 11110
BinOut(0, S0, S1, S2, S3, C); CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 6,685 ⟶ 7,214:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn xor(a,b) // a,b are 1|0 -->a^b(1|0)
{ a.bitAnd(b.bitNot()).bitOr(b.bitAnd(a.bitNot())) }
Line 6,708 ⟶ 7,237:
 
// add(10,9) result should be 1 0 0 1 1 (0x13, 3 carry 1)
println(fourBitAdder(1,0,1,0, 1,0,0,1));</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn nBitAddr(as,bs){ //-->(carry, sn..s3,s2,s1,s0)
(ss:=List()).append(
[as.len()-1 .. 0,-1].reduce('wrap(c,n){
Line 6,716 ⟶ 7,245:
.reverse();
}
println(nBitAddr(T(1,0,1,0), T(1,0,0,1)));</langsyntaxhighlight>
{{out}}
<pre>
1,479

edits