How to resolve the algorithm Logical operations step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Logical operations step by step in the D programming language

Table of Contents

Problem Statement

Write a function that takes two logical (boolean) values, and outputs the result of "and" and "or" on both arguments as well as "not" on the first arguments. If the programming language doesn't provide a separate type for logical values, use the type most commonly used for that purpose. If the language supports additional logical operations on booleans such as XOR, list them as well.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Logical operations step by step in the D programming language

Source code in the d programming language

import std.stdio;

void logic(T, U)(T lhs, U rhs) {
    writefln("'%s' is of type '%s', '%s' is of type '%s';", 
             lhs, typeid(typeof(lhs)), rhs,typeid(typeof(rhs)));
    writefln("\t'%s' AND '%s' is %s, ", lhs, rhs, lhs && rhs);
    writefln("\t'%s' OR '%s' is %s, ", lhs, rhs, lhs || rhs);
    writefln("\tNOT '%s' is %s.\n", lhs, !lhs);
}

class C { int value; } 

void main() {
    bool theTruth = true;
    bool theLie = false;
    real zeroReal = 0.0L;
    real NaN; // D initializes floating point values to NaN
    int zeroInt  = 0;
    real[] nullArr = null;
    string emptyStr = "";
    string nullStr = null;
    C someC = new C;
    C nullC = null;

    // Note: Struct is value type in D, but composite
    //  so no default bool equivalent.

    logic(theTruth, theLie); 
    logic(zeroReal, NaN);  
    logic(zeroInt, nullArr); 
    logic(nullStr, emptyStr);  
    logic(someC, nullC);  
}


  

You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Perl programming language
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Tic-tac-toe step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Hunt the Wumpus step by step in the Haskell programming language
You may also check:How to resolve the algorithm Arithmetic/Rational step by step in the Elisa programming language