Four bit adder: Difference between revisions
→{{header|APL}}: use built-in NAND operator ⍲ instead of defining a new one.
Alextretyak (talk | contribs) (Added 11l) |
(→{{header|APL}}: use built-in NAND operator ⍲ instead of defining a new one.) |
||
(43 intermediate revisions by 18 users not shown) | |||
Line 5:
This design can be realized using four [[wp:Adder_(electronics)#Full_adder|1-bit full adder]]s.
Each of these 1-bit full adders can be built with two [[wp:Adder_(electronics)#Half_adder|half adder]]s and an ''or'' [[wp:Logic gate|gate]].
Finally a half adder can be made using an ''xor'' gate and an ''and'' gate.
The ''xor'' gate can be made using two ''not''s, two ''and''s and one ''or''.
'''Not''', '''or''' and '''and''', the only allowed "gates" for the task, can be "imitated" by using the [[Bitwise operations|bitwise operators]] of your language.
If there is not a ''bit type'' in your language, to be sure that the ''not'' does not "invert" all the other bits of the basic type (e.g. a byte) we are not interested in, you can use an extra ''nand'' (''and'' then ''not'') with the constant '''1''' on one input.
Instead of optimizing and reducing the number of gates used for the final 4-bit adder, build it in the most straightforward way, ''connecting'' the other "constructive blocks", in turn made of "simpler" and "smaller" ones.
{|
|+Schematics of the "constructive blocks"
!(Xor gate
! (A half adder)
! (A full adder)
! (A 4-bit adder)
|-
|[[File:xor.png|frameless|Xor gate done with ands, ors and nots]]
Line 25 ⟶ 29:
|[[File:4bitsadder.png|frameless|A 4-bit adder]]
|}
Solutions should try to be as descriptive as possible, making it as easy as possible to identify "connections" between higher-order "blocks".
It is not mandatory to replicate the syntax of higher-order blocks in the atomic "gate" blocks, i.e. basic "gate" operations can be performed as usual bitwise operations, or they can be "wrapped" in a ''block'' in order to expose the same syntax of higher-order blocks, at implementers' choice.
Line 36 ⟶ 43:
{{trans|Python}}
<
R (a & !b) | (b & !a)
Line 70 ⟶ 77:
tot[i] = ta[i]
tot[width] = tlast
assert(a + b == bus2int(tot), ‘totals don't match: #. + #. != #.’.format(a, b, String(tot)))</
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE Bit="BYTE"
TYPE FourBit=[Bit b0,b1,b2,b3]
Bit FUNC Not(Bit a)
RETURN (1-a)
Bit FUNC MyXor(Bit a,b)
RETURN ((Not(a) AND b) OR (a AND Not(b)))
Bit FUNC HalfAdder(Bit a,b Bit POINTER c)
c^=a AND b
RETURN (MyXor(a,b))
Bit FUNC FullAdder(Bit a,b,c0 Bit POINTER c)
Bit s1,c1,s2,c2
s1=HalfAdder(a,c0,@c1)
s2=HalfAdder(b,s1,@c2)
c^=c1 OR c2
RETURN (s2)
PROC FourBitAdder(FourBit POINTER a,b,s Bit POINTER c)
Bit c1,c2,c3
s.b3=FullAdder(a.b3,b.b3,0,@c3)
s.b2=FullAdder(a.b2,b.b2,c3,@c2)
s.b1=FullAdder(a.b1,b.b1,c2,@c1)
s.b0=FullAdder(a.b0,b.b0,c1,c)
RETURN
PROC InitFourBit(BYTE a FourBit POINTER res)
res.b3=a&1 a==RSH 1
res.b2=a&1 a==RSH 1
res.b1=a&1 a==RSH 1
res.b0=a&1
RETURN
PROC PrintFourBit(FourBit POINTER a)
PrintB(a.b0) PrintB(a.b1)
PrintB(a.b2) PrintB(a.b3)
RETURN
PROC Main()
FourBit a,b,s
Bit c
BYTE i,v
FOR i=1 TO 20
DO
v=Rand(16) InitFourBit(v,a)
v=Rand(16) InitFourBit(v,b)
FourBitAdder(a,b,s,@c)
PrintFourBit(a) Print(" + ")
PrintFourBit(b) Print(" = ")
PrintFourBit(s) Print(" Carry=")
PrintBE(c)
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Four_bit_adder.png Screenshot from Atari 8-bit computer]
<pre>
0100 + 0000 = 0100 Carry=0
1101 + 1011 = 1000 Carry=1
1011 + 0010 = 1101 Carry=0
1101 + 0111 = 0100 Carry=1
0100 + 0101 = 1001 Carry=0
0110 + 1011 = 0001 Carry=1
1110 + 0010 = 0000 Carry=1
0010 + 1110 = 0000 Carry=1
1110 + 1110 = 1100 Carry=1
1100 + 0111 = 0011 Carry=1
0100 + 0011 = 0111 Carry=0
1101 + 0101 = 0010 Carry=1
0001 + 0011 = 0100 Carry=0
1001 + 1000 = 0001 Carry=1
1111 + 1001 = 1000 Carry=1
0001 + 0011 = 0100 Carry=0
0001 + 0000 = 0001 Carry=0
1000 + 0011 = 1011 Carry=0
1110 + 1000 = 0110 Carry=1
0010 + 0100 = 0110 Carry=0
</pre>
=={{header|Ada}}==
<syntaxhighlight lang="ada">
type Four_Bits is array (1..4) of Boolean;
Line 97 ⟶ 192:
Full_Adder (A (1), B (1), C (1), Carry);
end Four_Bits_Adder;
</syntaxhighlight>
A test program with the above definitions
<syntaxhighlight lang="ada">
with Ada.Text_IO; use Ada.Text_IO;
Line 152 ⟶ 247:
end loop;
end Test_4_Bit_Adder;
</syntaxhighlight>
{{out}}
<div style="height: 320px;overflow:scroll">
Line 414 ⟶ 509:
</pre>
</div>
=={{header|APL}}==
{{works with|Dyalog APL}}
{{works with|GNU APL}}
<syntaxhighlight 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
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 left result as well.
half_adder ← { (⍺ and ⍵), ⍺ xor ⍵ } ⍝ returns carry, sum
⍝ 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
(c0 s0) ← c_in half_adder a
(c1 s1) ← s0 half_adder b
result ← (c0 or c1), s1
∇
⍝ Finally, our four-bit adder
∇result ← a adder4 b ; a3; a2; a1; a0; b3; b2; b1; b0; c0; s0; c1; s1; c2; s2; s3; v
(a3 a2 a1 a0) ← a
(b3 b2 b1 b0) ← b
(c0 s0) ← 0 full_adder a0 b0
(c1 s1) ← c0 full_adder a1 b1
(c2 s2) ← c1 full_adder a2 b2
(v s3) ← c2 full_adder a3 b3
result ← v s3 s2 s1 s0
∇
⍝ Add one pair of numbers and print as equation
demo ← { 0⍴⎕←⍺,'+',⍵,'=',{ 1↓⍵,' with carry ',1↑⍵ } ⍺ adder4 ⍵ }
⍝ A way to generate some random numbers for our demo
randbits ← { 1-⍨?⍵⍴2 }
⍝ And go
{ (randbits 4) demo randbits 4 ⊣ ⍵ } ¨ ⍳20
</syntaxhighlight>
{{Out}}
<pre>
1 1 1 1 + 0 0 0 1 = 0 0 0 0 with carry 1
1 1 0 0 + 0 0 0 1 = 1 1 0 1 with carry 0
0 1 1 1 + 0 1 1 1 = 1 1 1 0 with carry 0
1 1 1 0 + 1 0 1 1 = 1 0 0 1 with carry 1
0 1 0 0 + 0 0 1 0 = 0 1 1 0 with carry 0
1 0 1 1 + 0 1 1 0 = 0 0 0 1 with carry 1
1 1 1 1 + 1 0 1 1 = 1 0 1 0 with carry 1
0 1 1 0 + 0 0 1 0 = 1 0 0 0 with carry 0
1 1 0 1 + 0 1 0 0 = 0 0 0 1 with carry 1
1 0 1 0 + 0 0 1 1 = 1 1 0 1 with carry 0
1 1 1 1 + 0 0 0 1 = 0 0 0 0 with carry 1
0 1 1 1 + 1 0 1 1 = 0 0 1 0 with carry 1
0 0 0 1 + 1 1 0 0 = 1 1 0 1 with carry 0
0 0 1 0 + 1 1 1 1 = 0 0 0 1 with carry 1
0 0 1 0 + 0 1 0 0 = 0 1 1 0 with carry 0
1 1 1 0 + 1 0 1 0 = 1 0 0 0 with carry 1
1 0 0 0 + 1 0 0 0 = 0 0 0 0 with carry 1
1 0 1 0 + 0 0 0 0 = 1 0 1 0 with carry 0
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}}
<
B := 9
N := FourBitAdd(A, B)
Line 458 ⟶ 925:
Res := Mod(N, 2) Res, N := N >> 1
return, Res
}</
{{out}}
<pre>13 + 9:
Line 465 ⟶ 932:
=={{header|AutoIt}}==
===Functions===
<syntaxhighlight lang="autoit">
Func _NOT($_A)
Return (Not $_A) *1
Line 506 ⟶ 973:
Return $Q4 & $Q3 & $Q2 & $Q1
EndFunc ;==>_4BitAdder
</syntaxhighlight>
===Example===
<syntaxhighlight lang="autoit">
Local $CarryOut, $sResult
$sResult = _4BitAdder(0, 0, 1, 1, 0, 1, 1, 1, 0, $CarryOut) ; adds 3 + 7
Line 515 ⟶ 982:
$sResult = _4BitAdder(1, 0, 1, 1, 1, 0, 0, 0, 0, $CarryOut) ; adds 11 + 8
ConsoleWrite('result: ' & $sResult & ' ==> carry out: ' & $CarryOut & @LF)
</syntaxhighlight>
{{out}}
<pre>
Line 522 ⟶ 989:
</pre>
--[[User:BugFix|BugFix]] ([[User talk:BugFix|talk]]) 17:10, 14 November 2013 (UTC)
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="basic">100 S$ = "1100 + 1100 = " : GOSUB 400
110 S$ = "1100 + 1101 = " : GOSUB 400
120 S$ = "1100 + 1110 = " : GOSUB 400
130 S$ = "1100 + 1111 = " : GOSUB 400
140 S$ = "1101 + 0000 = " : GOSUB 400
150 S$ = "1101 + 0001 = " : GOSUB 400
160 S$ = "1101 + 0010 = " : GOSUB 400
170 S$ = "1101 + 0011 = " : GOSUB 400
180 END
400 A0 = VAL(MID$(S$, 4, 1))
410 A1 = VAL(MID$(S$, 3, 1))
420 A2 = VAL(MID$(S$, 2, 1))
430 A3 = VAL(MID$(S$, 1, 1))
440 B0 = VAL(MID$(S$, 11, 1))
450 B1 = VAL(MID$(S$, 10, 1))
460 B2 = VAL(MID$(S$, 9, 1))
470 B3 = VAL(MID$(S$, 8, 1))
480 GOSUB 600
490 PRINT S$;
REM 4 BIT PRINT
500 PRINT C;S3;S2;S1;S0
510 RETURN
REM 4 BIT ADD
REM ADD A3 A2 A1 A0 TO B3 B2 B1 B0
REM RESULT IN S3 S2 S1 S0
REM CARRY IN C
600 C = 0
610 A = A0 : B = B0 : GOSUB 700 : S0 = S
620 A = A1 : B = B1 : GOSUB 700 : S1 = S
630 A = A2 : B = B2 : GOSUB 700 : S2 = S
640 A = A3 : B = B3 : GOSUB 700 : S3 = S
650 RETURN
REM FULL ADDER
REM ADD A + B + C
REM RESULT IN S
REM CARRY IN C
700 BH = B : B = C : GOSUB 800 : C1 = C
710 A = S : B = BH : GOSUB 800 : C2 = C
720 C = C1 OR C2
730 RETURN
REM HALF ADDER
REM ADD A + B
REM RESULT IN S
REM CARRY IN C
800 GOSUB 900 : S = C
810 C = A AND B
820 RETURN
REM XOR GATE
REM A XOR B
REM RESULT IN C
900 C = A AND NOT B
910 D = B AND NOT A
920 C = C OR D
930 RETURN</syntaxhighlight>
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight 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
PRINT "1100 + 1101 = ";
PROC4bitadd(1,1,0,0, 1,1,0,1, e,d,c,b,a) : PRINT e,d,c,b,a
PRINT "1100 + 1110 = ";
PROC4bitadd(1,1,0,0, 1,1,1,0, e,d,c,b,a) : PRINT e,d,c,b,a
PRINT "1100 + 1111 = ";
PROC4bitadd(1,1,0,0, 1,1,1,1, e,d,c,b,a) : PRINT e,d,c,b,a
PRINT "1101 + 0000 = ";
PROC4bitadd(1,1,0,1, 0,0,0,0, e,d,c,b,a) : PRINT e,d,c,b,a
PRINT "1101 + 0001 = ";
PROC4bitadd(1,1,0,1, 0,0,0,1, e,d,c,b,a) : PRINT e,d,c,b,a
PRINT "1101 + 0010 = ";
PROC4bitadd(1,1,0,1, 0,0,1,0, e,d,c,b,a) : PRINT e,d,c,b,a
PRINT "1101 + 0011 = ";
PROC4bitadd(1,1,0,1, 0,0,1,1, e,d,c,b,a) : PRINT e,d,c,b,a
END
DEF PROC4bitadd(a3&, a2&, a1&, a0&, b3&, b2&, b1&, b0&, \
\ RETURN c3&, RETURN s3&, RETURN s2&, RETURN s1&, RETURN s0&)
LOCAL c0&, c1&, c2&
PROCfulladder(a0&, b0&, 0, s0&, c0&)
PROCfulladder(a1&, b1&, c0&, s1&, c1&)
PROCfulladder(a2&, b2&, c1&, s2&, c2&)
PROCfulladder(a3&, b3&, c2&, s3&, c3&)
ENDPROC
DEF PROCfulladder(a&, b&, c&, RETURN s&, RETURN c1&)
LOCAL x&, y&, z&
PROChalfadder(a&, c&, x&, y&)
PROChalfadder(x&, b&, s&, z&)
c1& = y& OR z&
ENDPROC
DEF PROChalfadder(a&, b&, RETURN s&, RETURN c&)
s& = FNxorgate(a&, b&)
c& = a& AND b&
ENDPROC
DEF FNxorgate(a&, b&)
LOCAL c&, d&
c& = a& AND NOT b&
d& = b& AND NOT a&
= c& OR d&</syntaxhighlight>
{{out}}
<pre>
1100 + 1100 = 1 1 0 0 0
1100 + 1101 = 1 1 0 0 1
1100 + 1110 = 1 1 0 1 0
1100 + 1111 = 1 1 0 1 1
1101 + 0000 = 0 1 1 0 1
1101 + 0001 = 0 1 1 1 0
1101 + 0010 = 0 1 1 1 1
1101 + 0011 = 1 0 0 0 0
</pre>
=={{header|Batch File}}==
<
@echo off
setlocal enabledelayedexpansion
Line 661 ⟶ 1,252:
if %1==1 if %2==1 exit /b 1
exit /b 0
</syntaxhighlight>
{{out}}
<pre>
Line 667 ⟶ 1,258:
Second 4-Bit Binary Number: 0111
1011 + 0111 = 10010
</pre>
=={{header|C}}==
<
typedef char pin_t;
Line 863 ⟶ 1,330:
return 0;
}</
=={{header|C sharp|C#}}==
{{works with|C sharp|C#|3+}}
<
using System;
using System.Collections.Generic;
Line 1,000 ⟶ 1,464:
}
</syntaxhighlight>
=={{header|C++}}==
See [[Four bit adder/C++]]
=={{header|Clojure}}==
<
(ns rosettacode.adder
(:use clojure.test))
Line 1,038 ⟶ 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>
===Second Clojure solution===
<
;; a bit is represented as a boolean (true/false)
Line 1,093 ⟶ 1,560:
)
</syntaxhighlight>
===Using Bitwise Operators===
<
(defn to-binary-seq [^long x]
(map #(- (int %) (int \0))
Line 1,130 ⟶ 1,597:
(is (= (Long/parseLong (apply str (ripple-carry-adder (to-binary-seq 130) (to-binary-seq 250))) 2)
(+ 130 250))))
</syntaxhighlight>
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
program-id. test-add.
environment division.
Line 1,242 ⟶ 1,709:
end program add-4b.
</syntaxhighlight>
{{out}}
<pre>
Line 1,257 ⟶ 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.
<
# ATOMIC GATES
not_gate = (bit) ->
Line 1,297 ⟶ 1,764:
adder = n_bit_adder(4)
console.log adder [1, 0, 1, 0], [0, 1, 1, 0]
</syntaxhighlight>
=={{header|Common Lisp}}==
<
(defun half-adder (a b)
(list (logxor a b) (logand a b)))
Line 1,332 ⟶ 1,799:
(main)
</syntaxhighlight>
output:
<pre>
Line 1,345 ⟶ 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.
<
void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3,
Line 1,411 ⟶ 1,878:
writefln(" s0 %032b", s0);
writefln("overflow %032b", overflow);
}</
{{out}}
<pre> a3 00000000000000000000000000000000
Line 1,429 ⟶ 1,896:
overflow 11111111111111111111111111111111</pre>
128 4-bit adds in parallel:
<
void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3,
Line 1,494 ⟶ 1,961:
writefln(" s0 %(%08b%)", s0.array);
writefln("overflow %(%08b%)", overflow.array);
}</
{{out}}
<pre> a3 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Line 1,512 ⟶ 1,979:
overflow 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</pre>
Compiled by the ldc2 compiler to (where T = ubyte32, 256 adds using AVX2):
<
pushl %ebp
movl %esp, %ebp
Line 1,550 ⟶ 2,017:
popl %ebp
vzeroupper
ret $160</
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
program Four_bit_adder;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
Type
TBitAdderOutput = record
S, C: Boolean;
procedure Assign(_s, _C: Boolean);
function ToString: string;
end;
TNibbleBits = array[1..4] of Boolean;
TNibble = record
Bits: TNibbleBits;
procedure Assign(_Bits: TNibbleBits); overload;
procedure Assign(value: byte); overload;
function ToString: string;
end;
TFourBitAdderOutput = record
N: TNibble;
c: Boolean;
procedure Assign(_c: Boolean; _N: TNibbleBits);
function ToString: string;
end;
TLogic = record
class function GateNot(const A: Boolean): Boolean; static;
class function GateAnd(const A, B: Boolean): Boolean; static;
class function GateOr(const A, B: Boolean): Boolean; static;
class function GateXor(const A, B: Boolean): Boolean; static;
end;
TConstructiveBlocks = record
function HalfAdder(const A, B: Boolean): TBitAdderOutput;
function FullAdder(const A, B, CI: Boolean): TBitAdderOutput;
function FourBitAdder(const A, B: TNibble; const CI: Boolean): TFourBitAdderOutput;
end;
{ TBitAdderOutput }
procedure TBitAdderOutput.Assign(_s, _C: Boolean);
begin
s := _s;
c := _C;
end;
function TBitAdderOutput.ToString: string;
begin
Result := 'S' + ord(s).ToString + 'C' + ord(c).ToString;
end;
{ TNibble }
procedure TNibble.Assign(_Bits: TNibbleBits);
var
i: Integer;
begin
for i := 1 to 4 do
Bits[i] := _Bits[i];
end;
procedure TNibble.Assign(value: byte);
var
i: Integer;
begin
value := value and $0F;
for i := 1 to 4 do
Bits[i] := ((value shr (i - 1)) and 1) = 1;
end;
function TNibble.ToString: string;
var
i: Integer;
begin
Result := '';
for i := 4 downto 1 do
Result := Result + ord(Bits[i]).ToString;
end;
{ TFourBitAdderOutput }
procedure TFourBitAdderOutput.Assign(_c: Boolean; _N: TNibbleBits);
begin
N.Assign(_N);
c := _c;
end;
function TFourBitAdderOutput.ToString: string;
begin
Result := N.ToString + ' c=' + ord(c).ToString;
end;
{ TLogic }
class function TLogic.GateAnd(const A, B: Boolean): Boolean;
begin
Result := A and B;
end;
class function TLogic.GateNot(const A: Boolean): Boolean;
begin
Result := not A;
end;
class function TLogic.GateOr(const A, B: Boolean): Boolean;
begin
Result := A or B;
end;
class function TLogic.GateXor(const A, B: Boolean): Boolean;
begin
Result := GateOr(GateAnd(A, GateNot(B)), (GateAnd(GateNot(A), B)));
end;
{ TConstructiveBlocks }
function TConstructiveBlocks.FourBitAdder(const A, B: TNibble; const CI: Boolean):
TFourBitAdderOutput;
var
FA: array[1..4] of TBitAdderOutput;
i: Integer;
begin
FA[1] := FullAdder(A.Bits[1], B.Bits[1], CI);
Result.N.Bits[1] := FA[1].S;
for i := 2 to 4 do
begin
FA[i] := FullAdder(A.Bits[i], B.Bits[i], FA[i - 1].C);
Result.N.Bits[i] := FA[i].S;
end;
Result.C := FA[4].C;
end;
function TConstructiveBlocks.FullAdder(const A, B, CI: Boolean): TBitAdderOutput;
var
HA1, HA2: TBitAdderOutput;
begin
HA1 := HalfAdder(CI, A);
HA2 := HalfAdder(HA1.S, B);
Result.Assign(HA2.S, TLogic.GateOr(HA1.C, HA2.C));
end;
function TConstructiveBlocks.HalfAdder(const A, B: Boolean): TBitAdderOutput;
begin
Result.Assign(TLogic.GateXor(A, B), TLogic.GateAnd(A, B));
end;
var
j, k: Integer;
A, B: TNibble;
Blocks: TConstructiveBlocks;
begin
for k := 0 to 255 do
begin
A.Assign(0);
B.Assign(0);
for j := 0 to 7 do
begin
if j < 4 then
A.Bits[j + 1] := ((1 shl j) and k) > 0
else
B.Bits[j + 1 - 4] := ((1 shl j) and k) > 0;
end;
write(A.ToString, ' + ', B.ToString, ' = ');
Writeln(Blocks.FourBitAdder(A, B, false).ToString);
end;
Writeln;
Readln;
end.
</syntaxhighlight>
{{out}}
<pre>
0000 + 0000 = 0000 c=0
0001 + 0000 = 0001 c=0
0010 + 0000 = 0010 c=0
0011 + 0000 = 0011 c=0
0100 + 0000 = 0100 c=0
0101 + 0000 = 0101 c=0
0110 + 0000 = 0110 c=0
0111 + 0000 = 0111 c=0
1000 + 0000 = 1000 c=0
1001 + 0000 = 1001 c=0
1010 + 0000 = 1010 c=0
1011 + 0000 = 1011 c=0
1100 + 0000 = 1100 c=0
1101 + 0000 = 1101 c=0
1110 + 0000 = 1110 c=0
1111 + 0000 = 1111 c=0
0000 + 0001 = 0001 c=0
0001 + 0001 = 0010 c=0
0010 + 0001 = 0011 c=0
0011 + 0001 = 0100 c=0
0100 + 0001 = 0101 c=0
0101 + 0001 = 0110 c=0
0110 + 0001 = 0111 c=0
0111 + 0001 = 1000 c=0
1000 + 0001 = 1001 c=0
1001 + 0001 = 1010 c=0
1010 + 0001 = 1011 c=0
1011 + 0001 = 1100 c=0
1100 + 0001 = 1101 c=0
1101 + 0001 = 1110 c=0
1110 + 0001 = 1111 c=0
1111 + 0001 = 0000 c=1
0000 + 0010 = 0010 c=0
0001 + 0010 = 0011 c=0
0010 + 0010 = 0100 c=0
0011 + 0010 = 0101 c=0
0100 + 0010 = 0110 c=0
0101 + 0010 = 0111 c=0
0110 + 0010 = 1000 c=0
0111 + 0010 = 1001 c=0
1000 + 0010 = 1010 c=0
1001 + 0010 = 1011 c=0
1010 + 0010 = 1100 c=0
1011 + 0010 = 1101 c=0
1100 + 0010 = 1110 c=0
1101 + 0010 = 1111 c=0
1110 + 0010 = 0000 c=1
1111 + 0010 = 0001 c=1
0000 + 0011 = 0011 c=0
0001 + 0011 = 0100 c=0
0010 + 0011 = 0101 c=0
0011 + 0011 = 0110 c=0
0100 + 0011 = 0111 c=0
0101 + 0011 = 1000 c=0
0110 + 0011 = 1001 c=0
0111 + 0011 = 1010 c=0
1000 + 0011 = 1011 c=0
1001 + 0011 = 1100 c=0
1010 + 0011 = 1101 c=0
1011 + 0011 = 1110 c=0
1100 + 0011 = 1111 c=0
1101 + 0011 = 0000 c=1
1110 + 0011 = 0001 c=1
1111 + 0011 = 0010 c=1
0000 + 0100 = 0100 c=0
0001 + 0100 = 0101 c=0
0010 + 0100 = 0110 c=0
0011 + 0100 = 0111 c=0
0100 + 0100 = 1000 c=0
0101 + 0100 = 1001 c=0
0110 + 0100 = 1010 c=0
0111 + 0100 = 1011 c=0
1000 + 0100 = 1100 c=0
1001 + 0100 = 1101 c=0
1010 + 0100 = 1110 c=0
1011 + 0100 = 1111 c=0
1100 + 0100 = 0000 c=1
1101 + 0100 = 0001 c=1
1110 + 0100 = 0010 c=1
1111 + 0100 = 0011 c=1
0000 + 0101 = 0101 c=0
0001 + 0101 = 0110 c=0
0010 + 0101 = 0111 c=0
0011 + 0101 = 1000 c=0
0100 + 0101 = 1001 c=0
0101 + 0101 = 1010 c=0
0110 + 0101 = 1011 c=0
0111 + 0101 = 1100 c=0
1000 + 0101 = 1101 c=0
1001 + 0101 = 1110 c=0
1010 + 0101 = 1111 c=0
1011 + 0101 = 0000 c=1
1100 + 0101 = 0001 c=1
1101 + 0101 = 0010 c=1
1110 + 0101 = 0011 c=1
1111 + 0101 = 0100 c=1
0000 + 0110 = 0110 c=0
0001 + 0110 = 0111 c=0
0010 + 0110 = 1000 c=0
0011 + 0110 = 1001 c=0
0100 + 0110 = 1010 c=0
0101 + 0110 = 1011 c=0
0110 + 0110 = 1100 c=0
0111 + 0110 = 1101 c=0
1000 + 0110 = 1110 c=0
1001 + 0110 = 1111 c=0
1010 + 0110 = 0000 c=1
1011 + 0110 = 0001 c=1
1100 + 0110 = 0010 c=1
1101 + 0110 = 0011 c=1
1110 + 0110 = 0100 c=1
1111 + 0110 = 0101 c=1
0000 + 0111 = 0111 c=0
0001 + 0111 = 1000 c=0
0010 + 0111 = 1001 c=0
0011 + 0111 = 1010 c=0
0100 + 0111 = 1011 c=0
0101 + 0111 = 1100 c=0
0110 + 0111 = 1101 c=0
0111 + 0111 = 1110 c=0
1000 + 0111 = 1111 c=0
1001 + 0111 = 0000 c=1
1010 + 0111 = 0001 c=1
1011 + 0111 = 0010 c=1
1100 + 0111 = 0011 c=1
1101 + 0111 = 0100 c=1
1110 + 0111 = 0101 c=1
1111 + 0111 = 0110 c=1
0000 + 1000 = 1000 c=0
0001 + 1000 = 1001 c=0
0010 + 1000 = 1010 c=0
0011 + 1000 = 1011 c=0
0100 + 1000 = 1100 c=0
0101 + 1000 = 1101 c=0
0110 + 1000 = 1110 c=0
0111 + 1000 = 1111 c=0
1000 + 1000 = 0000 c=1
1001 + 1000 = 0001 c=1
1010 + 1000 = 0010 c=1
1011 + 1000 = 0011 c=1
1100 + 1000 = 0100 c=1
1101 + 1000 = 0101 c=1
1110 + 1000 = 0110 c=1
1111 + 1000 = 0111 c=1
0000 + 1001 = 1001 c=0
0001 + 1001 = 1010 c=0
0010 + 1001 = 1011 c=0
0011 + 1001 = 1100 c=0
0100 + 1001 = 1101 c=0
0101 + 1001 = 1110 c=0
0110 + 1001 = 1111 c=0
0111 + 1001 = 0000 c=1
1000 + 1001 = 0001 c=1
1001 + 1001 = 0010 c=1
1010 + 1001 = 0011 c=1
1011 + 1001 = 0100 c=1
1100 + 1001 = 0101 c=1
1101 + 1001 = 0110 c=1
1110 + 1001 = 0111 c=1
1111 + 1001 = 1000 c=1
0000 + 1010 = 1010 c=0
0001 + 1010 = 1011 c=0
0010 + 1010 = 1100 c=0
0011 + 1010 = 1101 c=0
0100 + 1010 = 1110 c=0
0101 + 1010 = 1111 c=0
0110 + 1010 = 0000 c=1
0111 + 1010 = 0001 c=1
1000 + 1010 = 0010 c=1
1001 + 1010 = 0011 c=1
1010 + 1010 = 0100 c=1
1011 + 1010 = 0101 c=1
1100 + 1010 = 0110 c=1
1101 + 1010 = 0111 c=1
1110 + 1010 = 1000 c=1
1111 + 1010 = 1001 c=1
0000 + 1011 = 1011 c=0
0001 + 1011 = 1100 c=0
0010 + 1011 = 1101 c=0
0011 + 1011 = 1110 c=0
0100 + 1011 = 1111 c=0
0101 + 1011 = 0000 c=1
0110 + 1011 = 0001 c=1
0111 + 1011 = 0010 c=1
1000 + 1011 = 0011 c=1
1001 + 1011 = 0100 c=1
1010 + 1011 = 0101 c=1
1011 + 1011 = 0110 c=1
1100 + 1011 = 0111 c=1
1101 + 1011 = 1000 c=1
1110 + 1011 = 1001 c=1
1111 + 1011 = 1010 c=1
0000 + 1100 = 1100 c=0
0001 + 1100 = 1101 c=0
0010 + 1100 = 1110 c=0
0011 + 1100 = 1111 c=0
0100 + 1100 = 0000 c=1
0101 + 1100 = 0001 c=1
0110 + 1100 = 0010 c=1
0111 + 1100 = 0011 c=1
1000 + 1100 = 0100 c=1
1001 + 1100 = 0101 c=1
1010 + 1100 = 0110 c=1
1011 + 1100 = 0111 c=1
1100 + 1100 = 1000 c=1
1101 + 1100 = 1001 c=1
1110 + 1100 = 1010 c=1
1111 + 1100 = 1011 c=1
0000 + 1101 = 1101 c=0
0001 + 1101 = 1110 c=0
0010 + 1101 = 1111 c=0
0011 + 1101 = 0000 c=1
0100 + 1101 = 0001 c=1
0101 + 1101 = 0010 c=1
0110 + 1101 = 0011 c=1
0111 + 1101 = 0100 c=1
1000 + 1101 = 0101 c=1
1001 + 1101 = 0110 c=1
1010 + 1101 = 0111 c=1
1011 + 1101 = 1000 c=1
1100 + 1101 = 1001 c=1
1101 + 1101 = 1010 c=1
1110 + 1101 = 1011 c=1
1111 + 1101 = 1100 c=1
0000 + 1110 = 1110 c=0
0001 + 1110 = 1111 c=0
0010 + 1110 = 0000 c=1
0011 + 1110 = 0001 c=1
0100 + 1110 = 0010 c=1
0101 + 1110 = 0011 c=1
0110 + 1110 = 0100 c=1
0111 + 1110 = 0101 c=1
1000 + 1110 = 0110 c=1
1001 + 1110 = 0111 c=1
1010 + 1110 = 1000 c=1
1011 + 1110 = 1001 c=1
1100 + 1110 = 1010 c=1
1101 + 1110 = 1011 c=1
1110 + 1110 = 1100 c=1
1111 + 1110 = 1101 c=1
0000 + 1111 = 1111 c=0
0001 + 1111 = 0000 c=1
0010 + 1111 = 0001 c=1
0011 + 1111 = 0010 c=1
0100 + 1111 = 0011 c=1
0101 + 1111 = 0100 c=1
0110 + 1111 = 0101 c=1
0111 + 1111 = 0110 c=1
1000 + 1111 = 0111 c=1
1001 + 1111 = 1000 c=1
1010 + 1111 = 1001 c=1
1011 + 1111 = 1010 c=1
1100 + 1111 = 1011 c=1
1101 + 1111 = 1100 c=1
1110 + 1111 = 1101 c=1
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}}
<
use Bitwise
@bit_size 4
Line 1,601 ⟶ 2,540:
end
RC.task</
{{out}}
Line 1,627 ⟶ 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">
-module( four_bit_adder ).
Line 1,698 ⟶ 2,637:
%% xor exists, this is another implementation.
z_xor( A, B ) -> (A band (2+bnot B)) bor ((2+bnot A) band B).
</syntaxhighlight>
{{out}}
<pre>
28> four_bit_adder:task().
{[0,1,0,1],0}
</pre>
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
type Bit =
| Zero
| One
let bNot = function
| Zero -> One
| One -> Zero
let bAnd a b =
match (a, b) with
| (One, One) -> One
| _ -> Zero
let bOr a b =
match (a, b) with
| (Zero, Zero) -> Zero
| _ -> One
let bXor a b = bAnd (bOr a b) (bNot (bAnd a b))
let bHA a b = bAnd a b, bXor a b
let bFA a b cin =
let (c0, s0) = bHA a b
let (c1, s1) = bHA s0 cin
(bOr c0 c1, s1)
let b4A (a3, a2, a1, a0) (b3, b2, b1, b0) =
let (c1, s0) = bFA a0 b0 Zero
let (c2, s1) = bFA a1 b1 c1
let (c3, s2) = bFA a2 b2 c2
let (c4, s3) = bFA a3 b3 c3
(c4, s3, s2, s1, s0)
printfn "0001 + 0111 ="
b4A (Zero, Zero, Zero, One) (Zero, One, One, One) |> printfn "%A"
</syntaxhighlight>
{{out}}
<pre>
0001 + 0111 =
(Zero, One, Zero, Zero,
</pre>
=={{header|Forth}}==
<
: "XOR" over over "NOT" and >r swap "NOT" and r> or ;
: halfadder over over and >r "XOR" r> ;
Line 1,717 ⟶ 2,701:
;
: .add4 4bitadder 0 .r 4 0 do i 3 - abs roll 0 .r loop cr ;</
{{out}}
<pre>1 1 0 0 0 0 1 1 .add4 01111
Line 1,724 ⟶ 2,708:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 1,800 ⟶ 2,784:
end do
end do
end program</
{{out}} (selected)
<pre>1100 + 1100 = 11000
Line 1,811 ⟶ 2,795:
1101 + 0011 = 10000</pre>
=={{header|
{{FormulaeEntry|page=https://formulae.org/?script=examples/Four_bit_adder}}
'''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> :</span>
[[File:Fōrmulæ - Four bit adder 07.png]]
=={{header|FreeBASIC}}==
<syntaxhighlight 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
c = a and b
end sub
sub full_add( byval a as ubyte, byval b as ubyte, byval c as ubyte,_
byref s as ubyte, byref g as ubyte )
dim as ubyte x, y, z
half_add( a, c, x, y )
half_add( x, b, s, z )
g = y or z
end sub
sub fourbit_add( byval a3 as ubyte, byval a2 as ubyte, byval a1 as ubyte, byval a0 as ubyte,_
byval b3 as ubyte, byval b2 as ubyte, byval b1 as ubyte, byval b0 as ubyte,_
byref s3 as ubyte, byref s2 as ubyte, byref s1 as ubyte, byref s0 as ubyte,_
byref carry as ubyte )
dim as ubyte c2, c1, c0
full_add(a0, b0, 0, s0, c0)
full_add(a1, b1, c0, s1, c1)
full_add(a2, b2, c1, s2, c2)
full_add(a3, b3, c2, s3, carry )
end sub
dim as ubyte s3, s2, s1, s0, carry
print "1100 + 0011 = ";
fourbit_add( 1, 1, 0, 0, 0, 0, 1, 1, s3, s2, s1, s0, carry )
print carry;s3;s2;s1;s0
print "1111 + 0001 = ";
fourbit_add( 1, 1, 1, 1, 0, 0, 0, 1, s3, s2, s1, s0, carry )
print carry;s3;s2;s1;s0
print "1111 + 1111 = ";
fourbit_add( 1, 1, 1, 1, 1, 1, 1, 1, s3, s2, s1, s0, carry )
print carry;s3;s2;s1;s0</syntaxhighlight>
{{out}}<pre>
1100 + 0011 = 01111
1111 + 0001 = 10000
1111 + 1111 = 11110
</pre>
Line 1,860 ⟶ 2,868:
Go does not have a bit type, so byte is used.
This is the straightforward solution using bytes and functions.
<
import "fmt"
Line 1,890 ⟶ 2,898:
// add 10+9 result should be 1 0 0 1 1
fmt.Println(add4(1, 0, 1, 0, 1, 0, 0, 1))
}</
{{out}}
<pre>
Line 1,898 ⟶ 2,906:
===Channels===
Alternative solution is a little more like a simulation.
<
import "fmt"
Line 2,023 ⟶ 3,031:
B[<-r4], B[<-r3], B[<-r2], B[<-r1], B[<-carry])
}
</syntaxhighlight>
Mini reference:
Line 2,030 ⟶ 3,038:
* "<tt><-channel</tt>" reads a value from a channel. Blocks if its buffer is empty.
* "go function()" creates and runs a go-rountine. It will continue executing concurrently.
=={{header|Groovy}}==
<syntaxhighlight lang="groovy">class Main {
static void main(args) {
def bit1, bit2, bit3, bit4, carry
def fourBitAdder = new FourBitAdder(
{ value -> bit1 = value },
{ value -> bit2 = value },
{ value -> bit3 = value },
{ value -> bit4 = value },
{ value -> carry = value }
)
// 5 + 6 = 11
// 0101 i.e. 5
fourBitAdder.setNum1Bit1 false
fourBitAdder.setNum1Bit2 true
fourBitAdder.setNum1Bit3 false
fourBitAdder.setNum1Bit4 true
// 0110 i.e 6
fourBitAdder.setNum2Bit1 false
fourBitAdder.setNum2Bit2 true
fourBitAdder.setNum2Bit3 true
fourBitAdder.setNum2Bit4 false
def boolToInt = { bool ->
bool ? 1 : 0
}
println ("0101 + 0110 = ${boolToInt(bit1)}${boolToInt(bit2)}${boolToInt(bit3)}${boolToInt(bit4)}")
}
}
class Not {
Closure output
Not(output) {
this.output = output
}
def setInput(input) {
output !input
}
}
class And {
boolean input1
boolean input2
Closure output
And(output) {
this.output = output
}
def setInput1(input) {
this.input1 = input
output(input1 && input2)
}
def setInput2(input) {
this.input2 = input
output(input1 && input2)
}
}
class Nand {
And andGate
Not notGate
Nand(output) {
notGate = new Not(output)
andGate = new And({ value ->
notGate.setInput value
})
}
def setInput1(input) {
andGate.setInput1 input
}
def setInput2(input) {
andGate.setInput2 input
}
}
class Or {
Not firstInputNegation
Not secondInputNegation
Nand nandGate
Or(output) {
nandGate = new Nand(output)
firstInputNegation = new Not({ value ->
nandGate.setInput1 value
})
secondInputNegation = new Not({ value ->
nandGate.setInput2 value
})
}
def setInput1(input) {
firstInputNegation.setInput input
}
def setInput2(input) {
secondInputNegation.setInput input
}
}
class Xor {
And andGate
Or orGate
Nand nandGate
Xor(output) {
andGate = new And(output)
orGate = new Or({ value ->
andGate.setInput1 value
})
nandGate = new Nand({ value ->
andGate.setInput2 value
})
}
def setInput1(input) {
orGate.setInput1 input
nandGate.setInput1 input
}
def setInput2(input) {
orGate.setInput2 input
nandGate.setInput2 input
}
}
class Adder {
Or orGate
Xor xorGate1
Xor xorGate2
And andGate1
And andGate2
Adder(sumOutput, carryOutput) {
xorGate1 = new Xor(sumOutput)
orGate = new Or(carryOutput)
andGate1 = new And({ value ->
orGate.setInput1 value
})
xorGate2 = new Xor({ value ->
andGate1.setInput1 value
xorGate1.setInput1 value
})
andGate2 = new And({ value ->
orGate.setInput2 value
})
}
def setBit1(input) {
xorGate2.setInput1 input
andGate2.setInput2 input
}
def setBit2(input) {
xorGate2.setInput2 input
andGate2.setInput1 input
}
def setCarry(input) {
andGate1.setInput2 input
xorGate1.setInput2 input
}
}
class FourBitAdder {
Adder adder1
Adder adder2
Adder adder3
Adder adder4
FourBitAdder(bit1, bit2, bit3, bit4, carry) {
adder1 = new Adder(bit1, carry)
adder2 = new Adder(bit2, { value ->
adder1.setCarry value
})
adder3 = new Adder(bit3, { value ->
adder2.setCarry value
})
adder4 = new Adder(bit4, { value ->
adder3.setCarry value
})
}
def setNum1Bit1(input) {
adder1.setBit1 input
}
def setNum1Bit2(input) {
adder2.setBit1 input
}
def setNum1Bit3(input) {
adder3.setBit1 input
}
def setNum1Bit4(input) {
adder4.setBit1 input
}
def setNum2Bit1(input) {
adder1.setBit2 input
}
def setNum2Bit2(input) {
adder2.setBit2 input
}
def setNum2Bit3(input) {
adder3.setBit2 input
}
def setNum2Bit4(input) {
adder4.setBit2 input
}
}</syntaxhighlight>
=={{header|Haskell}}==
Basic gates:
<
import Data.List (mapAccumR)
Line 2,040 ⟶ 3,277:
band = min
bnot :: Int -> Int
bnot = (1-)</
Gates built with basic ones:
<
nand = (bnot.).band
xor a b = uncurry nand. (nand a &&& nand b) $ nand a b</
Adder circuits:
<
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</
Example using adder4
<
(1,[1,0,0,1])</
=={{header|Icon}} and {{header|Unicon}}==
Line 2,059 ⟶ 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.
<
# 4bitadder.icn, emulate a 4 bit adder. Using only and, or, not
#
Line 2,161 ⟶ 3,398:
# cr.c is the overflow carry
return s
end</
{{out}}
Line 2,185 ⟶ 3,422:
1111 + 1001 = 1:1000
1111 + 1111 = 1:1110</pre>
=={{header|J}}==
===Implementation===
<
or=: +.
not=: -.
xor=: (and not) or (and not)~
hadd=: and ,"0 xor
add=: ((({.,0:)@[ or {:@[ hadd {.@]), }.@])/@hadd</
===Example use===
<
1 0 1 1 0</
To produce all results:
<
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:
<
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 2,222 ⟶ 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</
Alternatively, the fact that add was designed to operate on lists of bits could have been incorporated into its definition:
<
Then to get all results you could use:
<
Compare this to a regular addition table:
<syntaxhighlight lang
(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 2,269 ⟶ 3,505:
See also: "A Formal Description of System/360” by Adin Falkoff
=={{header|Java}}==
<
{
// Basic gate interfaces
Line 2,419 ⟶ 3,654:
}
}</
{{out}}
Line 2,436 ⟶ 3,671:
0s and 1s. To enforce this, we'll first create a couple of helper functions.
<syntaxhighlight lang="javascript">
function acceptedBinFormat(bin) {
if (bin == 1 || bin === 0 || bin === '0')
Line 2,451 ⟶ 3,686:
return true;
}
</syntaxhighlight>
===Implementation===
Line 2,458 ⟶ 3,693:
and, finally, the four bit adder.
<syntaxhighlight 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 2,531 ⟶ 3,766:
return out.join('');
}
</syntaxhighlight>
===Example Use===
<
all results:
<syntaxhighlight lang="javascript">
// run this in your browsers console
var outer = inner = 16, a, b;
Line 2,549 ⟶ 3,784:
inner = outer;
}
</syntaxhighlight>
=={{header|jq}}==
Line 2,557 ⟶ 3,791:
All the operations except fourBitAdder(a,b) assume the inputs are presented as 0 or 1 (i.e. integers).
<
# These allow us to construct 'nand', 'or', and 'xor',
# and so on.
Line 2,598 ⟶ 3,832:
| fullAdder($inA[0]; $inB[0]; $pass.carry) as $pass
| .[0] = $pass.sum
| map(tostring) | join("") ;</
'''Example:'''
<
{{out}}
$ jq -n -f Four_bit_adder.jq
Line 2,607 ⟶ 3,841:
=={{header|Jsish}}==
Based on Javascript entry.
<
/* 4 bit adder simulation, in Jsish */
function not(a) { return a == 1 ? 0 : 1; }
Line 2,687 ⟶ 3,921:
PASS!: err = bad bit at a[3] of "2"
=!EXPECTEND!=
*/</
{{out}}
<pre>prompt$ jsish --U fourBitAdder.jsi
Line 2,711 ⟶ 3,945:
'''Functions'''
<
xor{T<:Bool}(a::T, b::T) = (a&~b)|(~a&b)
Line 2,741 ⟶ 3,975:
Base.bits(n::BitArray{1}) = join(reverse(int(n)), "")
</syntaxhighlight>
'''Main'''
<syntaxhighlight lang="julia">
xavail = trues(15,15)
xcnt = 0
Line 2,763 ⟶ 3,997:
bits(s), oflow))
end
</syntaxhighlight>
{{out}}
Line 2,781 ⟶ 4,015:
=={{header|Kotlin}}==
<
val Boolean.I get() = if (this) 1 else 0
Line 2,832 ⟶ 4,066:
for (j in i..minOf(i + 1, 15)) test(i, j)
}
}</
{{out}}
Line 2,886 ⟶ 4,120:
[[File:4bit_adder_connector.png]] [[File:4bit_adder_panel.png]] [[File:4bit_adder_diagram.png]]
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def xor
{lambda {:a :b}
{or {and :a {not :b}} {and :b {not :a}}}}}
-> xor
{def halfAdder
{lambda {:a :b}
{cons {and :a :b} {xor :a :b}}}}
-> halfAdder
{def fullAdder
{lambda {:a :b :c}
{let { {:b :b}
{:ha1 {halfAdder :c :a}} }
{let { {:ha1 :ha1}
{:ha2 {halfAdder {cdr :ha1} :b}} }
{cons {or {car :ha1} {car :ha2}} {cdr :ha2}} }}}}
-> fullAdder
{def 4bitsAdder
{lambda {:a4 :a3 :a2 :a1 :b4 :b3 :b2 :b1}
{let { {:a4 :a4} {:a3 :a3} {:a2 :a2} {:b4 :b4} {:b3 :b3} {:b2 :b2}
{:fa1 {fullAdder :a1 :b1 false}} }
{let { {:a4 :a4} {:a3 :a3} {:b4 :b4} {:b3 :b3}
{:fa1 :fa1}
{:fa2 {fullAdder :a2 :b2 {car :fa1}}} }
{let { {:a4 :a4} {:b4 :b4}
{:fa1 :fa1} {:fa2 :fa2}
{:fa3 {fullAdder :a3 :b3 {car :fa2}}} }
{let { {:fa1 :fa1} {:fa2 :fa2} {:fa3 :fa3}
{:fa4 {fullAdder :a4 :b4 {car :fa3}}} }
{car :fa4} {cdr :fa4} {cdr :fa3} {cdr :fa2} {cdr :fa1}}}}}}}
-> 4bitsAdder
{def bin2bool
{lambda {:b}
{if {W.empty? {W.rest :b}}
then {= {W.first :b} 1}
else {= {W.first :b} 1} {bin2bool {W.rest :b}}}}}
-> bin2bool
{def bool2bin
{lambda {:b}
{if {S.empty? {S.rest :b}}
then {if {S.first :b} then 1 else 0}
else {if {S.first :b} then 1 else 0}{bool2bin {S.rest :b}}}}}
-> bool2bin
{def bin2dec
{def bin2dec.r
{lambda {:p :r}
{if {A.empty? :p}
then :r
else {bin2dec.r {A.rest :p} {+ {A.first :p} {* 2 :r}}}}}}
{lambda {:p} {bin2dec.r {A.split :p} 0}}}
-> bin2dec
{def add
{def numbers 0000 0001 0010 0011 0100 0101 0110 0111
1000 1001 1010 1011 1100 1101 1110 1111}
{lambda {:a :b}
{bin2dec
{bool2bin
{4bitsAdder {bin2bool {S.get :a {numbers}}}
{bin2bool {S.get :b {numbers}}}}}}}}
-> add
{table
{S.map {lambda {:i} {tr
{S.map {{lambda {:i :j} {td {add :i :j}}} :i}
{S.serie 0 15}}}}
{S.serie 0 15}}
}
->
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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>
=={{header|Lua}}==
<
function xor (a, b) return (a and not b) or (b and not a) end
Line 2,944 ⟶ 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)</
Output:
<pre>SUM OVERFLOW
Line 2,954 ⟶ 4,282:
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module FourBitAdder {
Flush
Line 3,014 ⟶ 4,342:
}
FourBitAdder
</syntaxhighlight>
=={{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 3,032 ⟶ 4,360:
{s2, c2} = fulladder[a2, b2, c1];
{s3, c3} = fulladder[a3, b3, c2];
{{s3, s2, s1, s0}, c3}];</
Example:
<syntaxhighlight lang="text">fourbitadder[{1, 0, 1, 0}, {1, 1, 1, 1}]</
Output:
<pre>{{1, 0, 0, 1}, 1}</pre>
Line 3,041 ⟶ 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.
<
%Make sure that only 4-Bit numbers are being added. This assumes that
Line 3,104 ⟶ 4,432:
v = num2str(v);
end
end %fourBitAdder</
Sample Usage:
<
S =
Line 3,160 ⟶ 4,488:
11
12</
=={{header|MUMPS}}==
<
QUIT (Y&'Z)!('Y&Z)
HALF(W,X)
Line 3,176 ⟶ 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</
Usage:<pre>USER>S N1="0110",N2="0010",C=0,T=$$FOUR^ADDER(N1,N2,C)
Line 3,189 ⟶ 4,517:
=={{header|MyHDL}}==
To interpret and run this code you will need a
The test code simulates the adder and exports trace wave file for debug support. Verilog and VHDL files are exported for hardware synthesis.
<syntaxhighlight lang="python">"""
To run:
python3 Four_bit_adder_011.py
"""
from myhdl import
# define set of primitives
@block
def NOTgate( a, q ): # define component name & interface
""" q <- not(a) """
@always_comb # define asynchronous logic
def
return NOTgateLogic # return defined logic function, named 'NOTgate'
@block
def ANDgate( a, b, q ):
""" q <- a and b """
@always_comb
def
return ANDgateLogic
@block
def ORgate( a, b, q ):
""" q <- a or b """
@always_comb
def
return ORgateLogic
# build components using defined primitive set
@block
def XORgate( a, b, q ):
""" q <- a xor b """
# define internal signals
nota, notb, annotb, bnnota = [Signal(bool(0)) for i in range(4)]
# name sub-components, and their interconnect
inv0 =
inv1 =
and2a =
and2b =
or2a =
return inv0, inv1, and2a, and2b, or2a
@block
def HalfAdder( in_a, in_b, summ, carry ):
""" carry,sum is the sum of in_a, in_b """
and2a =
xor2a
return and2a, xor2a
@block
def FullAdder( fa_c0, fa_a, fa_b, fa_s, fa_c1 ):
""" fa_c1,fa_s is the sum of fa_c0, fa_a, fa_b """
ha1_s, ha1_c1, ha2_c1 = [Signal(bool(0)) for i in range(3)]
HalfAdder01 = HalfAdder( fa_c0, fa_a, ha1_s, ha1_c1 )
HalfAdder02 = HalfAdder( ha1_s, fa_b, fa_s, ha2_c1 )
or2a = ORgate(ha1_c1, ha2_c1, fa_c1)
return HalfAdder01, HalfAdder02, or2a
@block
def Adder4b( ina, inb, cOut, sum4):
''' assemble 4 full adders '''
cl = [Signal(bool()) for i in range(0,4)] # carry signal list
sl = [Signal(bool()) for i in range(4)] # sum signal list
HalfAdder0 = HalfAdder( ina(0), inb(0), sl[0], cl[1] )
FullAdder1 = FullAdder( cl[1], ina(1), inb(1), sl[1], cl[2] )
FullAdder2 = FullAdder( cl[2], ina(2), inb(2), sl[2], cl[3] )
FullAdder3 = FullAdder( cl[3], ina(3), inb(3), sl[3], cOut )
sc = ConcatSignal(*reversed(sl)) # create internal bus for output list
@always_comb
def list2intbv():
sum4.next = sc # assign internal bus to actual output
return HalfAdder0, FullAdder1, FullAdder2, FullAdder3, list2intbv
""" define signals and code for testing
----------------------------------- """
t_co, t_s, t_a, t_b, dbug = [Signal(bool(0)) for i in range(5)]
ina4, inb4, sum4 = [Signal(intbv(0)[4:]) for i in range(3)]
from random import randrange
@block
def Test_Adder4b():
''' Test Bench for Adder4b '''
dut = Adder4b( ina4, inb4, t_co, sum4 )
@instance
def check():
print( "\n b a | c1 s \n -------------------" )
for i in range(15):
ina4.next, inb4.next = randrange(2**4), randrange(2**4)
yield delay(5)
print( " %2d %2d | %2d %2d " \
% (ina4,inb4, t_co,sum4) )
assert t_co * 16 + sum4 == ina4 + inb4 # test result
print()
return dut, check
""" instantiate components and run test
----------------------------------- """
def main():
simInst = Test_Adder4b()
simInst.name = "mySimInst"
simInst.config_sim(trace=True) # waveform trace turned on
simInst.run_sim(duration=None)
inst.convert(hdl='VHDL') # export VHDL
if __name__ == '__main__':
main()
</syntaxhighlight>
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="nim">type
Bools[N: static int] = array[N, bool]
SumCarry = tuple[sum, carry: bool]
proc ha(a, b: bool): SumCarry = (a xor b, a and b)
proc fa(a, b, ci: bool):
let a = ha(ci, a)
let b = ha(a[0], b)
proc fa4(a, b: Bools[4]):
var co, s:
for i in 0..3:
let r = fa(a[i], b[i], if i > 0: co[i-1] else: false)
Line 3,415 ⟶ 4,687:
result[4] = co[3]
proc int2bus(n: int):
var n = n
for i in 0..result.high:
Line 3,421 ⟶ 4,693:
n = n shr 1
proc bus2int(b: Bools): int =
for i, x in b:
result += (if x: 1 else: 0) shl i
for a in 0..7:
for b in 0..7:
assert a + b == bus2int fa4(int2bus(a), int2bus(b))</
=={{header|OCaml}}==
<
(* File blocks.ml
Line 3,633 ⟶ 4,905:
(eval add4_io 4 4 (Array.map Int64.of_int [| a; b |])) in
v.(0), v.(1);;
</syntaxhighlight>
Testing
<
# open Blocks;;
Line 3,651 ⟶ 4,923:
# plus 0 0;;
- : int * int = (0, 0)
</syntaxhighlight>
An extension : n-bit adder, for n <= 64 (n could be greater, but we use Int64 for I/O)
<
(* general adder (n bits with n <= 64) *)
let gen_adder n = block_array serial [|
Line 3,670 ⟶ 4,942:
(eval (gadd_io n) n n (Array.map Int64.of_int [| a; b |])) in
v.(0), v.(1);;
</syntaxhighlight>
And a test
<
# gen_plus 7 100 100;;
- : int * int = (72, 1)
# gen_plus 8 100 100;;
- : int * int = (200, 0)
</syntaxhighlight>
=={{header|PARI/GP}}==
<
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 3,693 ⟶ 4,965:
[s3[1],s3[2],s2[2],s1[2],s0[2]]
};
add4(0,0,0,0,0,0,0,0)</
=={{header|Perl}}==
<
sub bin2dec { oct "0b".shift }
sub bin2bits { reverse split(//, substr(shift,0,shift)); }
Line 3,738 ⟶ 5,010:
$a, $b, $abin, $bbin, $c, $s, bin2dec($c.$s);
}
}</
Output matches the [[Four bit adder#Ruby|Ruby]] output.
=={{header|Phix}}==
<!--<syntaxhighlight lang="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>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">a</span> <span style="color: #008080;">and</span> <span style="color: #008080;">not</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #008080;">not</span> <span style="color: #000000;">a</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">half_adder</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>
<span style="color: #004080;">bool</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">xor_gate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">full_adder</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> <span style="color: #004080;">bool</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">bool</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c1</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">half_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">),</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">half_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c1</span> <span style="color: #008080;">or</span> <span style="color: #000000;">c2</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">four_bit_adder</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">a0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b3</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">s0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">s0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">full_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">full_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">full_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">s3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">full_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">bool</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a3</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">int_to_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">),</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">b0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b3</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">int_to_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">),</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">r3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">four_bit_adder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b3</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">bits_to_int</span><span style="color: #0000FF;">({</span><span style="color: #000000;">r0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r3</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" [=%d+16]"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">):</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%04b + %04b = %04b %b (%d+%d=%d%s)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">*</span><span style="color: #000000;">16</span><span style="color: #0000FF;">+</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1111</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1111</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1100</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1101</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1110</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0b1100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0b1111</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;">0b0000</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;">0b0001</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;">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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
0000 + 0000 = 0000 0 (0+0=0)
0000 + 0001 = 0001 0 (0+1=1)
1111 + 1111 = 1110 1 (15+15=30 [=14+16])
0011 + 0111 = 1010 0 (3+7=10)
1011 + 1000 = 0011 1 (11+8=19 [=3+16])
1100 + 1100 = 1000 1 (12+12=24 [=8+16])
1100 + 1101 = 1001 1 (12+13=25 [=9+16])
1100 + 1110 = 1010 1 (12+14=26 [=10+16])
1100 + 1111 = 1011 1 (12+15=27 [=11+16])
1101 + 0000 = 1101 0 (13+0=13)
1101 + 0001 = 1110 0 (13+1=14)
1101 + 0010 = 1111 0 (13+2=15)
1101 + 0011 = 0000 1 (13+3=16 [=0+16])
</pre>
=={{header|PicoLisp}}==
<
(cons
(and A B)
Line 3,865 ⟶ 5,106:
(cdr Fa3)
(cdr Fa2)
(cdr Fa1) ) ) )</
{{out}}
<pre>: (4bitsAdder NIL NIL NIL T NIL NIL NIL T)
Line 3,880 ⟶ 5,121:
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
/* 4-BIT ADDER */
Line 3,916 ⟶ 5,157:
END TEST;
</syntaxhighlight>
=={{header|PowerShell}}==
===Using Bytes as Inputs===
<
{
$out1 = $a -band ( -bnot $b )
Line 3,970 ⟶ 5,212:
FourBitAdder 0xC 0xB
[Convert]::ToByte((FourBitAdder 0xC 0xB)["S"],2)</
===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">
$source = @'
using System;
Line 4,106 ⟶ 5,348:
Add-Type -TypeDefinition $source -Language CSharpVersion3
</syntaxhighlight>
<syntaxhighlight lang="powershell">
[RosettaCodeTasks.FourBitAdder.ConstructiveBlocks]::Test()
</syntaxhighlight>
{{Out}}
<pre>
Line 4,123 ⟶ 5,365:
1111 + 1111 = 1110c1
</pre>
=={{header|Prolog}}==
Using hi/lo symbols to represent binary. As this is a simulation, there is no real "arithmetic" happening.
<
b_not(in(hi), out(lo)) :- !. % not(1) = 0
Line 4,166 ⟶ 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').</
<pre>?- go.
in(hi,lo,lo,lo) + in(hi,lo,lo,lo) is out(lo,hi,lo,lo) c(lo) (1 + 1 = 2)
Line 4,177 ⟶ 5,420:
=={{header|PureBasic}}==
<
;Output values from the constructive building blocks is done using pointers (i.e. '*').
Line 4,230 ⟶ 5,473:
Input()
CloseConsole()
EndIf</
{{out}}
<pre>0110 + 1110 = 0100 overflow 1</pre>
Line 4,252 ⟶ 5,495:
between the normal Python values and those of the simulation.
<
def ha(a, b): return xor(a, b), a and b # sum, carry
Line 4,286 ⟶ 5,529:
if __name__ == '__main__':
test_fa4()</
=={{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}}==
<
(define (adder-and a b)
Line 4,334 ⟶ 5,672:
(cons v 4s))))
(n-bit-adder '(1 0 1 0) '(0 1 1 1)) ;-> '(1 0 0 0 1)</
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub xor ($a, $b) { (($a and not $b) or (not $a and $b)) ?? 1 !! 0 }
sub half-adder ($a, $b) {
return xor($a, $b), ($a and $b);
}
sub full-adder ($a, $b, $c0) {
my ($ha0_s, $ha0_c) = half-adder($c0, $a);
my ($ha1_s, $ha1_c) = half-adder($ha0_s, $b);
return $ha1_s, ($ha0_c or $ha1_c);
}
sub four-bit-adder ($a0, $a1, $a2, $a3, $b0, $b1, $b2, $b3) {
my ($fa0_s, $fa0_c) = full-adder($a0, $b0, 0);
my ($fa1_s, $fa1_c) = full-adder($a1, $b1, $fa0_c);
my ($fa2_s, $fa2_c) = full-adder($a2, $b2, $fa1_c);
my ($fa3_s, $fa3_c) = full-adder($a3, $b3, $fa2_c);
return $fa0_s, $fa1_s, $fa2_s, $fa3_s, $fa3_c;
}
{
use Test;
is four-bit-adder(1, 0, 0, 0, 1, 0, 0, 0), (0, 1, 0, 0, 0), '1 + 1 == 2';
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';
}</syntaxhighlight>
{{out}}
<pre>ok 1 - 1 + 1 == 2
ok 2 - 5 + 5 == 10
ok 3 - 7 + 9 == overflow</pre>
=={{header|REXX}}==
Line 4,343 ⟶ 5,717:
::::* the '''|''' symbol is a logical '''OR'''.
::::* the '''&''' symbol is a logical '''AND'''.
<
call hdr1;
/* [↓] traipse thru all possibilities.*/
do j=0 for 16
do m=0 for 4; a.m= bit(j, m)
end /*m*/
do k=0 for 16
do m=0 for 4; b.m= bit(k, m)
end /*m*/
sc= 4bitAdder(a., b.)
say translate( space(z, 0), , '~') /*translate tildes (~) to blanks in Z. */
end /*k*/
end /*j*/
call hdr2;
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
bit: procedure; parse arg x,y; return substr( reverse( x2b( d2x(x) ) ), y+1, 1)
halfAdder: procedure expose c; parse arg x,y; c= x & y; return x && y
hdr1: say 'aaaa + bbbb = c, sum [c=carry]'; return
hdr2: say '════ ════ ══════' ; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
fullAdder: procedure expose c; parse arg x,y,fc
/*──────────────────────────────────────────────────────────────────────────────────────*/
4bitAdder: procedure expose s. a. b.; carry.= 0
end /*j*/;
{{out|output|text= (most lines have been elided):}}
<pre style="height:63ex">
aaaa + bbbb = c, sum [c=carry]
Line 4,445 ⟶ 5,820:
{{Full One-Bit-Adder function is made up of XOR OR AND gates}}
<
###---------------------------
Line 4,517 ⟶ 5,892:
###------------------------
</syntaxhighlight>
Output:
<pre>
Line 4,529 ⟶ 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}}==
<
def four_bit_adder(a, b)
a_bits = binary_string_to_bits(a,4)
Line 4,587 ⟶ 6,044:
[a, b, bin_a, bin_b, carry, sum, (carry + sum).to_i(2)]
end
end</
{{out}}
Line 4,611 ⟶ 6,068:
=={{header|Rust}}==
<
// half adder with XOR and AND
// SUM = A XOR B
Line 4,676 ⟶ 6,133:
}
</syntaxhighlight>
=={{header|Sather}}==
<
-- that "sets" it to 0 or 1, while it can be "read"
-- ad libitum. (Tristate logic is not taken into account)
Line 4,815 ⟶ 6,273:
", overflow = " + fba.v.val + "\n";
end;
end;</
=={{header|Scala}}==
<
type Nibble=(Boolean, Boolean, Boolean, Boolean)
Line 4,919 ⟶ 6,301:
((s3, s2, s1, s0), cOut)
}
}</
A test program using the object above.
<
import FourBitAdder._
def main(args: Array[String]): Unit = {
Line 4,936 ⟶ 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)
}</
{{out}}
<pre> A B S C
Line 4,955 ⟶ 6,337:
{{trans|Common Lisp}}
<
(import (scheme base)
(scheme write)
Line 4,989 ⟶ 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>
{{out}}
Line 5,000 ⟶ 6,382:
(1 0 1 0) + (0 1 0 1) = ((1 1 1 1) 0)
</pre>
=={{header|Sed}}==
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
<syntaxhighlight lang="sed">
#!/bin/sed -f
# (C) 2005,2014 by Mariusz Woloszyn :)
# https://en.wikipedia.org/wiki/Adder_(electronics)
##############################
# PURE SED BINARY FULL ADDER #
##############################
# Input two lines, sanitize input
N
s/ //g
/^[01 ]\+\n[01 ]\+$/! {
i\
ERROR: WRONG INPUT DATA
d
q
}
s/[ ]//g
# Add place for Sum and Cary bit
s/$/\n\n0/
:LOOP
# Pick A,B and C bits and put that to hold
s/^\(.*\)\(.\)\n\(.*\)\(.\)\n\(.*\)\n\(.\)$/0\1\n0\3\n\5\n\6\2\4/
h
# Grab just A,B,C
s/^.*\n.*\n.*\n\(...\)$/\1/
# binary full adder module
# INPUT: 3bits (A,B,Carry in), for example 101
# OUTPUT: 2bits (Carry, Sum), for wxample 10
s/$/;000=00001=01010=01011=10100=01101=10110=10111=11/
s/^\(...\)[^;]*;[^;]*\1=\(..\).*/\2/
# Append the sum to hold
H
# Rewrite the output, append the sum bit to final sum
g
s/^\(.*\)\n\(.*\)\n\(.*\)\n...\n\(.\)\(.\)$/\1\n\2\n\5\3\n\4/
# Output result and exit if no more bits to process..
/^\([0]*\)\n\([0]*\)\n/ {
s/^.*\n.*\n\(.*\)\n\(.\)/\2\1/
s/^0\(.*\)/\1/
q
}
b LOOP</syntaxhighlight>
Example usage:
<syntaxhighlight lang="sed">
./binAdder.sed
1111110111
1
1111111000
./binAdder.sed
10
10001
10011
./binAdder.sed
0 1 1 0
0 0 0 1
111
</syntaxhighlight>
=={{header|Sidef}}==
{{trans|Perl}}
<
(~a & b) | (a & ~b)
}
Line 5,033 ⟶ 6,491:
a, b, abin.join, bbin.join, c, s, "#{c}#{s}".bin)
}
}</
{{out}}
Line 5,059 ⟶ 6,517:
=={{header|Swift}}==
<
func halfAdder(_ a: Int, _ b: Int) -> (Int, Int) {
Line 5,086 ⟶ 6,544:
print("\(a) + \(b) = \(fourBitAdder(a, b))")
</syntaxhighlight>
{{out}}
Line 5,094 ⟶ 6,552:
=={{header|SystemVerilog}}==
In SystemVerilog we can define a multibit adder as a parameterized module, that instantiates the components:
<syntaxhighlight lang="systemverilog">
module Half_Adder( input a, b, output s, c );
assign s = a ^ b;
Line 5,128 ⟶ 6,586:
endmodule
</syntaxhighlight>
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">
module simTop();
Line 5,164 ⟶ 6,622:
endprogram
</syntaxhighlight>
{{out}}
Line 5,198 ⟶ 6,656:
=={{header|Tcl}}==
This example shows how you can make little languages in Tcl that describe the problem space.
<
# Create our little language
Line 5,262 ⟶ 6,720:
fulladd a2 b2 c2 s2 c3
fulladd a3 b3 c3 s3 v
}</
<
proc 4add_driver {a b} {
lassign [split $a {}] a3 a2 a1 a0
Line 5,276 ⟶ 6,734:
set a 1011
set b 0110
puts $a+$b=[4add_driver $a $b]</
{{out}}
<pre>
Line 5,286 ⟶ 6,744:
=={{header|TorqueScript}}==
<
{
return (!%a && %b) || (%a && !%b);
Line 5,314 ⟶ 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);
}</
=={{header|UNIX Shell}}==
{{trans|Raku}}
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
{{works with|Z Shell}}
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.
<syntaxhighlight lang="sh">xor() {
typeset -i a=$1 b=$2
printf '%d\n' $(( (a || b) && ! (a && b) ))
}
half_adder() {
typeset -i a=$1 b=$2
printf '%d %d\n' $(xor $a $b) $(( a && b ))
}
full_adder() {
typeset -i a=$1 b=$2 c=$3
typeset -i ha0_s ha0_c ha1_s ha1_c
read ha0_s ha0_c < <(half_adder "$c" "$a")
read ha1_s ha1_c < <(half_adder "$ha0_s" "$b")
printf '%d %d\n' "$ha1_s" "$(( ha0_c || ha1_c ))"
}
four_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
read fa0_s fa0_c < <(full_adder "$a0" "$b0" 0)
read fa1_s fa1_c < <(full_adder "$a1" "$b1" "$fa0_c")
read fa2_s fa2_c < <(full_adder "$a2" "$b2" "$fa1_c")
read fa3_s fa3_c < <(full_adder "$a3" "$b3" "$fa2_c")
printf '%s' "$fa0_s"
printf ' %s' "$fa1_s" "$fa2_s" "$fa3_s" "$fa3_c"
printf '\n'
}
is() {
typeset label=$1
shift
if eval "$*"; then
printf 'ok'
else
printf 'not ok'
fi
printf ' %s\n' "$label"
}
is "1 + 1 = 2" "[[ '$(four_bit_adder 1 0 0 0 1 0 0 0)' == '0 1 0 0 0' ]]"
is "5 + 5 = 10" "[[ '$(four_bit_adder 1 0 1 0 1 0 1 0)' == '0 1 0 1 0' ]]"
is "7 + 9 = overflow" "a=($(four_bit_adder 1 0 0 1 1 1 1 0)); (( \${a[-1]}==1 ))"
</syntaxhighlight>
{{Out}}
<pre>ok 1 + 1 = 2
ok 5 + 5 = 10
ok 7 + 9 = overflow</pre>
=={{header|Verilog}}==
In Verilog we can also define a multibit adder as a component with multiple instances:
<syntaxhighlight lang="verilog">
module Half_Adder( output c, s, input a, b );
xor xor01 (s, a, b);
Line 5,367 ⟶ 6,885:
endmodule // test_Full_Adder
</syntaxhighlight>
{{out}}
Line 5,382 ⟶ 6,900:
=={{header|VHDL}}==
The following is a direct implementation of the proposed schematic:
<
USE ieee.std_logic_1164.all;
Line 5,542 ⟶ 7,060:
begin
x <= (a and not b) or (b and not a);
end architecture rtl;</
An exhaustive testbench:
<
USE ieee.std_logic_1164.all;
use ieee.NUMERIC_STD.all;
Line 5,590 ⟶ 7,108:
);
end struct;</
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="wren">var xor = Fn.new { |a, b| a&(~b) | b&(~a) }
var ha = Fn.new { |a, b| [xor.call(a, b), a & b] }
var fa = Fn.new { |a, b, c0|
var res = ha.call(a, c0)
var sa = res[0]
var ca = res[1]
res = ha.call(sa, b)
return [res[0], ca | res[1]]
}
var add4 = Fn.new { |a3, a2, a1, a0, b3, b2, b1, b0|
var res = fa.call(a0, b0, 0)
var s0 = res[0]
var c0 = res[1]
res = fa.call(a1, b1, c0)
var s1 = res[0]
var c1 = res[1]
res = fa.call(a2, b2, c1)
var s2 = res[0]
var c2 = res[1]
res = fa.call(a3, b3, c2)
return [res[1], res[0], s2, s1, s0]
}
System.print(add4.call(1, 0, 1, 0, 1, 0, 0, 1))</syntaxhighlight>
{{out}}
<pre>
[1, 0, 0, 1, 1]
</pre>
=={{header|XPL0}}==
<
func Not(A);
Line 5,651 ⟶ 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);
]</
{{out}}
Line 5,661 ⟶ 7,214:
=={{header|zkl}}==
<
{ a.bitAnd(b.bitNot()).bitOr(b.bitAnd(a.bitNot())) }
Line 5,684 ⟶ 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));</
<
(ss:=List()).append(
[as.len()-1 .. 0,-1].reduce('wrap(c,n){
Line 5,692 ⟶ 7,245:
.reverse();
}
println(nBitAddr(T(1,0,1,0), T(1,0,0,1)));</
{{out}}
<pre>
|