Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Creating a simple boolean logic calculator in C

  1. Sep 28, 2016 #1
    Code (Text):

    #include <stdio.h>

    int main()
        int A; int B;
        printf("This is a simple boolean truth value calculator.\n\n");
        printf("Please input the boolean value for statement A\n\n\n");
        scanf("%d", &A); printf("Press enter to continue\n\n\n");
        printf("Please input the boolean value of the statement B\n\n\n");
        scanf("%d", &B);
        int C = A + B;
        printf("Press enter to calculate the boolean value of their disjunction\n\n\n");
        printf("A AND B: %d", A*B);
        printf("A OR B: %d", C);
        printf("Press enter to exit program");
        return 0;
    I need to make the variable C to be only 0 or 1...not TWO when A and B are 1 and their disjunction gives 2.

    Please use simple language as much as you can, I am very new!

    thank you!: D
    Last edited by a moderator: Sep 28, 2016
  2. jcsd
  3. Sep 28, 2016 #2


    User Avatar

    Staff: Mentor

    Look up "logical operators in C" or "boolean operators in C". The '+' and '*' operators are not the ones that you need.
  4. Sep 28, 2016 #3
    oh alright
    thank you.
  5. Sep 29, 2016 #4
    I'm confused, neither of your representations are correct. You don't multiple values to do a logical AND and you don't add them to do a logical OR.

    C also has build in logical operators: || (OR), && (AND), ! (NOT). Just be careful when you google because & and && both mean AND, but in very different ways so make sure you know the difference between bitwise operator and plain operators.
  6. Sep 29, 2016 #5


    Staff: Mentor

    Elaborating on what NJrunner said, && and || are, respectively, logical AND and logical OR. & and | are, respectively, bitwise AND and bitwise OR.
  7. Oct 2, 2016 #6
    I want to make the user only able to input two values, 0 or 1 (as they should)....how do I go about this? sorry for late reply, I was busy.

    nd thanks for the answers..dont know what bitwise means? is it like exclusive and inclusive AND ?
  8. Oct 2, 2016 #7


    Staff: Mentor

    Inside a while loop, prompt the user to input either a 0 or a 1. Use scanf(), as you did in the code above, to read the input. If the input value is a 0 or 1, exit the loop. If it's any other value, start another iteration of the while loop.
    Bitwise means that the operation is performed on the respective bits of two different numbers. There is no such thing as exclusive AND and inclusive AND - but there are exclusive and inclusive OR.

    Here's how some of the bitwise operations work. Suppose A is set to 01011001 and that B is set to 00001111.
    01011001 (A)
    00001111 (B)
    00001001 (A & B) If both of the bits in a particular position of A and B are 1, then the bit in that position of the result will be 1; otherwise it is 0.
    01011111 (A | B) If either bit or both bits in a particular position of A and B are 1, then the bit in that position of the result will be 1; otherwise it is 0.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted