Microsoft 更喜欢 False 值?

发布于 2025-01-04 23:33:09 字数 475 浏览 0 评论 0原文

我正在读乔恩·斯基特的书。 (#4)

但有一件事(除其他外)引起了我的注意:

主题:布尔?

他在一张表中写道:(X,Y 是布尔值?)

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

好吧……所以 null 是决定者。 这里的 bool 操作数丢失了。

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

为什么? 为什么这里要考虑 bool 操作数,而在前面的示例中,是 null 决定结果......?

看来真假有朋友在不同的地方......:)

Im reading Jon Skeet book. (#4)

but one thing (among others) caught my eye :

topic : bool?

he wrote in a table that :(X,Y are bool?)

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

ok fine... so the null is the one who decides.
the bool operand here looses.

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

why ?
why the bool operand here is being taking into account while in the previous sample it was the null who decided about the result ...?

It seems that true and false has friends in different places....:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枕头说它不想醒 2025-01-11 23:33:09

null发音为未知

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

当然还有 OR 的镜像表:

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

它也适用于 &&||

Pronounce null as unknown.

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

And of course there is the mirror table for OR :

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

And it holds for && and || as well.

胡大本事 2025-01-11 23:33:09

如此设计的原因是为了与实现的三值逻辑保持一致在许多其他平台上,包括 SQL。在 Kleene 逻辑(三值逻辑所基于的)中,TRUE AND UNKNOWN 给出 UNKNOWN,而 FALSE AND UNKNOWN 给出 FALSE

您可以参考标题为“布尔值? 使用可空类型(C# 编程指南) 中的“type”有关 C# 中结果的解释和枚举。

The reason it was designed that way is to be consistent with three-valued logic, as implemented in numerous other platforms, including SQL. In Kleene logic (on which three-valued logic is based), TRUE AND UNKNOWN gives UNKNOWN, while FALSE AND UNKNOWN gives FALSE.

You may refer to the section titled “The bool? type” within Using Nullable Types (C# Programming Guide) for an explanation and enumeration of the results in C#.

只有影子陪我不离不弃 2025-01-11 23:33:09

根据微软的说法,这是...

确保&产生的结果和|运算符与SQL中的三值布尔类型一致

按位运算的真值表可以在此页面上找到可为 null 的布尔值

According to Microsoft, it is...

To ensure that the results produced by the & and | operators are consistent with the three-valued Boolean type in SQL

The truth table for bitwise operations on nullable booleans can be found on this page.

尝蛊 2025-01-11 23:33:09

原因是您现在正在处理三值逻辑

  • 对于 AND 运算符(无论是 & 或 &&),可以在第一次匹配 FALSE 时确定表达式结果。
  • 对于 OR 运算符(无论是 | 或 ||),可以在第一次匹配 TRUE 时确定表达式结果。
  • 对于最左边操作数为 NULL 的任何布尔运算,都将返回 NULL。

因此,您列出的表格并不完整:您需要更多条目来查看何时会发生什么,因为有 8 种可能的输入组合(如果算上 null 运算符 null 组合,则有 9 种)。

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

使用您的朋友类比,您可以说,对于三值逻辑,AND 倾向于 FALSE 和 NULL 作为结果,而 OR 倾向于 TRUE 和 NULL。

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

  • for an AND operator (be it & or &&) the expression result can be determined at the first match for FALSE.
  • for an OR operator (be it | or ||) the expression result can be determined at the first match for TRUE.
  • for any boolean operation having a left most operand of NULL will return NULL.

So the tables you list are incomplete: you need more entries to see what happens when, as there are 8 possible input combinations (nine if you count 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 could say that, for three-valued logic, AND favours FALSE and NULL as outcome whereas OR favours TRUE and NULL.

青萝楚歌 2025-01-11 23:33:09

&&和 ||运营商均根据短路评估进行评估。这意味着一旦确定复杂表达式为假,则不会检查其余表达式。由于第二个表达式中的假值足以确定逻辑与的结果,因此直接将结果确定为假。

The && and || operators are both evaluated according to short circuit evaluation. This means once a complex expression has been determined to be false, the remaining expressions will not be checked. Since false value in second expression is enough to determine the result of logical AND, result is determined as false directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文