Sign in to follow this  
HeyHoHey

Basic question

Recommended Posts

HeyHoHey    122
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
MaulingMonkey    1730
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
SimonForsman    7642
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
TheAdmiral    1122
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
HeyHoHey    122
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this