Creating a simple boolean logic calculator in C

In summary: A ^ B) The bitwise exclusive OR (XOR) of the bits in a particular position of A and B is the result of inverting the bits in that position of each number and then combining them.
  • #1
Logical Dog
362
97
Code:
#define _CRT_SECURE_NO_DEPRECATE
#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");
    getchar();
    printf("Please input the boolean value of the statement B\n\n\n");
    scanf("%d", &B);
    int C = A + B;
    getchar();
    printf("Press enter to calculate the boolean value of their disjunction\n\n\n");
    getchar();
    printf("A AND B: %d", A*B);
    printf("A OR B: %d", C);
    getchar();
    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:
Technology news on Phys.org
  • #2
Look up "logical operators in C" or "boolean operators in C". The '+' and '*' operators are not the ones that you need.
 
  • Like
Likes Logical Dog
  • #3
jtbell said:
Look up "logical operators in C" or "boolean operators in C". The '+' and '*' operators are not the ones that you need.
oh alright
thank you.
 
  • #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.
 
  • Like
Likes Logical Dog
  • #5
newjerseyrunner said:
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.
Elaborating on what NJrunner said, && and || are, respectively, logical AND and logical OR. & and | are, respectively, bitwise AND and bitwise OR.
 
  • Like
Likes Logical Dog
  • #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 ?
 
  • #7
Bipolar Demon said:
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.
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.
Bipolar Demon said:
nd thanks for the answers..dont know what bitwise means? is it like exclusive and inclusive AND ?
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.
 

1. What is a boolean logic calculator?

A boolean logic calculator is a tool that can perform logical operations using boolean values (true or false). It can help you determine the truth value of complex statements by evaluating the individual boolean values and applying logical operators such as AND, OR, and NOT.

2. Why use C for creating a boolean logic calculator?

C is a popular and powerful programming language that provides low-level control over hardware, making it well-suited for developing efficient and fast programs. Additionally, C has built-in support for boolean data types and logical operators, making it a suitable choice for creating a boolean logic calculator.

3. How do I create a simple boolean logic calculator in C?

To create a simple boolean logic calculator in C, you can follow these steps:

  1. Start by defining boolean variables and assigning them true or false values.
  2. Use conditional statements (such as if-else) to evaluate the boolean values and perform logical operations.
  3. Use logical operators (such as && for AND, || for OR, and ! for NOT) to combine the boolean values and obtain the desired result.
  4. Repeat these steps for different logical operations and display the results.

4. Can I add more complex logical operations to my boolean logic calculator?

Yes, you can add more complex logical operations to your boolean logic calculator by using nested conditional statements and/or logical operators. You can also add more variables and expand the scope of your calculator to handle more complex boolean expressions.

5. How can I test my boolean logic calculator to ensure it is working correctly?

You can test your boolean logic calculator by providing different sets of boolean values and checking the output against the expected results. You can also use various test cases, including edge cases, to verify the correctness of your program. Additionally, you can use a debugger or print statements to track the flow of your program and identify any potential errors.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
2
Views
930
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
4
Views
896
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
993
Back
Top