Hello World! An Invitation to Computer Science

Lecture Slides: Boolean Logic

Monday, February 25, 2008


Key ideas from last time


Boolean logic


Examples

numeric ranges and sets:

  x > 33  AND  x < 67
  p = 2  OR  p is odd
  NOT  y = 0  

on-line searches:
  explicit: library
  implicit: amazon; imdb; google; kayak ...

car warranties: 3 years OR 30,000 miles

Boolean logic is everywhere!


Truth tables

              a   NOT a
              ---------
              F     T  
              T     F


  a  b    a AND b     a  b    a OR b
  ---------------     ---------------
  F  F       F        F  F       F   
  F  T       F        F  T       T   
  T  F       F        T  F       T   
  T  T       T        T  T       T

Boolean expression → truth table

Consider: (NOT p) AND (NOT q)

  p  q    NOT p    NOT q    
  ---------------------------------------------
  F  F       T        T              T   
  F  T       T        F              F   
  T  F       F        T              F   
  T  T       F        F              F

Expressions do not have to mean anything.


Contradiction!

Some expressions are never true.

  a  b    (NOT (a OR b)) AND a
  ----------------------------
  F  F             F   
  F  T             F   
  T  F             F   
  T  T             F

Tautology!

Some expressions are always true.

  p  q    (NOT (p AND q)) OR q
  ------------------------------------------
  F  F               T   
  F  T               T   
  T  F               T   
  T  T               T

Proof by truth table

One of DeMorgan's Laws:

  a  b    (NOT a) AND (NOT b)    NOT (a OR b)
  ---------------------------------------------
  F  F             T                 T
  F  T             F                 F
  T  F             F                 F
  T  T             F                 F
The NOT of the OR is the AND of the NOTs.


Logical implication

"If a then b."

  a  b    a → b   (NOT a) OR b
  -----------------------------
  F  F       T          T
  F  T       T          T
  T  F       F          F
  T  T       T          T


Boolean → bits

  inputs   outputs
   a  b     c  s      c terms    s terms
  --------------------------------------------
   0  0     0  0    
   0  1     0  1                 (NOT a) AND b
   1  0     0  1                 a AND (NOT b)
   1  1     1  0       a AND b

What does this capture?


1-bit addition is logic!

  sum   = a XOR b

  carry = a AND b

Realizing logic as circuits


Example: exclusive or

  a  b    a XOR b
  ---------------
  F  F       F
  F  T       T
  T  F       T
  T  T       F