C语言 && 逻辑和 || 的真假 逻辑表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7583853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:44:37  来源:igfitidea点击:

True and False for && logic and || Logic table

ctruthtable

提问by Ali

Table true/false for C Language

C语言表真/假

I have heard of a table true false for C Language for and && or || is kind of the mathematics one for which they say if true+true=true and false+true=false

我听说 C 语言中有一个表 true false for and && or || 是一种数学,他们说如果 true+true=true 和 false+true=false

I'm just kind of confuse on this and I tried to do the research but couldn't find any of the table

我只是对此感到困惑,我试图进行研究,但找不到任何表格

I just wish to have this table for my notes since I will do more in C language

我只是希望有这张桌子作为我的笔记,因为我会用 C 语言做更多的事情

if someone could bring me to the site or resources where they explain about this more

如果有人可以带我到他们对此进行更多解释的站点或资源



I've edited my original question to make it a note for my own study. Thanks @thiton for the great references and the rest for an awesome answer/resources.

我已经编辑了我的原始问题,以作为我自己学习的笔记。感谢 @thiton 提供了很好的参考资料,其余部分提供了很棒的答案/资源。

Logical AND (&&)

Logical AND (&&)

false && false: false

false && false: false

false && true: false

false && true: false

true && false: false

true && false: false

true && true: true

true && true: true

Logical OR (||)

逻辑或 (||)

false || false: false

false || false: false

false || true: true

false || true: true

true || false: true

true || false: true

true || true: true

true || true: true

Logical NOT (!)

逻辑非 (!)

!false: true

!false: true

!true: false

!true: false

采纳答案by thiton

You probably mean a truth table for the boolean operators, which displays the result of the usual boolean operations (&&, ||). This table is not language-specific, but can be found e.g. here.

您可能指的是布尔运算符的真值表,它显示通常的布尔运算(&&、||)的结果。该表不是特定于语言的,但可以在例如此处找到。

回答by John Bode

You're thinking of Boolean algebra.

您正在考虑Boolean algebra

回答by pmr

Truth values can be described using a Boolean algebra. The article also contains tables for andand or. This should help you to get started or to get even more confused.

真值可以使用布尔代数来描述。该文章还包含and和 的表格or。这应该可以帮助您入门或变得更加困惑。

回答by matekm

I think You ask for Boolean algebrawhich describes the output of various operations performed on boolean variables. Just look at the article on Wikipedia.

我认为您要求布尔代数,它描述了对布尔变量执行的各种操作的输出。看看维基百科上的文章就知道了。

回答by M. D. H.

I`d like to add to the already good answers:

我想补充已经很好的答案:

The symbols '+', '*' and '-' are sometimes used as shorthand in some older textbooks for OR,∨ and AND,∧ and NOT,? logical operators in Bool`s algebra. In C/C++ of course we use "and","&&" and "or","||" and "not","!".

符号“+”、“*”和“-”有时在一些较旧的教科书中用作 OR、∨ 和 AND、∧ 和 NOT,? Bool 代数中的逻辑运算符。在 C/C++ 中,我们当然使用“and”、“&&”和“or”、“||” 并不是”,”!”。

Watch out:"true + true" evaluates to 2in C/C++ via internal representation of true and false as 1 and 0, and the implicit cast to int!

注意:通过将 true 和 false 内部表示为 1 和 0,以及隐式转换为 int,“true + true”在 C/C++ 中的计算结果为2

int main ()
{
  std::cout <<  "true - true = " << true - true << std::endl;
// This can be used as signum function:
// "(x > 0) - (x < 0)" evaluates to +1 or -1 for numbers.
  std::cout <<  "true - false = " << true - false << std::endl;
  std::cout <<  "false - true = " << false - true << std::endl;
  std::cout <<  "false - false = " << false - false << std::endl << std::endl;

  std::cout <<  "true + true = " << true + true << std::endl;
  std::cout <<  "true + false = " << true + false << std::endl;
  std::cout <<  "false + true = " << false + true << std::endl;
  std::cout <<  "false + false = " << false + false << std::endl << std::endl;

  std::cout <<  "true * true = " << true * true << std::endl;
  std::cout <<  "true * false = " << true * false << std::endl;
  std::cout <<  "false * true = " << false * true << std::endl;
  std::cout <<  "false * false = " << false * false << std::endl << std::endl;

  std::cout <<  "true / true = " << true / true << std::endl;
  //  std::cout <<  true / false << std::endl; ///-Wdiv-by-zero
  std::cout <<  "false / true = " << false / true << std::endl << std::endl;
  //  std::cout <<  false / false << std::endl << std::endl; ///-Wdiv-by-zero

  std::cout <<  "(true || true) = " << (true || true) << std::endl;
  std::cout <<  "(true || false) = " << (true || false) << std::endl;
  std::cout <<  "(false || true) = " << (false || true) << std::endl;
  std::cout <<  "(false || false) = " << (false || false) << std::endl << std::endl;

  std::cout <<  "(true && true) = " << (true && true) << std::endl;
  std::cout <<  "(true && false) = " << (true && false) << std::endl;
  std::cout <<  "(false && true) = " << (false && true) << std::endl;
  std::cout <<  "(false && false) = " << (false && false) << std::endl << std::endl;

}

yields :

产量:

true - true = 0
true - false = 1
false - true = -1
false - false = 0

true + true = 2
true + false = 1
false + true = 1
false + false = 0

true * true = 1
true * false = 0
false * true = 0
false * false = 0

true / true = 1
false / true = 0

(true || true) = 1
(true || false) = 1
(false || true) = 1
(false || false) = 0

(true && true) = 1
(true && false) = 0
(false && true) = 0
(false && false) = 0