🔒 Locked
C++ logical operators
Advertisement
Original Post
// equal
(false == false) == true
(false == true) == false
(true == false) == false
(true == true) == true
// not equal / xor
(false != false) == false
(false != true) == true
(true != false) == true
(true != true) == false
// not
!false == true
!true == false
// or
false || false == false
false || true == true
true || false == true
true || true == true
// and
false && false == false
false && true == false
true && false == false
true && true == true
[edited by - smart_idiot on March 10, 2004 4:46:55 PM]
quote:
Original post by smart_idiot// not
!false == true
!true == false
// or
false || false == false
false || true == true
true || false == true
true || true == true
// and
false && false == false
false && true == false
true && false == false
true && true == true
// xor
false ^ false == false
false ^ true == true
true ^ false == true
true ^ true == false
[edited by - simba on March 10, 2004 3:35:42 PM]
^ is a bitwise operator, not a logical operator, and although that will work with true and false, it won''t work right for other values.
For example, 2 is non zero, and therefore considered true, but 2 ^ 1 = 2, not the 0 you would expect if it were a logical operator.
For example, 2 is non zero, and therefore considered true, but 2 ^ 1 = 2, not the 0 you would expect if it were a logical operator.
quote:
Original post by spec
can anyone here help me? i need some good tutorials on logical operators. I have a book but its really confusing for this bit.
thanks in advance.
it's easy the number 0 is false and others is true only this.
[edited by - h0bbes on March 10, 2004 4:18:34 PM]
quote:
Original post by smart_idiot
^ is a bitwise operator, not a logical operator, and although that will work with true and false, it won't work right for other values.
inline bool xor(bool a, bool b) { return a != b; }
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
[edited by - Fruny on March 10, 2004 4:37:51 PM]
It would be easier to just use != than your function, but thanks for demonstrating why there is no ^^ operator.
quote:
Original post by smart_idiot
It would be easier to just use != than your function, but thanks for demonstrating why there is no ^^ operator.
Well no, because as you pointed out, it wouldn't work if I did 3 != 2, which is different from (bool)3 != (bool)2.
BTW, 2^1 is 3 not 2
[edited by - Fruny on March 10, 2004 4:47:58 PM]
*smacks head on wall*
quote:
Original post by smart_idiot// equal
(false == false) == true
(false == true) == false
(true == false) == false
(true == true) == true
// not equal / xor
(false != false) == false
(false != true) == true
(true != false) == true
(true != true) == false
// not
!false == true
!true == false
// or
false || false == false
false || true == true
true || false == true
true || true == true
// and
false && false == false
false && true == false
true && false == false
true && true == true
[edited by - smart_idiot on March 10, 2004 4:46:55 PM]
pretty smart, but you are actually confusing him more. you are using ( false == false ) to check equality, and have == true also to check equality?
the correct term should have been
true = ( false == false )which actually makes sense because ( false == false ) equal true, and we are not checking here
That Was Teh Evil ^^
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement