# Thread: Newbie question !( 1 || 0 ) - ?

1. ## Newbie question !( 1 || 0 ) - ?

I was going through Lesson 2 in the tutorials. At the end, it has some statements ~
Code:
```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)```
What does the ! do here?
How does 1 && 0 evaluate to 0? (previous paragraph in tutorial)

Thanks

2. ! means not

for example:
Code:
```a = 1;
if( a != 3 )
{
cout << "blah Blah" << endl;
}

//OUTPUT:

blah blah

//because a  is not 3..```

3. > How does 1 && 0 evaluate to 0?
How can something be true and false?

- SirCrono6

4. Originally Posted by SirCrono6
> How does 1 && 0 evaluate to 0?
How can something be true and false?

- SirCrono6
Maybe I'm reading into it too much..

All I get from !(1 && 0) is "Not(True AND False)" I don't see how an answer can come from that

I understand If statments quite well

5. Hi,

AND requires both terms to be true for the result to be true, where OR only requires one term to be true for the result to be true. So, starting with the parentheses:

(True AND False)

you get:

(false)

because both terms aren't true. Then, NOT reverses the result, so

!(false)

gives you true.

!(true OR false)

since OR only requires one term to be true for the result to be true, you would get:

!(true)

which is false.

6. Don't read too much into the names given to the logical operators ('and', 'or' ... especially 'or'). The meanings of these words in English does not completely correspond to their logical meanings, just take them as definitions.

p && q is true if and only if p is true and q is true
p || q is false if and only if p is false and q is false
!p is true if and only if p is false

Cheers

7. Ok, that makes sense now.. Thanks for clarifying

8. i'm sure you probably know, but i haven't seen anyone point this out, so i will. anything that evaluates to zero evaluates to false;
anything that evaluates to anything that's NOT zero, evaluates to true.

if you REALLY want to learn about logic operators, i suggest reading some ASM tutorials concerning bitwise operations.

just a little example of logic operators in it's binary form
Code:
```1010
1000 &&
----------
1000   = true (not zero)  //  !1000 = false

1010
0100 ||
---------
1110  = true (not zero)  //  !1110 = false;

0101
0101 XOR //i forget the XOR operator in c/c++ (^ maybe?)
------------
0000  = false (zero)    //  !0000 = true;

for you specific question -

0001
0000 ||
------------
0001 true  // !0001 = 0000 = false```

9. I dont think there is a XOR operator availible in c++..some people say its a flaw..hu knows..a quick and easy work around is:

bool p, q;

(p || q) && !(p && q)

producing an xclusive OR..

cheers

Alex

10. > I dont think there is a XOR operator availible in c++
Of course there is, and it's ^

11. is that quite a recent addition to c++?

becuase the book im reading says there isnt one..

man..i feel dumb!

cheers
Alex

12. > is that quite a recent addition to c++?
No, it's there from original C, which means it's been there forever basically.

13. Of course what Salem is failing to state is that it's bitwise XOR

14. bitwise?

cheers
Alex

15. Originally Posted by macman
bitwise?

cheers
Alex
Bitwise:

00010001 XOR 00100001

00010001
00100001
-------------
00110000

Bitwise means that the XOR operator takes a look at the individual bits. XOR, in this case 'Exclusive OR', means that only one can be true to evaluate to true.

Does that help?