• Advertisement
Sign in to follow this  

Basic question

This topic is 3948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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
)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
!(1 && !(0 || 1))
!(1 && !(1))
!(1 && 0)
!(0)
1

1 is true,
0 is false

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement