Skip to main content
gamedev.net
🔒 Locked

C++ logical operators

Started by spec 9 replies 1k views
Original Post
spec
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.
Spec
smart_idiot
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]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
simba
simba
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]
smart_idiot
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.

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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
H0bbes
H0bbes
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]
Agony
Agony
Can you describe some of your confusions in more depth by any chance, if you''re still confused, that is.


int main() { return *((int*)0); }
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
Fruny
Fruny
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]
"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
smart_idiot
smart_idiot
It would be easier to just use != than your function, but thanks for demonstrating why there is no ^^ operator.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Fruny
Fruny
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]
"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
smart_idiot
smart_idiot
*smacks head on wall*
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
Teh Evil
Teh Evil
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 ^^
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.