Basic question
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
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.
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.
Quote:Original post by HeyHoHeyLogical 'and' does precede logical 'or', but parentheses precede them both, so this isn't a consideration. I think everything else has been covered.
So im thinking about this i know and comes before or right?
Admiral
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement