15,796,904 members
Articles / Programming Languages / C

# Four Boolean Logic

Rate me:
14 Apr 2018CPOL1 min read 12.1K   6   11
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```

Written By
Software Developer (Senior)
Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

 First Prev Next
 Don't think we need it. Paulo Zemek16-Apr-18 11:50 Paulo Zemek 16-Apr-18 11:50
 Two booleans will naturally map 4 values. I don't think a tribool is really a boolean, but I see its value for cases like: True, False, Unset/Unknown etc. This could be accomplished by two booleans too, but one of them would be telling: Yes, there's a value, so accept what's on the second boolean, while the other boolean would be simply ignored if the first one is false (and guaranteeing that we never use it when we don't have a value for it might be a problem, so having a tribool is useful). But having a 4-bool? At least by your example, having two booleans seems easier. sent and had_errors are quite clear.
 Re: Don't think we need it. Rick York20-Apr-18 12:30 Rick York 20-Apr-18 12:30
 Seems not to explain the potential of the logic Avitevet16-Apr-18 11:35 Avitevet 16-Apr-18 11:35
 Re: Seems not to explain the potential of the logic Paulo Zemek17-Apr-18 11:39 Paulo Zemek 17-Apr-18 11:39