Boolean characters

• 11-14-2006
andrewclone
Boolean characters
hi I'm andrew.
I'm new here, I've been reading the tutorials but I haven't gone past the second because the answers to the questions arent obvious to me. I was wondering if somebody could explain to me the reason for the answers please.:D

A. !( 1 || 0 ) ANSWER: 0
B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)
C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)
• 11-14-2006
Daved
I'll start with the first one. Always start from the inside of parentheses. The || operator returns true (which is 1 in this case) if either side of the expression is true. So 1 || 0 evaluates to 1 because true OR false evaluates to true. That leaves !(1). The parentheses are not necessary there since there is a single value inside, so that leaves !1. The ! operator evaluates to the opposite of the expression. So !true is false and !false is true. In this case, that means that !1 is 0.

Now how would you get the answer to the other two based on that and what you learned from the tutorial?
• 11-14-2006
andrewclone
C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

1 || 0 = 1 =0

0 || 0 =1

is this a correct disection of the problem? is 0 || 0 = 1?

I really apreciate your help
• 11-14-2006
dwks
0 || 0 = 0. English: false or false. Does false or false sound true to you? (Two wrongs . . . .) :)

 To solve it, follow Daved's advise.
Quote:

Always start from the inside of parentheses.
!( ( 1 || 0 ) && 0 )

Solve 1||0, and call that A.

!( A && 0)

Solve A&&0, and call that B.

!B

Then solve that . . .
[/edit]
• 11-14-2006
Daved
>> 1 || 0 = 1 =0
Ok, explain in words what you are thinking here.

>> 0 || 0 =1
Remember, operator || evaluates to 1 only if one side or the other side is 1. Also, where did the 0 || 0 come from in the problem? There is a big and important difference between || and &&.
• 11-14-2006
dwks
From the tutorial:
Quote:

NOT: The NOT operator accepts one input. If that input is TRUE, it returns FALSE, and if that input is FALSE, it returns TRUE. For example, NOT (1) evalutes to 0, and NOT (0) evalutes to 1. NOT (any number but zero) evaluates to 0. In C NOT is written as !. NOT is evaluated prior to both AND and OR.

AND: This is another important command. AND returns TRUE if both inputs are TRUE (if 'this' AND 'that' are true). (1) AND (0) would evaluate to zero because one of the inputs is false (both must be TRUE for it to evaluate to TRUE). (1) AND (1) evaluates to 1. (any number but 0) AND (0) evaluates to 0. The AND operator is written && in C. Do not be confused by thinking it checks equality between numbers: it does not. Keep in mind that the AND operator is evaluated before the OR operator.

OR: Very useful is the OR statement! If either (or both) of the two values it checks are TRUE then it returns TRUE. For example, (1) OR (0) evaluates to 1. (0) OR (0) evaluates to 0. The OR is written as || in C. Those are the pipe characters. On your keyboard, they may look like a stretched colon. On my computer the pipe shares its key with \. Keep in mind that OR will be evaluated after AND.
That explains it pretty well. If you don't get it, try looking at some truth tables:
Code:

```  AND 0 && 0 = 0 0 && 1 = 0 1 && 0 = 0 1 && 1 = 1   OR 0 || 0 = 0 0 || 1 = 1 1 || 0 = 1 1 || 1 = 1   NOT !0 = 1 !1 = 0```
If you still don't get it, say why.
• 11-14-2006
andrewclone
Quote:

Originally Posted by Daved
>> 1 || 0 = 1 =0
one or zero equals one and with the not (!) I musta change the answer to the oposite, that being 0
Ok, explain in words what you are thinking here.

>> 0 || 0 =1 correction 0 && 0 = 0 =1 because of !(first zero representing the first equation and the second representing the original && 0 and then the answer being changed by !)
Remember, operator || evaluates to 1 only if one side or the other side is 1. Also, where did the 0 || 0 come from in the problem? There is a big and important difference between || and &&.

Questiong: does the ! affect the answer of !( ( 1 || 0 ) && 0 ) ?
• 11-14-2006
dwks
No. ( 1 || 0 ) is evaluated by itself first, an expression unto itself. The result of that is &&ed with 0. The result of that is inverted.
• 11-14-2006
andrewclone
Quote:

Originally Posted by dwks
No. ( 1 || 0 ) is evaluated by itself first, an expression unto itself. The result of that is &&ed with 0. The result of that is inverted.

perfect!!!! I got it.

Thank you guys and thank you for you patience.

Hope I can return the favor!