Basic question

Started by
5 comments, last by HeyHoHey 16 years, 11 months ago
Hi, im new to these forums and i just started learning C++. Right now one of the quiz questions confuses me. Even though i can look at the answer i dont understand the logic behind the experssion. I think this is on boonlean operators. Here is the question. Evaluate !(1 && !(0 || 1)). So im thinking about this i know and comes before or right? so its not 1 and not not 0 or 1? even if what i just typed up there is right if anyone could explain it to me so i can understand i would appreciate this. oh yea and true is = 1 and false is = 0 correct? thanks hey
Advertisement
Quote:So im thinking about this i know and comes before or right? so its not 1 and not not 0 or 1?

That's the word for word translation. The boolean logic to plain english translation comes out a little modified. Let's break down (0 || 1).

0 or 1.

Adding in the implied english:

(true if) 0 (is true) or 1 (is true).

This can be generalized:

<condition 1> <boolean operator> <condition 2>: true if <condition 1> is true, <boolean operator> <condition 2> is true.

The same basic premise applies to unary logic:

not <condition 1>: true if not <condition 1>

Quote:oh yea and true is = 1 and false is = 0 correct?

That's a bad way to think of it -- not all languages treat true as 1 (they may use -1 instead, for example).

Instead, we say:
In C++, 0 is false
In C++, N is true (for N == any number other than 0)

There are a lot of other things that are false and true as well.



Now, back to your original problem:

Evaluate !(1 && !(0 || 1)).

This evaluates to true if:
!        This is not true:    (1       1 is true    &&       and    !        This is not true:        (0       0 is true        ||       or        1)       1 is true    )
we can start by evaluating (0 || 1) which evaluates to true

thus we get

!(1 && !true)

1 evaluates to true aswell ofcourse :)

thus we get

!(true && !true)

!true evaluates to false

thus

!(true && false)

true && false evaluates to false

thus we have !(false) which evaluates to true.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
!(1 && !(0 || 1))
!(1 && !(1))
!(1 && 0)
!(0)
1

1 is true,
0 is false
[ my blog ]
Moving you to For Beginners.

- Jason Astle-Adams

Quote:Original post by HeyHoHey
So im thinking about this i know and comes before or right?
Logical 'and' does precede logical 'or', but parentheses precede them both, so this isn't a consideration. I think everything else has been covered.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
Okay thank you everyone I'm pretty sure I understand it now. Also sorry about posting in the wrong forum. :)

Thanks again for the responses/answers.
Hey

This topic is closed to new replies.

Advertisement