Creating a simple boolean logic calculator in C

  • Context:
  • Thread starter Thread starter Logical Dog
  • Start date Start date
  • Tags Tags
    Calculator Logic
Click For Summary

Discussion Overview

The discussion revolves around creating a simple boolean logic calculator in C, focusing on the correct implementation of logical operations and user input validation for boolean values. Participants explore the use of logical operators and how to restrict user input to only 0 or 1.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant expresses a need to ensure that the variable C only takes values 0 or 1, rather than 2 when both A and B are 1.
  • Several participants suggest looking up logical operators in C, indicating that the '+' and '*' operators are not appropriate for boolean logic.
  • Another participant clarifies that logical AND and OR should use '&&' and '||', respectively, rather than multiplication or addition.
  • A participant seeks guidance on how to restrict user input to only 0 or 1, indicating a lack of familiarity with bitwise operations.
  • Another participant explains that bitwise operations are performed on the respective bits of two numbers and distinguishes between bitwise and logical operators.

Areas of Agreement / Disagreement

Participants generally agree on the need to use appropriate logical operators in C, but there is no consensus on the best method for implementing user input validation or the understanding of bitwise operations.

Contextual Notes

Some participants express confusion regarding the distinction between bitwise and logical operators, and there are unresolved questions about the implementation details for user input validation.

Logical Dog
Messages
362
Reaction score
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
Look up "logical operators in C" or "boolean operators in C". The '+' and '*' operators are not the ones that you need.
 
  • Like
Likes   Reactions: Logical Dog
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.
 
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   Reactions: Logical Dog
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   Reactions: Logical Dog
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 ?
 
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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K