Jump to content

Four bit adder: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Added 11l)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 522:
</pre>
--[[User:BugFix|BugFix]] ([[User talk:BugFix|talk]]) 17:10, 14 November 2013 (UTC)
 
=={{header|BASIC}}==
 
==={{header|Applesoft BASIC}}===
 
<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</lang>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<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&</lang>
{{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}}==
Line 667 ⟶ 791:
Second 4-Bit Binary Number: 0111
1011 + 0111 = 10010
</pre>
 
=={{header|BASIC}}==
 
==={{header|Applesoft BASIC}}===
 
<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</lang>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<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&</lang>
{{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>
 
Line 864:
return 0;
}</lang>
 
=={{header|C++}}==
See [[Four bit adder/C++]]
 
=={{header|C sharp|C#}}==
Line 1,001 ⟶ 998:
 
</lang>
 
=={{header|C++}}==
See [[Four bit adder/C++]]
 
=={{header|Clojure}}==
Line 1,703:
28> four_bit_adder:task().
{[0,1,0,1],0}
</pre>
 
=={{header|F#}}==
<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"
</lang>
{{out}}
<pre>
0001 + 0111 =
(Zero, One, Zero, Zero,
</pre>
 
Line 1,810 ⟶ 1,855:
1101 + 0010 = 01111
1101 + 0011 = 10000</pre>
 
=={{header|F#}}==
<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"
</lang>
{{out}}
<pre>
0001 + 0111 =
(Zero, One, Zero, Zero,
</pre>
 
=={{header|Go}}==
Line 2,185:
1111 + 1001 = 1:1000
1111 + 1111 = 1:1110</pre>
 
 
=={{header|J}}==
Line 2,269 ⟶ 2,268:
 
See also: "A Formal Description of System/360” by Adin Falkoff
 
 
=={{header|Java}}==
Line 2,550 ⟶ 2,548:
}
</lang>
 
 
=={{header|jq}}==
Line 3,741 ⟶ 3,738:
 
Output matches the [[Four bit adder#Ruby|Ruby]] output.
 
=={{header|Perl 6}}==
<lang perl6>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';
}</lang>
 
{{out}}
<pre>ok 1 - 1 + 1 == 2
ok 2 - 5 + 5 == 10
ok 3 - 7 + 9 == overflow</pre>
 
=={{header|Phix}}==
Line 3,917 ⟶ 3,879:
END TEST;
</lang>
 
=={{header|PowerShell}}==
===Using Bytes as Inputs===
Line 4,123 ⟶ 4,086:
1111 + 1111 = 1110c1
</pre>
 
=={{header|Prolog}}==
Using hi/lo symbols to represent binary. As this is a simulation, there is no real "arithmetic" happening.
Line 4,334 ⟶ 4,298:
(cons v 4s))))
 
(n-bit-adder '(1 0 1 0) '(0 1 1 1)) ;-> '(1 0 0 0 1)</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>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';
}</lang>
 
{{out}}
<pre>ok 1 - 1 + 1 == 2
ok 2 - 5 + 5 == 10
ok 3 - 7 + 9 == overflow</pre>
 
=={{header|REXX}}==
Line 4,677:
 
</lang>
 
=={{header|Sather}}==
<lang sather>-- a "pin" can be connected only to one component
Line 4,816 ⟶ 4,817:
end;
end;</lang>
 
=={{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
<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</lang>
 
Example usage:
 
<lang sed>
./binAdder.sed
1111110111
1
1111111000
 
./binAdder.sed
10
10001
10011
 
./binAdder.sed
0 1 1 0
0 0 0 1
111
</lang>
 
=={{header|Scala}}==
Line 5,000 ⟶ 4,925:
(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
<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</lang>
 
Example usage:
 
<lang sed>
./binAdder.sed
1111110111
1
1111111000
 
./binAdder.sed
10
10001
10011
 
./binAdder.sed
0 1 1 0
0 0 0 1
111
</lang>
 
=={{header|Sidef}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.