Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
31
Task/Four-bit-adder/Ballerina/four-bit-adder.ballerina
Normal file
31
Task/Four-bit-adder/Ballerina/four-bit-adder.ballerina
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import ballerina/io;
|
||||
|
||||
function xor(byte a, byte b) returns byte {
|
||||
return a & (~b) | b & (~a);
|
||||
}
|
||||
|
||||
function ha(byte a, byte b) returns [byte, byte] {
|
||||
return [xor(a, b), a & b];
|
||||
}
|
||||
|
||||
function fa(byte a, byte b, byte c0) returns [byte, byte] {
|
||||
var [sa, ca] = ha(a, c0);
|
||||
var [s, cb] = ha(sa, b);
|
||||
var c1 = ca | cb;
|
||||
return [s, c1];
|
||||
}
|
||||
|
||||
function add4(byte a3, byte a2, byte a1, byte a0, byte b3, byte b2, byte b1, byte b0)
|
||||
returns [byte, byte, byte, byte, byte] {
|
||||
var [s0, c0] = fa(a0, b0, 0);
|
||||
var [s1, c1] = fa(a1, b1, c0);
|
||||
var [s2, c2] = fa(a2, b2, c1);
|
||||
var [s3, v] = fa(a3, b3, c2);
|
||||
return [v, s3, s2, s1, s0];
|
||||
}
|
||||
|
||||
public function main() {
|
||||
// add 10+9 result should be [1, 0, 0, 1, 1]
|
||||
var sum = add4(1, 0, 1, 0, 1, 0, 0, 1);
|
||||
io:println(re `,`.replaceAll(sum.toString(), ", "));
|
||||
}
|
||||
|
|
@ -1,18 +1,18 @@
|
|||
proc xor a b . r .
|
||||
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 .
|
||||
proc half_add a b &s &c .
|
||||
xor a b s
|
||||
c = bitand a b
|
||||
.
|
||||
proc full_add a b c . s g .
|
||||
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 .
|
||||
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue