How to resolve the algorithm Ternary logic step by step in the D programming language
How to resolve the algorithm Ternary logic step by step in the D programming language
Table of Contents
Problem Statement
In logic, a three-valued logic (also trivalent, ternary, or trinary logic, sometimes abbreviated 3VL) is any of several many-valued logic systems in which there are three truth values indicating true, false and some indeterminate third value.
This is contrasted with the more commonly known bivalent logics (such as classical sentential or boolean logic) which provide only for true and false.
Conceptual form and basic ideas were initially created by Łukasiewicz, Lewis and Sulski.
These were then re-formulated by Grigore Moisil in an axiomatic algebraic form, and also extended to n-valued logics in 1945.
Note: Setun (Сетунь) was a balanced ternary computer developed in 1958 at Moscow State University. The device was built under the lead of Sergei Sobolev and Nikolay Brusentsov. It was the only modern ternary computer, using three-valued ternary logic
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Ternary logic step by step in the D programming language
Source code in the d programming language
import std.stdio;
struct Trit {
private enum Val : byte { F = -1, M, T }
private Val t;
alias t this;
static immutable Trit[3] vals = [{Val.F}, {Val.M}, {Val.T}];
static immutable F = Trit(Val.F); // Not necessary but handy.
static immutable M = Trit(Val.M);
static immutable T = Trit(Val.T);
string toString() const pure nothrow {
return "F?T"[t + 1 .. t + 2];
}
Trit opUnary(string op)() const pure nothrow
if (op == "~") {
return Trit(-t);
}
Trit opBinary(string op)(in Trit b) const pure nothrow
if (op == "&") {
return t < b ? this : b;
}
Trit opBinary(string op)(in Trit b) const pure nothrow
if (op == "|") {
return t > b ? this : b;
}
Trit opBinary(string op)(in Trit b) const pure nothrow
if (op == "^") {
return ~(this == b);
}
Trit opEquals(in Trit b) const pure nothrow {
return Trit(cast(Val)(t * b));
}
Trit imply(in Trit b) const pure nothrow {
return -t > b ? ~this : b;
}
}
void showOperation(string op)(in string opName) {
writef("\n[%s]\n F ? T\n -------", opName);
foreach (immutable a; Trit.vals) {
writef("\n%s |", a);
foreach (immutable b; Trit.vals)
static if (op == "==>")
writef(" %s", a.imply(b));
else
writef(" %s", mixin("a " ~ op ~ " b"));
}
writeln();
}
void main() {
writeln("[Not]");
foreach (const a; Trit.vals)
writefln("%s | %s", a, ~a);
showOperation!"&"("And");
showOperation!"|"("Or");
showOperation!"^"("Xor");
showOperation!"=="("Equiv");
showOperation!"==>"("Imply");
}
You may also check:How to resolve the algorithm Date format step by step in the Apex programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm JSON step by step in the Fortran programming language
You may also check:How to resolve the algorithm Ascending primes step by step in the Prolog programming language
You may also check:How to resolve the algorithm Superpermutation minimisation step by step in the Python programming language