+ Reply to Thread
Results 1 to 5 of 5

Thread: Basics of Logic

  1. #1
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Basics of Logic

    One of the core concepts behind all of programming is logic. In particular, the boolean operators are used in almost every program that exists. While most people study arithmetic and algebra, many people do not realize that there is a similar amount of theory behind boolean logic.

    The values that can exist in boolean logic are TRUE and FALSE. The operators available are NOT, AND, OR, XOR, XNOR, NAND and NOR. They are defined as follows:
    NOT is a unary operator that returns the opposite value: NOT TRUE = FALSE, NOT FALSE = TRUE.
    AND is a binary operator that returns TRUE if both values are TRUE, otherwise it returns FALSE.
    OR is a binary operator that returns FALSE if both values are FALSE, otherwise it returns TRUE.
    XOR is a binary operator that returns TRUE if the values are different, otherwise it returns FALSE.
    XNOR is a binary operator that returns TRUE if both values are the same, otherwise it returns FALSE.
    NAND is a binary operator that returns FALSE if both values are TRUE, otherwise it returns TRUE.
    NOR is a binary operator that returns TRUE if both values are FALSE, otherwise it returns FALSE.

    If your eyes are crossing, that means you're normal. To demonstrate this, we can instead use what is called a truth table to present the same information in an easy-to-read format.
    Code:
    A | NOT A
    --+------
    T |  F
    F |  T
    With the binary operators, there are four rows:
    Code:
    A B | A AND B
    ----+--------
    T T |    T
    T F |    F
    F T |    F
    F F |    F
    Code:
    A B | A OR B
    ----+--------
    T T |    T
    T F |    T
    F T |    T
    F F |    F
    Code:
    A B | A XOR B
    ----+--------
    T T |    F
    T F |    T
    F T |    T
    F F |    F
    Code:
    A B | A NAND B
    ----+--------
    T T |    F
    T F |    T
    F T |    T
    F F |    T
    Code:
    A B | A NOR B
    ----+--------
    T T |    F
    T F |    F
    F T |    F
    F F |    T
    Code:
    A B | A XNOR B
    ----+--------
    T T |    T
    T F |    F
    F T |    F
    F F |    T
    For each table, simply look up the row that has the values of the operands, and the result will be in the third column. The reality is that NAND, NOR, and XNOR are generally not used in programming languages. The reason for this is that they are redundant. NAND, for example, is equivalent to AND followed by NOT. To see this, look at this table:
    Code:
    A B || A NAND B || A AND B | NOT (A AND B)
    ----++----------++---------+--------------
    T T ||    F     ||    T    |  F
    T F ||    T     ||    F    |  T 
    F T ||    T     ||    F    |  T 
    F F ||    T     ||    F    |  T
    The same logic applies to NOR and XNOR. An operation that is important, but usually not used in programming is IF ... THEN ..., or IMPLIES. Its table is.
    Code:
    A B | A IMPLIES B
    ----+------------
    T T |      T
    T F |      F
    F T |      T
    F F |      T
    The idea for this can be best explained in terms of proving that someone has performed false advertising. If I say "If you bring me $100 then I will give you a HD TV", there are four possibilities when you show up at the store:
    1) You give me $100 and I give you an HD TV. Obviously I kept my bargain.
    2) You give me $100 and I tell you I changed my mind and want $1000. That's false advertising.
    3) You offer me $50 and I give you an HD TV anyway. Generous on my part, no problem.
    4) You offer me $50 and I tell you to get lost. You can't sue me... just scrape up $50 more dollars.

    Only in case 2 can you accuse me of false advertising.

    IMPLIES is used for determining whether an argument is valid in certain branches of logic (proofs), but is not really relevant here.

    NOT, AND, and OR are the basis of all of the other operators (even XOR and IMPLIES). As a result, we will just analyze these three (with an occasional look at XOR). The ideas we will look at are the following (stolen blatantly from John): The principal of duality, identity elements, idempotent law, involution law, absorption law, commutative law, associative law, distributive law, DeMorgan's law, and the consensus theorem. (I stole the list from John because half these laws are not talked about regularly in mathy circles, and because you will need these for some tutorials he promises will be coming soon. To be honest, a lot of these are abstract algebra concepts that I hadn't associated with boolean algebra.)

    For most of the items below, the law will be stated in general terms, then followed with concrete states for logic, then a truth table. # and @ will be used as generic operators.

    Principal of Duality: A dual is what you get when you replace all the elements of a statement with its opposite element. So, if you have x' is the dual of x, you would have x#y=z changes to x'@y'=z'. In logic, AND and OR are duals, and TRUE and FALSE are duals. The following pair of truth tables illustrates this principal:
    Code:
    A B | A AND B   A B | A OR B
    ----+--------   ----+-------
    T T |    T      F F |    F
    T F |    F      F T |    T
    F T |    F      T F |    T
    F F |    F      T T |    T
    Identity Elements: For any operator, an identity element is an element that doesn't affect the other element when the operator is used. Using 1 for identity, 1#x=x, x#1=x. For AND, the Identity element is TRUE, for OR the Identity element is FALSE. You can see how this works below:
    Code:
    T B | T AND B
    ----+--------
    T T |    T
    T F |    F
    Code:
    A T | A AND T
    ----+--------
    T T |    T
    F T |    F
    Code:
    F B | F OR B
    ----+--------
    F T |    T
    F F |    F
    Code:
    A F | A OR F
    ----+--------
    T F |    T
    F F |    F
    Idempotent Law: An Idempotent is something that doesn't change when it interacts with itself. x#x=x. AND and OR both satisfy this, while XOR does not, as seen below:
    Code:
    A B | A AND B
    ----+--------
    T T |    T
    F F |    F
    Code:
    A B | A OR B
    ----+--------
    T T |    T
    F F |    F
    Involution Law: Involution deals with the NOT operator. NOT NOT A = A, as seen below:
    Code:
    A | NOT A | NOT (NOT A)
    --+-------+------------
    T |  F    |  T
    F |  T    |  F
    Absorption Law: The absorption law states that x#(x@y) = x, and x@(x#y) = x. A AND (A OR B) = A OR (A AND B) = A.
    Code:
    A B | A AND B | A OR (A AND B)
    ----+---------+---------------
    T T |    T    |    T
    T F |    F    |    T
    F T |    F    |    F
    F F |    F    |    F
    Code:
    A B | A OR B | A AND (A OR B)
    ----+--------+---------------
    T T |    T   |    T
    T F |    T   |    T
    F T |    T   |    F
    F F |    F   |    F
    Commutative Law: x#y = y#x. A AND B = B AND A, A OR B = B OR A. (note: IMPLIES does not satisfy this)
    Code:
    A B | A AND B | B AND A
    ----+---------+--------
    T T |    T    |    T
    T F |    F    |    F
    F T |    F    |    F
    F F |    F    |    F
    Code:
    A B | A OR B | B OR A
    ----+--------+--------
    T T |    T   |    T
    T F |    T   |    T
    F T |    T   |    T
    F F |    F   |    F
    Associative Law: x#(y#z) = (x#y)#z. A AND (B AND C) = (A AND B) AND C, A OR (B OR C) = (A OR B) OR C.
    Code:
    A B C | A AND B | B AND C | A AND (B AND C) | (A AND B) AND C
    ------+---------+---------+-----------------+----------------
    T T T |    T    |    T    |    T            |            T
    T T F |    T    |    F    |    F            |            F
    T F T |    F    |    F    |    F            |            F
    T F F |    F    |    F    |    F            |            F
    F T T |    F    |    T    |    F            |            F
    F T F |    F    |    F    |    F            |            F
    F F T |    F    |    F    |    F            |            F
    F F F |    F    |    F    |    F            |            F
    Code:
    A B C | A OR B | B OR C | A OR (B OR C) | (A OR B) OR C
    ------+--------+--------+---------------+--------------
    T T T |    T   |    T   |    T          |           T
    T T F |    T   |    T   |    T          |           T
    T F T |    T   |    T   |    T          |           T
    T F F |    T   |    F   |    T          |           T
    F T T |    T   |    T   |    T          |           T
    F T F |    T   |    T   |    T          |           T
    F F T |    F   |    T   |    T          |           T
    F F F |    F   |    F   |    F          |           F
    Distributive Law: x#(y@z) = (x#y)@(x#z). A AND (B OR C) = (A AND B) OR (A AND C), A OR (B AND C) = (A OR B) AND (A OR C).
    Code:
    A B C | A AND B | A AND C | B OR C | A AND (B OR C) | (A AND B) OR (A AND C)
    ------+---------+---------+--------+----------------+-----------------------
    T T T |    T    |    T    |    T   |    T           |            T
    T T F |    T    |    F    |    T   |    T           |            T
    T F T |    F    |    T    |    T   |    T           |            T
    T F F |    F    |    F    |    F   |    F           |            F
    F T T |    F    |    F    |    T   |    F           |            F
    F T F |    F    |    F    |    T   |    F           |            F
    F F T |    F    |    F    |    T   |    F           |            F
    F F F |    F    |    F    |    F   |    F           |            F
    Code:
    A B C | A OR B | A OR C | B AND C | A OR (B AND C) | (A OR B) AND (A OR C)
    ------+--------+--------+---------+----------------+-----------------------
    T T T |    T   |    T   |    T    |    T           |           T
    T T F |    T   |    T   |    F    |    T           |           T
    T F T |    T   |    T   |    F    |    T           |           T
    T F F |    T   |    T   |    F    |    T           |           T
    F T T |    T   |    T   |    T    |    T           |           T
    F T F |    T   |    F   |    F    |    F           |           F
    F F T |    F   |    T   |    F    |    F           |           F
    F F F |    F   |    F   |    F    |    F           |           F
    DeMorgan's Law: NOT (A AND B) = NOT A OR NOT B, NOT (A OR B) = NOT A AND NOT B.
    Code:
    A B | A AND B | NOT A | NOT B | NOT (A AND B) | NOT A OR NOT B
    ----+---------+-------+-------+---------------+---------------
    T T |    T    |  F    |  F    |  F            |        F
    T F |    F    |  F    |  T    |  T            |        T
    F T |    F    |  T    |  F    |  T            |        T
    F F |    F    |  T    |  T    |  T            |        T
    Code:
    A B | A OR B | NOT A | NOT B | NOT (A OR B) | NOT A AND NOT B
    ----+--------+-------+-------+--------------+----------------
    T T |    T   |  F    |  F    |  F           |        F
    T F |    T   |  F    |  T    |  F           |        F
    F T |    T   |  T    |  F    |  F           |        F
    F F |    F   |  T    |  T    |  T           |        T
    Consensus Theorem: (A AND B) OR (NOT A AND C) OR (B AND C) = (A AND B) OR (NOT A AND C)
    Code:
    A B C | A AND B | NOT A | NOT A AND C | B AND C | (A AND B) OR (NOT A AND C) OR (B AND C) | (A AND B) OR (NOT A AND C)
    ------+---------+-------+-------------+---------+-----------------------------------------+---------------------------
    T T T |    T    |  F    |        F    |    T    |                 T                       |            T
    T T F |    T    |  F    |        F    |    F    |                 T                       |            T
    T F T |    F    |  F    |        F    |    F    |                 F                       |            F
    T F F |    F    |  F    |        F    |    F    |                 F                       |            F
    F T T |    F    |  T    |        T    |    T    |                 T                       |            T
    F T F |    F    |  T    |        F    |    F    |                 F                       |            F
    F F T |    F    |  T    |        T    |    F    |                 T                       |            T
    F F F |    F    |  T    |        F    |    F    |                 F                       |            F
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Re: Basics of Logic

    great tutorial that will hopefully lead into some good tutorials from john. i will +rep you tonight when i come online
    Posted via CodeCall Mobile

  4. #3
    Jordan Guest

    Re: Basics of Logic

    Wow, that is great tutorial! Very informative and descriptive. +rep

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Basics of Logic

    Exactly what I was looking for, but now I feel obligated to write mine. :[

  6. #5
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Basics of Logic

    Bwahahahaha! BTW, thanks for the kudos, guys.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help with basics
    By isuru in forum Python
    Replies: 5
    Last Post: 05-06-2010, 06:30 PM
  2. The Logic behind solving Logic?
    By Pyro.699 in forum General Programming
    Replies: 2
    Last Post: 04-18-2010, 09:50 PM
  3. Do i have the basics down.
    By Prof_Oak in forum Website Design
    Replies: 2
    Last Post: 04-03-2008, 02:53 PM
  4. C basics.
    By justin1993 in forum C and C++
    Replies: 4
    Last Post: 07-24-2007, 05:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts