View Single Post
  #7   Spotlight this post!  
Unread 12-09-2011, 13:41
Jon Stratis's Avatar
Jon Stratis Jon Stratis is offline
Electrical/Programming Mentor
FRC #2177 (The Robettes)
Team Role: Mentor
 
Join Date: Feb 2007
Rookie Year: 2006
Location: Minnesota
Posts: 3,753
Jon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond reputeJon Stratis has a reputation beyond repute
Re: Simply question about C program

Sometimes it's also helpful to use "truth tables" when dealing with logical operations (especially as they become more complex). Examples of the three operations being discussed here, where T indicates that the argument is logically TRUE (2+2 == 4), and F indicates it is logically FALSE (2+2 == 5)
Code:
 A  |  B  | A OR B
--------------------
 T  |  T  |    T
 T  |  F  |    T
 F  |  T  |    T
 F  |  F  |    F

 A  |  B  | A AND B
--------------------
 T  |  T  |    T
 T  |  F  |    F
 F  |  T  |    F
 F  |  F  |    F

 A  | NOT A
------------
 T  |    F
 F  |    T
This way, you show all possible combinations of A and B (where both are true, A is true and B is false, A is false and B is true, and both are false), and can join various combinations of operations together quite easily by simply adding an operation on columns already existing. For example, if we wanted the combined logical operation A OR ((NOT B) AND C), We first evaluate NOT B, then once we have that column, we can use it to evaluate (NOT B) AND C, and finally with that column we can evaluate the whole thing:

Code:
 A  |  B  |  C  |  NOT B  |  (NOT B) AND C  | A OR ((NOT B) AND C)
-------------------------------------------------------------------
 T  |  T  |  T  |    F    |        F        |         T
 T  |  T  |  F  |    F    |        F        |         T
 T  |  F  |  T  |    T    |        T        |         T
 T  |  F  |  F  |    T    |        F        |         T
 F  |  T  |  T  |    F    |        F        |         F
 F  |  T  |  F  |    F    |        F        |         F
 F  |  F  |  T  |    T    |        T        |         T
 F  |  F  |  F  |    T    |        F        |         F
So, we know that there are exactly 5 combinations of A, B and C that result in a "true" result, and 3 that do not.

I had a sophomore-level class in college that spent a lot of time on stuff like this. There are rules for simplifying logic statements and such that can be very powerful, but essentially rely on analysis like this to know what conditions lead to a True or False result.

Also a note on OR: With very few exceptions, OR means "Inclusive OR" - that it's true in every situation except when both arguments are false. Just about everything I've seen then makes a very distinctive difference for an XOR, or "Exclusive OR" operation - where it's only true if the two arguments are different - one is true and one is false. Essentially A XOR B = (A OR B) AND NOT (A AND B)

Last edited by Jon Stratis : 12-09-2011 at 13:50.