Anonymous user
Four bit adder: Difference between revisions
More strict D version
(→{{header|Go}}: added simple solution) |
(More strict D version) |
||
Line 614:
<lang d>import std.stdio: writefln;
pure nothrow void fourBitsAdder(T)(in T a0, in T a1, in T a2, in T a3,▼
/// A XOR using only NOT, AND and OR, as task requires▼
out T overflow) {▼
static pure nothrow T xor(in T x, in T y) {
return (~x & y) | (x & ~y);
}
static pure nothrow void halfAdder
out T s, out T c) {
s = xor(a, b);▼
// s = a ^ b; // a natural XOR in D
}
static pure nothrow void fullAdder(
out T s, out T oc) {
T ps, pc, tc;
halfAdder(/*input*/a, b, /*output*/ps, pc);
halfAdder(/*input*/ps, ic, /*output*/s, tc);
oc = tc | pc;
}
▲void fourBitsAdder(T)(T a0, T a1, T a2, T a3,
▲ T b0, T b1, T b2, T b3,
▲ out T o0, out T o1, out T o2, out T o3,
▲ out T overflow) {
T zero, tc0, tc1, tc2;
Line 645 ⟶ 651:
alias size_t T;
enum T one = T.max
T s0, s1, s2, s3, overflow;
fourBitsAdder(/*input*/ a0, a1, a2, a3,
/*input*/ b0, b1, b2, b3,
/*output*/
writefln(" a3 %032b", a3);
|