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

后端 未结 5 1630
没有蜡笔的小新
没有蜡笔的小新 2021-01-29 23:07

Table true/false for C Language

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+tru

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 23:26

    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","!".

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

    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
    

提交回复
热议问题