67

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+true=true and 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

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.

Logical AND (&&)

false && false: false

false && true: false

true && false: false

true && true: true

Logical OR (||)

false || false: false

false || true: true

true || false: true

true || true: true

Logical NOT (!)

!false: true

!true: false

3
  • 4
    You really cannot come up with the truth table for AND and OR for a single boolean? Give it a try at least...
    – Kerrek SB
    Sep 28, 2011 at 13:20
  • 1
    "true" and "false" were introduced as macros with C99. true evaluates to 1 and false to 0, so true+false = 1+0 = 1 (== true), I'm not sure if true+true is valid. As that's 1+1, which is no valid value for _Bool. But don't have the standard at hand.
    – johannes
    Sep 28, 2011 at 13:23
  • 1
    Here's something that's good to know, although it has little practical use in programming: since false is evaluated to 0 and true to 1, you can (but shouldn't) replace && with * and || with +. Similarly, AND is equivalent to the intersection in set theory and OR is equivalent to the union. All of this was very useful in old-timer electronics design, in the days before microcontrollers, when they implemented math functions with gates. Programmers may still encounter weird legacy code where they use multiplication and addition to perform logic operations and vice versa.
    – Lundin
    Sep 28, 2011 at 13:59

5 Answers 5

18

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.

1
  • Yes this what been taught in my class and I really was looking for this one! :D Thank you so much I'm sure going to have this note in my notes
    – Ali
    Sep 28, 2011 at 13:25
8

You're thinking of Boolean algebra.

6

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

5

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

4

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.