Four bit adder: Difference between revisions
Content added Content deleted
m (→{{header|UNIX Shell}}: FIx spelling of "adder.") |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 43: | Line 43: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F xor(a, b) |
||
R (a & !b) | (b & !a) |
R (a & !b) | (b & !a) |
||
Line 77: | Line 77: | ||
tot[i] = ta[i] |
tot[i] = ta[i] |
||
tot[width] = tlast |
tot[width] = tlast |
||
assert(a + b == bus2int(tot), ‘totals don't match: #. + #. != #.’.format(a, b, String(tot)))</ |
assert(a + b == bus2int(tot), ‘totals don't match: #. + #. != #.’.format(a, b, String(tot)))</syntaxhighlight> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">DEFINE Bit="BYTE" |
||
TYPE FourBit=[Bit b0,b1,b2,b3] |
TYPE FourBit=[Bit b0,b1,b2,b3] |
||
Line 141: | Line 141: | ||
PrintBE(c) |
PrintBE(c) |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Four_bit_adder.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Four_bit_adder.png Screenshot from Atari 8-bit computer] |
||
Line 168: | Line 168: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<syntaxhighlight lang="ada"> |
|||
<lang Ada> |
|||
type Four_Bits is array (1..4) of Boolean; |
type Four_Bits is array (1..4) of Boolean; |
||
Line 192: | Line 192: | ||
Full_Adder (A (1), B (1), C (1), Carry); |
Full_Adder (A (1), B (1), C (1), Carry); |
||
end Four_Bits_Adder; |
end Four_Bits_Adder; |
||
</syntaxhighlight> |
|||
</lang> |
|||
A test program with the above definitions |
A test program with the above definitions |
||
<syntaxhighlight lang="ada"> |
|||
<lang Ada> |
|||
with Ada.Text_IO; use Ada.Text_IO; |
with Ada.Text_IO; use Ada.Text_IO; |
||
Line 247: | Line 247: | ||
end loop; |
end loop; |
||
end Test_4_Bit_Adder; |
end Test_4_Bit_Adder; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<div style="height: 320px;overflow:scroll"> |
<div style="height: 320px;overflow:scroll"> |
||
Line 513: | Line 513: | ||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
{{works with|GNU 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. |
not ← { ~ ⍵ } ⍝ in Dyalog these assignments can be simplified to "not ← ~", "and ← ∧", etc. |
||
and ← { ⍺ ∧ ⍵ } |
and ← { ⍺ ∧ ⍵ } |
||
Line 553: | Line 553: | ||
⍝ And go |
⍝ And go |
||
{ (randbits 4) demo randbits 4 ⊣ ⍵ } ¨ ⍳20 |
{ (randbits 4) demo randbits 4 ⊣ ⍵ } ¨ ⍳20 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 580: | Line 580: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{works with|AutoHotkey 1.1}} |
{{works with|AutoHotkey 1.1}} |
||
< |
<syntaxhighlight lang="autohotkey">A := 13 |
||
B := 9 |
B := 9 |
||
N := FourBitAdd(A, B) |
N := FourBitAdd(A, B) |
||
Line 621: | Line 621: | ||
Res := Mod(N, 2) Res, N := N >> 1 |
Res := Mod(N, 2) Res, N := N >> 1 |
||
return, Res |
return, Res |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>13 + 9: |
<pre>13 + 9: |
||
Line 628: | Line 628: | ||
=={{header|AutoIt}}== |
=={{header|AutoIt}}== |
||
===Functions=== |
===Functions=== |
||
<syntaxhighlight lang="autoit"> |
|||
<lang AutoIt> |
|||
Func _NOT($_A) |
Func _NOT($_A) |
||
Return (Not $_A) *1 |
Return (Not $_A) *1 |
||
Line 669: | Line 669: | ||
Return $Q4 & $Q3 & $Q2 & $Q1 |
Return $Q4 & $Q3 & $Q2 & $Q1 |
||
EndFunc ;==>_4BitAdder |
EndFunc ;==>_4BitAdder |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Example=== |
===Example=== |
||
<syntaxhighlight lang="autoit"> |
|||
<lang AutoIt> |
|||
Local $CarryOut, $sResult |
Local $CarryOut, $sResult |
||
$sResult = _4BitAdder(0, 0, 1, 1, 0, 1, 1, 1, 0, $CarryOut) ; adds 3 + 7 |
$sResult = _4BitAdder(0, 0, 1, 1, 0, 1, 1, 1, 0, $CarryOut) ; adds 3 + 7 |
||
Line 678: | Line 678: | ||
$sResult = _4BitAdder(1, 0, 1, 1, 1, 0, 0, 0, 0, $CarryOut) ; adds 11 + 8 |
$sResult = _4BitAdder(1, 0, 1, 1, 1, 0, 0, 0, 0, $CarryOut) ; adds 11 + 8 |
||
ConsoleWrite('result: ' & $sResult & ' ==> carry out: ' & $CarryOut & @LF) |
ConsoleWrite('result: ' & $sResult & ' ==> carry out: ' & $CarryOut & @LF) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 690: | Line 690: | ||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang="basic">100 S$ = "1100 + 1100 = " : GOSUB 400 |
||
110 S$ = "1100 + 1101 = " : GOSUB 400 |
110 S$ = "1100 + 1101 = " : GOSUB 400 |
||
120 S$ = "1100 + 1110 = " : GOSUB 400 |
120 S$ = "1100 + 1110 = " : GOSUB 400 |
||
Line 749: | Line 749: | ||
910 D = B AND NOT A |
910 D = B AND NOT A |
||
920 C = C OR D |
920 C = C OR D |
||
930 RETURN</ |
930 RETURN</syntaxhighlight> |
||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> @% = 2 |
||
PRINT "1100 + 1100 = "; |
PRINT "1100 + 1100 = "; |
||
PROC4bitadd(1,1,0,0, 1,1,0,0, e,d,c,b,a) : PRINT e,d,c,b,a |
PROC4bitadd(1,1,0,0, 1,1,0,0, e,d,c,b,a) : PRINT e,d,c,b,a |
||
Line 797: | Line 797: | ||
c& = a& AND NOT b& |
c& = a& AND NOT b& |
||
d& = b& AND NOT a& |
d& = b& AND NOT a& |
||
= c& OR d&</ |
= c& OR d&</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 811: | Line 811: | ||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos"> |
||
@echo off |
@echo off |
||
setlocal enabledelayedexpansion |
setlocal enabledelayedexpansion |
||
Line 948: | Line 948: | ||
if %1==1 if %2==1 exit /b 1 |
if %1==1 if %2==1 exit /b 1 |
||
exit /b 0 |
exit /b 0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 957: | Line 957: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
typedef char pin_t; |
typedef char pin_t; |
||
Line 1,026: | Line 1,026: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{works with|C sharp|C#|3+}} |
{{works with|C sharp|C#|3+}} |
||
< |
<syntaxhighlight lang="csharp"> |
||
using System; |
using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 1,160: | Line 1,160: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 1,166: | Line 1,166: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure"> |
||
(ns rosettacode.adder |
(ns rosettacode.adder |
||
(:use clojure.test)) |
(:use clojure.test)) |
||
Line 1,201: | Line 1,201: | ||
(n-bit-adder [true true true true true true] [true true true true true true]) |
(n-bit-adder [true true true true true true] [true true true true true true]) |
||
=> (false true true true true true true) |
=> (false true true true true true true) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Second Clojure solution=== |
===Second Clojure solution=== |
||
< |
<syntaxhighlight lang="clojure">(ns rosetta.fourbit) |
||
;; a bit is represented as a boolean (true/false) |
;; a bit is represented as a boolean (true/false) |
||
Line 1,256: | Line 1,256: | ||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Using Bitwise Operators=== |
===Using Bitwise Operators=== |
||
< |
<syntaxhighlight lang="clojure"> |
||
(defn to-binary-seq [^long x] |
(defn to-binary-seq [^long x] |
||
(map #(- (int %) (int \0)) |
(map #(- (int %) (int \0)) |
||
Line 1,293: | Line 1,293: | ||
(is (= (Long/parseLong (apply str (ripple-carry-adder (to-binary-seq 130) (to-binary-seq 250))) 2) |
(is (= (Long/parseLong (apply str (ripple-carry-adder (to-binary-seq 130) (to-binary-seq 250))) 2) |
||
(+ 130 250)))) |
(+ 130 250)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
<syntaxhighlight lang="cobol"> |
|||
<lang COBOL> |
|||
program-id. test-add. |
program-id. test-add. |
||
environment division. |
environment division. |
||
Line 1,405: | Line 1,405: | ||
end program add-4b. |
end program add-4b. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,420: | Line 1,420: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="coffeescript"> |
||
# ATOMIC GATES |
# ATOMIC GATES |
||
not_gate = (bit) -> |
not_gate = (bit) -> |
||
Line 1,460: | Line 1,460: | ||
adder = n_bit_adder(4) |
adder = n_bit_adder(4) |
||
console.log adder [1, 0, 1, 0], [0, 1, 1, 0] |
console.log adder [1, 0, 1, 0], [0, 1, 1, 0] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">;; returns a list of bits: '(sum carry) |
||
(defun half-adder (a b) |
(defun half-adder (a b) |
||
(list (logxor a b) (logand a b))) |
(list (logxor a b) (logand a b))) |
||
Line 1,495: | Line 1,495: | ||
(main) |
(main) |
||
</syntaxhighlight> |
|||
</lang> |
|||
output: |
output: |
||
<pre> |
<pre> |
||
Line 1,508: | Line 1,508: | ||
=={{header|D}}== |
=={{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. |
From the C version. An example of SWAR (SIMD Within A Register) code, that performs 32 (or 64) 4-bit adds in parallel. |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.traits; |
||
void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3, |
void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3, |
||
Line 1,574: | Line 1,574: | ||
writefln(" s0 %032b", s0); |
writefln(" s0 %032b", s0); |
||
writefln("overflow %032b", overflow); |
writefln("overflow %032b", overflow); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> a3 00000000000000000000000000000000 |
<pre> a3 00000000000000000000000000000000 |
||
Line 1,592: | Line 1,592: | ||
overflow 11111111111111111111111111111111</pre> |
overflow 11111111111111111111111111111111</pre> |
||
128 4-bit adds in parallel: |
128 4-bit adds in parallel: |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.traits, core.simd; |
||
void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3, |
void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3, |
||
Line 1,657: | Line 1,657: | ||
writefln(" s0 %(%08b%)", s0.array); |
writefln(" s0 %(%08b%)", s0.array); |
||
writefln("overflow %(%08b%)", overflow.array); |
writefln("overflow %(%08b%)", overflow.array); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> a3 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
<pre> a3 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
||
Line 1,675: | Line 1,675: | ||
overflow 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</pre> |
overflow 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</pre> |
||
Compiled by the ldc2 compiler to (where T = ubyte32, 256 adds using AVX2): |
Compiled by the ldc2 compiler to (where T = ubyte32, 256 adds using AVX2): |
||
< |
<syntaxhighlight lang="asm">fourBitsAdder: |
||
pushl %ebp |
pushl %ebp |
||
movl %esp, %ebp |
movl %esp, %ebp |
||
Line 1,713: | Line 1,713: | ||
popl %ebp |
popl %ebp |
||
vzeroupper |
vzeroupper |
||
ret $160</ |
ret $160</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
{{libheader| System.SysUtils}} |
{{libheader| System.SysUtils}} |
||
{{Trans|C#}} |
{{Trans|C#}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Four_bit_adder; |
program Four_bit_adder; |
||
Line 1,896: | Line 1,896: | ||
Readln; |
Readln; |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,159: | Line 2,159: | ||
{{works with|Elixir|1.1}} |
{{works with|Elixir|1.1}} |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir">defmodule RC do |
||
use Bitwise |
use Bitwise |
||
@bit_size 4 |
@bit_size 4 |
||
Line 2,205: | Line 2,205: | ||
end |
end |
||
RC.task</ |
RC.task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,231: | Line 2,231: | ||
=={{header|Erlang}}== |
=={{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. |
Yes, it is misleading to have a "choose your own number of bits" adder in the four_bit_adder module. But it does make it easier to find the module from the Rosettacode task name. |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( four_bit_adder ). |
-module( four_bit_adder ). |
||
Line 2,302: | Line 2,302: | ||
%% xor exists, this is another implementation. |
%% xor exists, this is another implementation. |
||
z_xor( A, B ) -> (A band (2+bnot B)) bor ((2+bnot A) band B). |
z_xor( A, B ) -> (A band (2+bnot B)) bor ((2+bnot A) band B). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,310: | Line 2,310: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
type Bit = |
type Bit = |
||
| Zero |
| Zero |
||
Line 2,347: | Line 2,347: | ||
printfn "0001 + 0111 =" |
printfn "0001 + 0111 =" |
||
b4A (Zero, Zero, Zero, One) (Zero, One, One, One) |> printfn "%A" |
b4A (Zero, Zero, Zero, One) (Zero, One, One, One) |> printfn "%A" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,355: | Line 2,355: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: "NOT" invert 1 and ; |
||
: "XOR" over over "NOT" and >r swap "NOT" and r> or ; |
: "XOR" over over "NOT" and >r swap "NOT" and r> or ; |
||
: halfadder over over and >r "XOR" r> ; |
: halfadder over over and >r "XOR" r> ; |
||
Line 2,366: | Line 2,366: | ||
; |
; |
||
: .add4 4bitadder 0 .r 4 0 do i 3 - abs roll 0 .r loop cr ;</ |
: .add4 4bitadder 0 .r 4 0 do i 3 - abs roll 0 .r loop cr ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 1 0 0 0 0 1 1 .add4 01111 |
<pre>1 1 0 0 0 0 1 1 .add4 01111 |
||
Line 2,373: | Line 2,373: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">module logic |
||
implicit none |
implicit none |
||
Line 2,449: | Line 2,449: | ||
end do |
end do |
||
end do |
end do |
||
end program</ |
end program</syntaxhighlight> |
||
{{out}} (selected) |
{{out}} (selected) |
||
<pre>1100 + 1100 = 11000 |
<pre>1100 + 1100 = 11000 |
||
Line 2,469: | Line 2,469: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">sub half_add( byval a as ubyte, byval b as ubyte,_ |
||
byref s as ubyte, byref c as ubyte) |
byref s as ubyte, byref c as ubyte) |
||
s = a xor b |
s = a xor b |
||
Line 2,506: | Line 2,506: | ||
print "1111 + 1111 = "; |
print "1111 + 1111 = "; |
||
fourbit_add( 1, 1, 1, 1, 1, 1, 1, 1, s3, s2, s1, s0, carry ) |
fourbit_add( 1, 1, 1, 1, 1, 1, 1, 1, s3, s2, s1, s0, carry ) |
||
print carry;s3;s2;s1;s0</ |
print carry;s3;s2;s1;s0</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
1100 + 0011 = 01111 |
1100 + 0011 = 01111 |
||
Line 2,517: | Line 2,517: | ||
Go does not have a bit type, so byte is used. |
Go does not have a bit type, so byte is used. |
||
This is the straightforward solution using bytes and functions. |
This is the straightforward solution using bytes and functions. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 2,547: | Line 2,547: | ||
// add 10+9 result should be 1 0 0 1 1 |
// add 10+9 result should be 1 0 0 1 1 |
||
fmt.Println(add4(1, 0, 1, 0, 1, 0, 0, 1)) |
fmt.Println(add4(1, 0, 1, 0, 1, 0, 0, 1)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,555: | Line 2,555: | ||
===Channels=== |
===Channels=== |
||
Alternative solution is a little more like a simulation. |
Alternative solution is a little more like a simulation. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 2,680: | Line 2,680: | ||
B[<-r4], B[<-r3], B[<-r2], B[<-r1], B[<-carry]) |
B[<-r4], B[<-r3], B[<-r2], B[<-r1], B[<-carry]) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Mini reference: |
Mini reference: |
||
Line 2,689: | Line 2,689: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">class Main { |
||
static void main(args) { |
static void main(args) { |
||
Line 2,915: | Line 2,915: | ||
adder4.setBit2 input |
adder4.setBit2 input |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Basic gates: |
Basic gates: |
||
< |
<syntaxhighlight lang="haskell">import Control.Arrow |
||
import Data.List (mapAccumR) |
import Data.List (mapAccumR) |
||
Line 2,926: | Line 2,926: | ||
band = min |
band = min |
||
bnot :: Int -> Int |
bnot :: Int -> Int |
||
bnot = (1-)</ |
bnot = (1-)</syntaxhighlight> |
||
Gates built with basic ones: |
Gates built with basic ones: |
||
< |
<syntaxhighlight lang="haskell">nand, xor :: Int -> Int -> Int |
||
nand = (bnot.).band |
nand = (bnot.).band |
||
xor a b = uncurry nand. (nand a &&& nand b) $ nand a b</ |
xor a b = uncurry nand. (nand a &&& nand b) $ nand a b</syntaxhighlight> |
||
Adder circuits: |
Adder circuits: |
||
< |
<syntaxhighlight lang="haskell">halfAdder = uncurry band &&& uncurry xor |
||
fullAdder (c, a, b) = (\(cy,s) -> first (bor cy) $ halfAdder (b, s)) $ halfAdder (c, a) |
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</ |
adder4 as = mapAccumR (\cy (f,a,b) -> f (cy,a,b)) 0 . zip3 (replicate 4 fullAdder) as</syntaxhighlight> |
||
Example using adder4 |
Example using adder4 |
||
< |
<syntaxhighlight lang="haskell">*Main> adder4 [1,0,1,0] [1,1,1,1] |
||
(1,[1,0,0,1])</ |
(1,[1,0,0,1])</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Line 2,945: | Line 2,945: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="unicon"># |
||
# 4bitadder.icn, emulate a 4 bit adder. Using only and, or, not |
# 4bitadder.icn, emulate a 4 bit adder. Using only and, or, not |
||
# |
# |
||
Line 3,047: | Line 3,047: | ||
# cr.c is the overflow carry |
# cr.c is the overflow carry |
||
return s |
return s |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,075: | Line 3,075: | ||
===Implementation=== |
===Implementation=== |
||
< |
<syntaxhighlight lang="j">and=: *. |
||
or=: +. |
or=: +. |
||
not=: -. |
not=: -. |
||
xor=: (and not) or (and not)~ |
xor=: (and not) or (and not)~ |
||
hadd=: and ,"0 xor |
hadd=: and ,"0 xor |
||
add=: ((({.,0:)@[ or {:@[ hadd {.@]), }.@])/@hadd</ |
add=: ((({.,0:)@[ or {:@[ hadd {.@]), }.@])/@hadd</syntaxhighlight> |
||
===Example use=== |
===Example use=== |
||
< |
<syntaxhighlight lang="j"> 1 1 1 1 add 0 1 1 1 |
||
1 0 1 1 0</ |
1 0 1 1 0</syntaxhighlight> |
||
To produce all results: |
To produce all results: |
||
< |
<syntaxhighlight lang="j"> add"1/~#:i.16</syntaxhighlight> |
||
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: |
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: |
||
< |
<syntaxhighlight lang="j"> ,"2 ' ',"1 -.&' '@":"1 add"1/~#:i.16 |
||
00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 01010 01011 01100 01101 01110 01111 |
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 |
00001 00010 00011 00100 00101 00110 00111 01000 01001 01010 01011 01100 01101 01110 01111 10000 |
||
Line 3,107: | Line 3,107: | ||
01101 01110 01111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 |
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 |
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</ |
01111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110</syntaxhighlight> |
||
Alternatively, the fact that add was designed to operate on lists of bits could have been incorporated into its definition: |
Alternatively, the fact that add was designed to operate on lists of bits could have been incorporated into its definition: |
||
< |
<syntaxhighlight lang="j">add=: ((({.,0:)@[ or {:@[ hadd {.@]), }.@])/@hadd"1</syntaxhighlight> |
||
Then to get all results you could use: |
Then to get all results you could use: |
||
< |
<syntaxhighlight lang="j"> add/~#:i.16</syntaxhighlight> |
||
Compare this to a regular addition table: |
Compare this to a regular addition table: |
||
<lang |
<syntaxhighlight lang="j"> +/~i.10</syntaxhighlight> |
||
(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.) |
(this produces a 10 by 10 array -- the results have no further internal array structure, though of course in the machine implementation integers can be thought of as being represented as fixed width lists of bits.) |
||
Line 3,157: | Line 3,157: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">public class GateLogic |
||
{ |
{ |
||
// Basic gate interfaces |
// Basic gate interfaces |
||
Line 3,303: | Line 3,303: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,320: | Line 3,320: | ||
0s and 1s. To enforce this, we'll first create a couple of helper functions. |
0s and 1s. To enforce this, we'll first create a couple of helper functions. |
||
<syntaxhighlight lang="javascript"> |
|||
<lang JavaScript> |
|||
function acceptedBinFormat(bin) { |
function acceptedBinFormat(bin) { |
||
if (bin == 1 || bin === 0 || bin === '0') |
if (bin == 1 || bin === 0 || bin === '0') |
||
Line 3,335: | Line 3,335: | ||
return true; |
return true; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Implementation=== |
===Implementation=== |
||
Line 3,342: | Line 3,342: | ||
and, finally, the four bit adder. |
and, finally, the four bit adder. |
||
<syntaxhighlight lang="javascript"> |
|||
<lang JavaScript> |
|||
// basic building blocks allowed by the rules are ~, &, and |, we'll fake these |
// 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 |
// in a way that makes what they do (at least when you use them) more obvious |
||
Line 3,415: | Line 3,415: | ||
return out.join(''); |
return out.join(''); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Example Use=== |
===Example Use=== |
||
< |
<syntaxhighlight lang="javascript">fourBitAdder('1010', '0101'); // 1111 (15)</syntaxhighlight> |
||
all results: |
all results: |
||
<syntaxhighlight lang="javascript"> |
|||
<lang JavaScript> |
|||
// run this in your browsers console |
// run this in your browsers console |
||
var outer = inner = 16, a, b; |
var outer = inner = 16, a, b; |
||
Line 3,433: | Line 3,433: | ||
inner = outer; |
inner = outer; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 3,440: | Line 3,440: | ||
All the operations except fourBitAdder(a,b) assume the inputs are presented as 0 or 1 (i.e. integers). |
All the operations except fourBitAdder(a,b) assume the inputs are presented as 0 or 1 (i.e. integers). |
||
< |
<syntaxhighlight lang="jq"># Start with the 'not' and 'and' building blocks. |
||
# These allow us to construct 'nand', 'or', and 'xor', |
# These allow us to construct 'nand', 'or', and 'xor', |
||
# and so on. |
# and so on. |
||
Line 3,481: | Line 3,481: | ||
| fullAdder($inA[0]; $inB[0]; $pass.carry) as $pass |
| fullAdder($inA[0]; $inB[0]; $pass.carry) as $pass |
||
| .[0] = $pass.sum |
| .[0] = $pass.sum |
||
| map(tostring) | join("") ;</ |
| map(tostring) | join("") ;</syntaxhighlight> |
||
'''Example:''' |
'''Example:''' |
||
< |
<syntaxhighlight lang="jq">fourBitAdder("0111"; "0001")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
$ jq -n -f Four_bit_adder.jq |
$ jq -n -f Four_bit_adder.jq |
||
Line 3,490: | Line 3,490: | ||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
Based on Javascript entry. |
Based on Javascript entry. |
||
< |
<syntaxhighlight lang="javascript">#!/usr/bin/env jsish |
||
/* 4 bit adder simulation, in Jsish */ |
/* 4 bit adder simulation, in Jsish */ |
||
function not(a) { return a == 1 ? 0 : 1; } |
function not(a) { return a == 1 ? 0 : 1; } |
||
Line 3,570: | Line 3,570: | ||
PASS!: err = bad bit at a[3] of "2" |
PASS!: err = bad bit at a[3] of "2" |
||
=!EXPECTEND!= |
=!EXPECTEND!= |
||
*/</ |
*/</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>prompt$ jsish --U fourBitAdder.jsi |
<pre>prompt$ jsish --U fourBitAdder.jsi |
||
Line 3,594: | Line 3,594: | ||
'''Functions''' |
'''Functions''' |
||
< |
<syntaxhighlight lang="julia">using Printf |
||
xor{T<:Bool}(a::T, b::T) = (a&~b)|(~a&b) |
xor{T<:Bool}(a::T, b::T) = (a&~b)|(~a&b) |
||
Line 3,624: | Line 3,624: | ||
Base.bits(n::BitArray{1}) = join(reverse(int(n)), "") |
Base.bits(n::BitArray{1}) = join(reverse(int(n)), "") |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Main''' |
'''Main''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
xavail = trues(15,15) |
xavail = trues(15,15) |
||
xcnt = 0 |
xcnt = 0 |
||
Line 3,646: | Line 3,646: | ||
bits(s), oflow)) |
bits(s), oflow)) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,664: | Line 3,664: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.51 |
||
val Boolean.I get() = if (this) 1 else 0 |
val Boolean.I get() = if (this) 1 else 0 |
||
Line 3,715: | Line 3,715: | ||
for (j in i..minOf(i + 1, 15)) test(i, j) |
for (j in i..minOf(i + 1, 15)) test(i, j) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,771: | Line 3,771: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
<syntaxhighlight lang="scheme"> |
|||
<lang Scheme> |
|||
{def xor |
{def xor |
||
{lambda {:a :b} |
{lambda {:a :b} |
||
Line 3,862: | Line 3,862: | ||
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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 |
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">-- Build XOR from AND, OR and NOT |
||
function xor (a, b) return (a and not b) or (b and not a) end |
function xor (a, b) return (a and not b) or (b and not a) end |
||
Line 3,921: | Line 3,921: | ||
print(add(0101, 1010)) -- 5 + 10 = 15 |
print(add(0101, 1010)) -- 5 + 10 = 15 |
||
print(add(0000, 1111)) -- 0 + 15 = 15 |
print(add(0000, 1111)) -- 0 + 15 = 15 |
||
print(add(0001, 1111)) -- 1 + 15 = 16 (causes overflow)</ |
print(add(0001, 1111)) -- 1 + 15 = 16 (causes overflow)</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>SUM OVERFLOW |
<pre>SUM OVERFLOW |
||
Line 3,931: | Line 3,931: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module FourBitAdder { |
Module FourBitAdder { |
||
Flush |
Flush |
||
Line 3,991: | Line 3,991: | ||
} |
} |
||
FourBitAdder |
FourBitAdder |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<lang>and[a_, b_] := Max[a, b]; |
<syntaxhighlight lang="text">and[a_, b_] := Max[a, b]; |
||
or[a_, b_] := Min[a, b]; |
or[a_, b_] := Min[a, b]; |
||
not[a_] := 1 - a; |
not[a_] := 1 - a; |
||
Line 4,009: | Line 4,009: | ||
{s2, c2} = fulladder[a2, b2, c1]; |
{s2, c2} = fulladder[a2, b2, c1]; |
||
{s3, c3} = fulladder[a3, b3, c2]; |
{s3, c3} = fulladder[a3, b3, c2]; |
||
{{s3, s2, s1, s0}, c3}];</ |
{{s3, s2, s1, s0}, c3}];</syntaxhighlight> |
||
Example: |
Example: |
||
<lang>fourbitadder[{1, 0, 1, 0}, {1, 1, 1, 1}]</ |
<syntaxhighlight lang="text">fourbitadder[{1, 0, 1, 0}, {1, 1, 1, 1}]</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>{{1, 0, 0, 1}, 1}</pre> |
<pre>{{1, 0, 0, 1}, 1}</pre> |
||
Line 4,018: | Line 4,018: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="matlab">function [S,v] = fourBitAdder(input1,input2) |
||
%Make sure that only 4-Bit numbers are being added. This assumes that |
%Make sure that only 4-Bit numbers are being added. This assumes that |
||
Line 4,081: | Line 4,081: | ||
v = num2str(v); |
v = num2str(v); |
||
end |
end |
||
end %fourBitAdder</ |
end %fourBitAdder</syntaxhighlight> |
||
Sample Usage: |
Sample Usage: |
||
< |
<syntaxhighlight lang="matlab">>> [S,V] = fourBitAdder([0 0 0 1],[1 1 1 1]) |
||
S = |
S = |
||
Line 4,137: | Line 4,137: | ||
11 |
11 |
||
12</ |
12</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
< |
<syntaxhighlight lang="mumps">XOR(Y,Z) ;Uses logicals - i.e., 0 is false, anything else is true (1 is used if setting a value) |
||
QUIT (Y&'Z)!('Y&Z) |
QUIT (Y&'Z)!('Y&Z) |
||
HALF(W,X) |
HALF(W,X) |
||
Line 4,153: | Line 4,153: | ||
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) |
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 |
K I,T |
||
QUIT S_"^"_C4</ |
QUIT S_"^"_C4</syntaxhighlight> |
||
Usage:<pre>USER>S N1="0110",N2="0010",C=0,T=$$FOUR^ADDER(N1,N2,C) |
Usage:<pre>USER>S N1="0110",N2="0010",C=0,T=$$FOUR^ADDER(N1,N2,C) |
||
Line 4,170: | Line 4,170: | ||
The test code simulates the adder and exports trace wave file for debug support. Verilog and VHDL files are exported for hardware synthesis. |
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: |
To run: |
||
python3 Four_bit_adder_011.py |
python3 Four_bit_adder_011.py |
||
Line 4,311: | Line 4,311: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main() |
main() |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">type |
||
Bools[N: static int] = array[N, bool] |
Bools[N: static int] = array[N, bool] |
||
Line 4,348: | Line 4,348: | ||
for a in 0..7: |
for a in 0..7: |
||
for b in 0..7: |
for b in 0..7: |
||
assert a + b == bus2int fa4(int2bus(a), int2bus(b))</ |
assert a + b == bus2int fa4(int2bus(a), int2bus(b))</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml"> |
||
(* File blocks.ml |
(* File blocks.ml |
||
Line 4,554: | Line 4,554: | ||
(eval add4_io 4 4 (Array.map Int64.of_int [| a; b |])) in |
(eval add4_io 4 4 (Array.map Int64.of_int [| a; b |])) in |
||
v.(0), v.(1);; |
v.(0), v.(1);; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Testing |
Testing |
||
< |
<syntaxhighlight lang="ocaml"> |
||
# open Blocks;; |
# open Blocks;; |
||
Line 4,572: | Line 4,572: | ||
# plus 0 0;; |
# plus 0 0;; |
||
- : int * int = (0, 0) |
- : int * int = (0, 0) |
||
</syntaxhighlight> |
|||
</lang> |
|||
An extension : n-bit adder, for n <= 64 (n could be greater, but we use Int64 for I/O) |
An extension : n-bit adder, for n <= 64 (n could be greater, but we use Int64 for I/O) |
||
< |
<syntaxhighlight lang="ocaml"> |
||
(* general adder (n bits with n <= 64) *) |
(* general adder (n bits with n <= 64) *) |
||
let gen_adder n = block_array serial [| |
let gen_adder n = block_array serial [| |
||
Line 4,591: | Line 4,591: | ||
(eval (gadd_io n) n n (Array.map Int64.of_int [| a; b |])) in |
(eval (gadd_io n) n n (Array.map Int64.of_int [| a; b |])) in |
||
v.(0), v.(1);; |
v.(0), v.(1);; |
||
</syntaxhighlight> |
|||
</lang> |
|||
And a test |
And a test |
||
< |
<syntaxhighlight lang="ocaml"> |
||
# gen_plus 7 100 100;; |
# gen_plus 7 100 100;; |
||
- : int * int = (72, 1) |
- : int * int = (72, 1) |
||
# gen_plus 8 100 100;; |
# gen_plus 8 100 100;; |
||
- : int * int = (200, 0) |
- : int * int = (200, 0) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">xor(a,b)=(!a&b)||(a&!b); |
||
halfadd(a,b)=[a&&b,xor(a,b)]; |
halfadd(a,b)=[a&&b,xor(a,b)]; |
||
fulladd(a,b,c)=my(t=halfadd(a,c),s=halfadd(t[2],b));[t[1]||s[1],s[2]]; |
fulladd(a,b,c)=my(t=halfadd(a,c),s=halfadd(t[2],b));[t[1]||s[1],s[2]]; |
||
Line 4,614: | Line 4,614: | ||
[s3[1],s3[2],s2[2],s1[2],s0[2]] |
[s3[1],s3[2],s2[2],s1[2],s0[2]] |
||
}; |
}; |
||
add4(0,0,0,0,0,0,0,0)</ |
add4(0,0,0,0,0,0,0,0)</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">sub dec2bin { sprintf "%04b", shift } |
||
sub bin2dec { oct "0b".shift } |
sub bin2dec { oct "0b".shift } |
||
sub bin2bits { reverse split(//, substr(shift,0,shift)); } |
sub bin2bits { reverse split(//, substr(shift,0,shift)); } |
||
Line 4,659: | Line 4,659: | ||
$a, $b, $abin, $bbin, $c, $s, bin2dec($c.$s); |
$a, $b, $abin, $bbin, $c, $s, bin2dec($c.$s); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output matches the [[Four bit adder#Ruby|Ruby]] output. |
Output matches the [[Four bit adder#Ruby|Ruby]] output. |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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;">function</span> <span style="color: #000000;">xor_gate</span><span style="color: #0000FF;">(</span><span style="color: #004080;">bool</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> |
||
Line 4,714: | Line 4,714: | ||
<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;">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> |
<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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,733: | Line 4,733: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de halfAdder (A B) #> (Carry . Sum) |
||
(cons |
(cons |
||
(and A B) |
(and A B) |
||
Line 4,755: | Line 4,755: | ||
(cdr Fa3) |
(cdr Fa3) |
||
(cdr Fa2) |
(cdr Fa2) |
||
(cdr Fa1) ) ) )</ |
(cdr Fa1) ) ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>: (4bitsAdder NIL NIL NIL T NIL NIL NIL T) |
<pre>: (4bitsAdder NIL NIL NIL T NIL NIL NIL T) |
||
Line 4,770: | Line 4,770: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
/* 4-BIT ADDER */ |
/* 4-BIT ADDER */ |
||
Line 4,806: | Line 4,806: | ||
END TEST; |
END TEST; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
===Using Bytes as Inputs=== |
===Using Bytes as Inputs=== |
||
< |
<syntaxhighlight lang="powershell">function bxor2 ( [byte] $a, [byte] $b ) |
||
{ |
{ |
||
$out1 = $a -band ( -bnot $b ) |
$out1 = $a -band ( -bnot $b ) |
||
Line 4,861: | Line 4,861: | ||
FourBitAdder 0xC 0xB |
FourBitAdder 0xC 0xB |
||
[Convert]::ToByte((FourBitAdder 0xC 0xB)["S"],2)</ |
[Convert]::ToByte((FourBitAdder 0xC 0xB)["S"],2)</syntaxhighlight> |
||
===Translation of C# code=== |
===Translation of C# code=== |
||
The well-written C# code on this page can be translated without any modification into a .NET type by PowerShell. |
The well-written C# code on this page can be translated without any modification into a .NET type by PowerShell. |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
$source = @' |
$source = @' |
||
using System; |
using System; |
||
Line 4,997: | Line 4,997: | ||
Add-Type -TypeDefinition $source -Language CSharpVersion3 |
Add-Type -TypeDefinition $source -Language CSharpVersion3 |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
[RosettaCodeTasks.FourBitAdder.ConstructiveBlocks]::Test() |
[RosettaCodeTasks.FourBitAdder.ConstructiveBlocks]::Test() |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 5,017: | Line 5,017: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Using hi/lo symbols to represent binary. As this is a simulation, there is no real "arithmetic" happening. |
Using hi/lo symbols to represent binary. As this is a simulation, there is no real "arithmetic" happening. |
||
< |
<syntaxhighlight lang="prolog">% binary 4 bit adder chip simulation |
||
b_not(in(hi), out(lo)) :- !. % not(1) = 0 |
b_not(in(hi), out(lo)) :- !. % not(1) = 0 |
||
Line 5,058: | Line 5,058: | ||
test_add(in(hi,hi,lo,hi), in(hi,lo,lo,hi), '11 + 9 = 20'), |
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(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').</ |
test_add(in(hi,hi,hi,hi), in(hi,lo,lo,lo), '15 + 1 = 16').</syntaxhighlight> |
||
<pre>?- go. |
<pre>?- go. |
||
in(hi,lo,lo,lo) + in(hi,lo,lo,lo) is out(lo,hi,lo,lo) c(lo) (1 + 1 = 2) |
in(hi,lo,lo,lo) + in(hi,lo,lo,lo) is out(lo,hi,lo,lo) c(lo) (1 + 1 = 2) |
||
Line 5,069: | Line 5,069: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">;Because no representation for a solitary bit is present, bits are stored as bytes. |
||
;Output values from the constructive building blocks is done using pointers (i.e. '*'). |
;Output values from the constructive building blocks is done using pointers (i.e. '*'). |
||
Line 5,122: | Line 5,122: | ||
Input() |
Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0110 + 1110 = 0100 overflow 1</pre> |
<pre>0110 + 1110 = 0100 overflow 1</pre> |
||
Line 5,144: | Line 5,144: | ||
between the normal Python values and those of the simulation. |
between the normal Python values and those of the simulation. |
||
< |
<syntaxhighlight lang="python">def xor(a, b): return (a and not b) or (b and not a) |
||
def ha(a, b): return xor(a, b), a and b # sum, carry |
def ha(a, b): return xor(a, b), a and b # sum, carry |
||
Line 5,178: | Line 5,178: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
test_fa4()</ |
test_fa4()</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (adder-and a b) |
(define (adder-and a b) |
||
Line 5,226: | Line 5,226: | ||
(cons v 4s)))) |
(cons v 4s)))) |
||
(n-bit-adder '(1 0 1 0) '(0 1 1 1)) ;-> '(1 0 0 0 1)</ |
(n-bit-adder '(1 0 1 0) '(0 1 1 1)) ;-> '(1 0 0 0 1)</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>sub xor ($a, $b) { (($a and not $b) or (not $a and $b)) ?? 1 !! 0 } |
||
sub half-adder ($a, $b) { |
sub half-adder ($a, $b) { |
||
Line 5,257: | Line 5,257: | ||
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, 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'; |
is four-bit-adder(1, 0, 0, 1, 1, 1, 1, 0)[4], 1, '7 + 9 == overflow'; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,271: | Line 5,271: | ||
::::* the '''|''' symbol is a logical '''OR'''. |
::::* the '''|''' symbol is a logical '''OR'''. |
||
::::* the '''&''' symbol is a logical '''AND'''. |
::::* the '''&''' symbol is a logical '''AND'''. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program displays (all) the sums of a full 4─bit adder (with carry). */ |
||
call hdr1; call hdr2 /*note the order of headers & trailers.*/ |
call hdr1; call hdr2 /*note the order of headers & trailers.*/ |
||
/* [↓] traipse thru all possibilities.*/ |
/* [↓] traipse thru all possibilities.*/ |
||
Line 5,301: | Line 5,301: | ||
do j=0 for 4; n= j - 1 |
do j=0 for 4; n= j - 1 |
||
s.j= fullAdder(a.j, b.j, carry.n); carry.j= c |
s.j= fullAdder(a.j, b.j, carry.n); carry.j= c |
||
end /*j*/; return c</ |
end /*j*/; return c</syntaxhighlight> |
||
{{out|output|text= (most lines have been elided):}} |
{{out|output|text= (most lines have been elided):}} |
||
<pre style="height:63ex"> |
<pre style="height:63ex"> |
||
Line 5,374: | Line 5,374: | ||
{{Full One-Bit-Adder function is made up of XOR OR AND gates}} |
{{Full One-Bit-Adder function is made up of XOR OR AND gates}} |
||
< |
<syntaxhighlight lang="ring"> |
||
###--------------------------- |
###--------------------------- |
||
Line 5,446: | Line 5,446: | ||
###------------------------ |
###------------------------ |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 5,461: | Line 5,461: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby"># returns pair [sum, carry] |
||
def four_bit_adder(a, b) |
def four_bit_adder(a, b) |
||
a_bits = binary_string_to_bits(a,4) |
a_bits = binary_string_to_bits(a,4) |
||
Line 5,516: | Line 5,516: | ||
[a, b, bin_a, bin_b, carry, sum, (carry + sum).to_i(2)] |
[a, b, bin_a, bin_b, carry, sum, (carry + sum).to_i(2)] |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,540: | Line 5,540: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
// half adder with XOR and AND |
// half adder with XOR and AND |
||
// SUM = A XOR B |
// SUM = A XOR B |
||
Line 5,605: | Line 5,605: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
< |
<syntaxhighlight lang="sather">-- a "pin" can be connected only to one component |
||
-- that "sets" it to 0 or 1, while it can be "read" |
-- that "sets" it to 0 or 1, while it can be "read" |
||
-- ad libitum. (Tristate logic is not taken into account) |
-- ad libitum. (Tristate logic is not taken into account) |
||
Line 5,745: | Line 5,745: | ||
", overflow = " + fba.v.val + "\n"; |
", overflow = " + fba.v.val + "\n"; |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">object FourBitAdder { |
||
type Nibble=(Boolean, Boolean, Boolean, Boolean) |
type Nibble=(Boolean, Boolean, Boolean, Boolean) |
||
Line 5,773: | Line 5,773: | ||
((s3, s2, s1, s0), cOut) |
((s3, s2, s1, s0), cOut) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
A test program using the object above. |
A test program using the object above. |
||
< |
<syntaxhighlight lang="scala">object FourBitAdderTest { |
||
import FourBitAdder._ |
import FourBitAdder._ |
||
def main(args: Array[String]): Unit = { |
def main(args: Array[String]): Unit = { |
||
Line 5,790: | Line 5,790: | ||
implicit def intToNibble(i:Int):Nibble=((i>>>3)&1, (i>>>2)&1, (i>>>1)&1, i&1) |
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) |
def nibbleToString(n:Nibble):String="%d%d%d%d".format(n._1.toInt, n._2.toInt, n._3.toInt, n._4.toInt) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> A B S C |
<pre> A B S C |
||
Line 5,809: | Line 5,809: | ||
{{trans|Common Lisp}} |
{{trans|Common Lisp}} |
||
< |
<syntaxhighlight lang="scheme"> |
||
(import (scheme base) |
(import (scheme base) |
||
(scheme write) |
(scheme write) |
||
Line 5,843: | Line 5,843: | ||
(show-eg (list 1 1 1 1) (list 1 1 1 1)) |
(show-eg (list 1 1 1 1) (list 1 1 1 1)) |
||
(show-eg (list 1 0 1 0) (list 0 1 0 1)) |
(show-eg (list 1 0 1 0) (list 0 1 0 1)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 5,858: | Line 5,858: | ||
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). |
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 |
I took it from https://github.com/emsi/SedScripts |
||
< |
<syntaxhighlight lang="sed"> |
||
#!/bin/sed -f |
#!/bin/sed -f |
||
# (C) 2005,2014 by Mariusz Woloszyn :) |
# (C) 2005,2014 by Mariusz Woloszyn :) |
||
Line 5,910: | Line 5,910: | ||
} |
} |
||
b LOOP</ |
b LOOP</syntaxhighlight> |
||
Example usage: |
Example usage: |
||
< |
<syntaxhighlight lang="sed"> |
||
./binAdder.sed |
./binAdder.sed |
||
1111110111 |
1111110111 |
||
Line 5,929: | Line 5,929: | ||
0 0 0 1 |
0 0 0 1 |
||
111 |
111 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">func bxor(a, b) { |
||
(~a & b) | (a & ~b) |
(~a & b) | (a & ~b) |
||
} |
} |
||
Line 5,963: | Line 5,963: | ||
a, b, abin.join, bbin.join, c, s, "#{c}#{s}".bin) |
a, b, abin.join, bbin.join, c, s, "#{c}#{s}".bin) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,989: | Line 5,989: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">typealias FourBit = (Int, Int, Int, Int) |
||
func halfAdder(_ a: Int, _ b: Int) -> (Int, Int) { |
func halfAdder(_ a: Int, _ b: Int) -> (Int, Int) { |
||
Line 6,016: | Line 6,016: | ||
print("\(a) + \(b) = \(fourBitAdder(a, b))") |
print("\(a) + \(b) = \(fourBitAdder(a, b))") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 6,024: | Line 6,024: | ||
=={{header|SystemVerilog}}== |
=={{header|SystemVerilog}}== |
||
In SystemVerilog we can define a multibit adder as a parameterized module, that instantiates the components: |
In SystemVerilog we can define a multibit adder as a parameterized module, that instantiates the components: |
||
<syntaxhighlight lang="systemverilog"> |
|||
<lang SystemVerilog> |
|||
module Half_Adder( input a, b, output s, c ); |
module Half_Adder( input a, b, output s, c ); |
||
assign s = a ^ b; |
assign s = a ^ b; |
||
Line 6,058: | Line 6,058: | ||
endmodule |
endmodule |
||
</syntaxhighlight> |
|||
</lang> |
|||
And then a testbench to test it -- here I use random stimulus with an assertion (it's aften good to separate the stimulus generation from the results-checking): |
And then a testbench to test it -- here I use random stimulus with an assertion (it's aften good to separate the stimulus generation from the results-checking): |
||
<syntaxhighlight lang="systemverilog"> |
|||
<lang SystemVerilog> |
|||
module simTop(); |
module simTop(); |
||
Line 6,094: | Line 6,094: | ||
endprogram |
endprogram |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 6,128: | Line 6,128: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
This example shows how you can make little languages in Tcl that describe the problem space. |
This example shows how you can make little languages in Tcl that describe the problem space. |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
# Create our little language |
# Create our little language |
||
Line 6,192: | Line 6,192: | ||
fulladd a2 b2 c2 s2 c3 |
fulladd a2 b2 c2 s2 c3 |
||
fulladd a3 b3 c3 s3 v |
fulladd a3 b3 c3 s3 v |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="tcl"># Simple driver for the circuit |
||
proc 4add_driver {a b} { |
proc 4add_driver {a b} { |
||
lassign [split $a {}] a3 a2 a1 a0 |
lassign [split $a {}] a3 a2 a1 a0 |
||
Line 6,206: | Line 6,206: | ||
set a 1011 |
set a 1011 |
||
set b 0110 |
set b 0110 |
||
puts $a+$b=[4add_driver $a $b]</ |
puts $a+$b=[4add_driver $a $b]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 6,216: | Line 6,216: | ||
=={{header|TorqueScript}}== |
=={{header|TorqueScript}}== |
||
< |
<syntaxhighlight lang="torque">function XOR(%a, %b) |
||
{ |
{ |
||
return (!%a && %b) || (%a && !%b); |
return (!%a && %b) || (%a && !%b); |
||
Line 6,244: | Line 6,244: | ||
%r3 = FullAdd(%a3, %b3, getWord(%r2, 0)); |
%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); |
return getWord(%r0,1) SPC getWord(%r1,1) SPC getWord(%r2,1) SPC getWord(%r3,1) SPC getWord(%r3,0); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 6,254: | Line 6,254: | ||
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. |
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 |
typeset -i a=$1 b=$2 |
||
printf '%d\n' $(( (a || b) && ! (a && b) )) |
printf '%d\n' $(( (a || b) && ! (a && b) )) |
||
Line 6,299: | Line 6,299: | ||
is "7 + 9 = overflow" "a=($(four_bit_adder 1 0 0 1 1 1 1 0)); (( \${a[-1]}==1 ))" |
is "7 + 9 = overflow" "a=($(four_bit_adder 1 0 0 1 1 1 1 0)); (( \${a[-1]}==1 ))" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 6,308: | Line 6,308: | ||
=={{header|Verilog}}== |
=={{header|Verilog}}== |
||
In Verilog we can also define a multibit adder as a component with multiple instances: |
In Verilog we can also define a multibit adder as a component with multiple instances: |
||
<syntaxhighlight lang="verilog"> |
|||
<lang Verilog> |
|||
module Half_Adder( output c, s, input a, b ); |
module Half_Adder( output c, s, input a, b ); |
||
xor xor01 (s, a, b); |
xor xor01 (s, a, b); |
||
Line 6,357: | Line 6,357: | ||
endmodule // test_Full_Adder |
endmodule // test_Full_Adder |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 6,372: | Line 6,372: | ||
=={{header|VHDL}}== |
=={{header|VHDL}}== |
||
The following is a direct implementation of the proposed schematic: |
The following is a direct implementation of the proposed schematic: |
||
< |
<syntaxhighlight lang="vhdl">LIBRARY ieee; |
||
USE ieee.std_logic_1164.all; |
USE ieee.std_logic_1164.all; |
||
Line 6,532: | Line 6,532: | ||
begin |
begin |
||
x <= (a and not b) or (b and not a); |
x <= (a and not b) or (b and not a); |
||
end architecture rtl;</ |
end architecture rtl;</syntaxhighlight> |
||
An exhaustive testbench: |
An exhaustive testbench: |
||
< |
<syntaxhighlight lang="vhdl">LIBRARY ieee; |
||
USE ieee.std_logic_1164.all; |
USE ieee.std_logic_1164.all; |
||
use ieee.NUMERIC_STD.all; |
use ieee.NUMERIC_STD.all; |
||
Line 6,580: | Line 6,580: | ||
); |
); |
||
end struct;</ |
end struct;</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ecmascript">var xor = Fn.new { |a, b| a&(~b) | b&(~a) } |
||
var ha = Fn.new { |a, b| [xor.call(a, b), a & b] } |
var ha = Fn.new { |a, b| [xor.call(a, b), a & b] } |
||
Line 6,610: | Line 6,610: | ||
} |
} |
||
System.print(add4.call(1, 0, 1, 0, 1, 0, 0, 1))</ |
System.print(add4.call(1, 0, 1, 0, 1, 0, 0, 1))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,618: | Line 6,618: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">code CrLf=9, IntOut=11; |
||
func Not(A); |
func Not(A); |
||
Line 6,676: | Line 6,676: | ||
Add4Bits(1, 1, 1, 1, 1, 1, 1, 1, @S0, @S1, @S2, @S3, @C); \1111 + 1111 = 11110 |
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); |
BinOut(0, S0, S1, S2, S3, C); CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,686: | Line 6,686: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn xor(a,b) // a,b are 1|0 -->a^b(1|0) |
||
{ a.bitAnd(b.bitNot()).bitOr(b.bitAnd(a.bitNot())) } |
{ a.bitAnd(b.bitNot()).bitOr(b.bitAnd(a.bitNot())) } |
||
Line 6,709: | Line 6,709: | ||
// add(10,9) result should be 1 0 0 1 1 (0x13, 3 carry 1) |
// 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));</ |
println(fourBitAdder(1,0,1,0, 1,0,0,1));</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">fcn nBitAddr(as,bs){ //-->(carry, sn..s3,s2,s1,s0) |
||
(ss:=List()).append( |
(ss:=List()).append( |
||
[as.len()-1 .. 0,-1].reduce('wrap(c,n){ |
[as.len()-1 .. 0,-1].reduce('wrap(c,n){ |
||
Line 6,717: | Line 6,717: | ||
.reverse(); |
.reverse(); |
||
} |
} |
||
println(nBitAddr(T(1,0,1,0), T(1,0,0,1)));</ |
println(nBitAddr(T(1,0,1,0), T(1,0,0,1)));</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |