Is Microsoft Likes the False better?

I am reading a book by John Skeet. (# 4)

but one thing (among others) caught my attention:

topic: bool?

he wrote in the table that: (X, Y - bool?)

X | Y | X & Y --------------------------- true null null 

ok fine ... so zero is the one who decides. the bool guardian is losing here.

 X | Y | X & Y --------------------------- false null false 

why? why is the bool operand taken into account here, while in the previous example it was zero that decided on the result ...?

True and false seem to have friends in different places .... :)

+4
source share
5 answers

Say null as unknown.

 true & unknown => unknown false & unknown => false because the second operand does not matter. 

And, of course, there is a mirror table for OR:

 true | unknown => true because the second operand does not matter. false | unknown => unknown 

And it is saved for && and || .

+10
source

The reason it was designed this way should be compatible with three-valued logic , as it is implemented on many other platforms, including SQL. In Klein's logic (on the basis of which three-valued logic is based), TRUE AND UNKNOWN gives UNKNOWN , and FALSE AND UNKNOWN gives FALSE .

You can refer to the " " Type bool " section in Using Nullable Types (C # Programming Guide) to explain and list the results in C #.

+3
source

According to Microsoft, this is ...

To ensure that the results obtained using and and | operators are consistent with a three-digit boolean type in SQL

The truth table for bitwise operations with zero Boolean values ​​can be found on this page .

+1
source

The reason is that now you are dealing with three-valued logic .

  • for the AND (be it and or &) operator, the result of the expression can be determined in the first match for FALSE.
  • for the OR operator (be it | or ||), the result of the expression can be determined in the first match for TRUE.
  • for any logical operation that has the leftmost operand of NULL, will return NULL.

So the tables you have given are incomplete: you need more entries to find out what happens when there are 8 possible input combinations (nine if you consider the null operator null combination).

and

 X | Y | X & Y --------------------------- true null null null true null false null false null false null true false false true true true false true false false false false 

OR

 X | Y | X | Y --------------------------- true null true null true null false null null null false null true false true true true true false true true false false false 

Using your friends analogy, you can say that for three-valued logic, And stands for FALSE and NULL as the result, while OR supports TRUE and NULL.

+1
source

& & and || Operators are rated according to short circuit rating . This means that if a complex expression is defined as false, the rest of the expressions will not be checked. Since a false value in the second expression is enough to determine the result of a logical AND, the result is defined as false directly.

0
source

Source: https://habr.com/ru/post/918707/


All Articles