Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C

Four Boolean Logic

3.41/5 (11 votes)
14 Apr 2018CPOL1 min read 13.8K  
Four Boolean Logic induction

Introduction

The simple boolean or Aristotle logic is well-known and widely used in Western World. The development of Information Technologies came to realization that boolean logic is not a sufficient tool for some tasks. Thus it led to tri-boolean logic. The sample of realization of this could be found in boost library.

The Essence

If we take a closer look at the case, it will be found that the tri-boolean logic operates with three conditions: true, false, and something else. Being more precise, this something else is neither true nor false. Following the logic induction, it is clear the tri-boolean logic is incomplete case and it needs one more condition: and true and false.

I am not inventing anything. Four boolean logic is a common case for Asian Buddhism, though it seems something outstanding for Western World.

There is an old Zen puzzle that breaks binary boolean Aristotle logic apart, this is a question of half-full or half-empty glass of water. It is not possible for binary logic to provide an answer on this, as to four boolean logic, it is a simplicity itself.

Here are the four conditions of four boolean logic:

  • Pure TRUE
  • Pure FALSE
  • Neither TRUE nor FALSE
  • And TRUE and FALSE

Saying condition, I do not mean state. States could be infinite in number. These four conditions make the boolean logic case complete. It is nothing to add nor to remove.

A Sample

four_boolean.cpp

C++
#include <stdint.h>
#include <inttypes.h>
#include <iostream>

/**
 * Let's take a function that implements sending data
 */

uint8_t send_data(
                    void* data          // data to send
                ,   uint32_t len        // length of the data
                );

/**
 * let's suppose the function returning code looks like this:
 *
 * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | bit's number
 * ---------------------------------
 * | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
 *   |                       |   |
 *   ` ----------------------'   `--- 1 = true (succeeded in sending)
 *                |                   0 = false (failed to send)
 *                `------------------ Error code
 */

/**
 * some helper C++ classes
 */

namespace four_boolean
{
    struct resolver
    {
        resolver (const uint8_t n) : _n(n) {}
        bool is_true() const { return _n & 0x01; }    // if 0's bit is set - true
        bool is_false() const { return _n & 0xFE ? 1 : 0; }   // if there is an error code - false
        uint8_t err_code() const { return _n >> 1; }
        uint8_t _n;
    };

    typedef enum BOOL_tag {
            TRUE            = 0x01          // pure TRUE
        ,   FALSE           = 0x02          // pure FALSE
        ,   TRUE_AND_FALSE  = TRUE | FALSE  // And TRUE and FALSE
        ,   NTRUE_NFALSE    = TRUE & FALSE  // Neither TRUE nor FALSE
    } BOOL;                                 // this is our four-boolean type

    template <class R> BOOL booler(const R& r) {
        return static_cast<BOOL>((r.is_false() << 1) | r.is_true());
    }

}

/**
 * For testing purposes let's redefine send_data function
 */

uint8_t send_data_test(
                    uint8_t retv        // ret value
                ,   void* data          // data to send
                ,   uint32_t len        // length of the data
                )
{
    return retv;
}

void print_four_boolean(const uint8_t retv)
{
    four_boolean::resolver r(retv);
    switch (four_boolean::booler(r)) {
        case four_boolean::TRUE:
            std::cout
                << "Pure True;"
                << " data were sent - no errors;"
                << " Err Code: " << (int)r.err_code() << std::endl;
            break;
        case four_boolean::FALSE:
            std::cout
                << "Pure FALSE;"
                << " data were not sent due to:"
                << " Err code: " << (int)r.err_code() << std::endl;
            break;
        case four_boolean::TRUE_AND_FALSE:
            std::cout
                << "And True and False;"
                << " data were sent, but there were some errors:"
                << " Err Code: " << (int)r.err_code() << std::endl;
            break;
        case four_boolean::NTRUE_NFALSE:
            std::cout
                << "Neither True Nor False;"
                << " something unpredictable happened."
                << " it is not possible to make a statement on this;"
                << " Err Code: " << (int)r.err_code() << std::endl;
            break;
    }
}

int main()
{
    uint8_t retv = 23;
    std::cout << retv;
    /**
     * case 1
     * pure TRUE
     */
    print_four_boolean(send_data_test(1, 0, 0));

    /**
     * case 2
     * pure FALSE
     * 34 means err code 17
     */
    print_four_boolean(send_data_test(34, 0, 0));

    /**
     * case 3
     * AND TRUE and FALSE
     * 99 means err code 49
     */
    print_four_boolean(send_data_test(99, 0, 0));

    /**
     * case 4
     * Neither TRUE nor FALSE
     */
    print_four_boolean(send_data_test(0, 0, 0));

    return 0;
}

The Output

$ g++ ./four-boolean.cpp -o ./four-boolean
$ ./four-boolean
Pure True; data were sent - no errors; Err Code: 0
Pure FALSE; data were not sent due to: Err code: 17
And True and False; data were sent, but there were some errors: Err Code: 49
Neither True Nor False; something unpredictable happened. 
It is not possible to make a statement on this; Err Code: 0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)