-2

Sorry for asking you guys and not my professor. (its due in a couple of hours and she is not available) but I am just looking for a point in the right directions.

one piece of what I am writing tonight states two things...

Single-character versions of the C logical operators:

& for && (logical AND)
| for || (logical OR)
! for logical NOT

and this... as example output.

Enter an expression: (0 & 1) | (1 & 1)
Result: (0 & 1) | (1 & 1) = true

My pseudo logic is to take the input of '&' or '|' and have it return as '&&' or '||' and throw it all back together based on the entered expression and let the program do the math.

But what I don't understand is what would make the above expression evaulwate to true? and or false? What should I research to lean more about the above expression? and what makes it true or false?

Jonas
  • 121,568
  • 97
  • 310
  • 388
RandomNumberFun
  • 638
  • 2
  • 8
  • 25

3 Answers3

2

Look at it this way:

(0 & 1) | (1 & 1)

= (0 and 1) or (1 and 1)

= (false and true) or (true and true)

= (false) or (true)

= true

Further reading at wikipedia

Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
2

The concept of true and false in C is of integers 0 for false and a non-zero number for true.

"single" boolean operators like &, |, ~ and ^ are called bitwise operators.

They work on two numbers, bit by bit - following the logic tables respectively - AND, OR, ONE-COMPLEMENT(not) and XOR.

So, your expression:

(0 & 1) | (1 & 1) is true because 0 & 1 = 0, 1 & 1 = 1 and 0 | 1 is 1. which is true.

What could turn this into false is if the expression was combined with an & instead of | like so:

(0 & 1) & (1 & 1) = false.

Truth table of AND:

+---------------+---------+
|  A    +   B   |   A & B |
+---------------+---------+
| 0     |   0   |    0    |
| 0     |   1   |    0    | 
| 1     |   0   |    0    |
| 1     |   1   |    1    |
+---------------+---------+

Truth table of OR:

+---------------+---------+
|  A    +   B   |   A | B |
+---------------+---------+
| 0     |   0   |    0    |
| 0     |   1   |    1    | 
| 1     |   0   |    1    |
| 1     |   1   |    1    |
+---------------+---------+

However, since C99, true and false are reserved words, and will evaluate to 0 and 1 internally - The answer posted above applies for C99 as well as C89

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • your answer is a bit misleading. Your first setence is correct for the thruth values "false" and "true", but not for the reserved identifiers `false` and `true`, in particular for `true`. Since C99, this is supposed to be a macro that evaluates to `1`. – Jens Gustedt Mar 17 '13 at 08:25
  • @JensGustedt you are right - I must clarify this in my answer – Aniket Inge Mar 17 '13 at 08:28
-1

Thanks for everyone's input. Really helped.

int logicalCheck (int a, int x, int b) {

// let 88 = true
//  let 89 = false

if (x == '&') {
    if (a == b) {
        return 88;
    }
    if (a != b) {
        return 89;
    }

}

if (x == '|') {
    if ((a == b && a) || b != 0) {
        return 88;
    }
    if (a || b == 0) {
        return 89;
    }

}

return 0;
RandomNumberFun
  • 638
  • 2
  • 8
  • 25
  • 2
    Your result is incorrect. It would resolve to 0 & 0 being 1. Actually it is even worse and entirely incorrect. It would only work with the given example and that is just an example. Your professor will use different values for the actual test and you will need to get the entire boolean logic correct. Additionally, you entirely missed the ! (not). – Till Mar 17 '13 at 05:24