Learnings

DeMorgan's Law: !(a || b) = (!a && !b) and !(a && b) = (!a || !b)

Compound Boolean Expression

Compound boolean expressions include AND (&&), OR (||), and NOT (!).

Truth tables

Truth tables display all the possible results for boolean expressions.

Below is the truth table for && and ||:

P Q P&&Q
true true true
true false false
false true false
false false false


P Q P\ \ Q
true true true
true false true
false true true
false false false

De Morgan's Law

First, some vocab:

  • Set: A group of objects
  • Universal set: A set that has all of the elements
  • Subsets: Contains elements from the universal set
  • Complement: For example, if we have set A, the complement of set A would have the elements in the universal set, but not in set A.
  • Union: New set has elements from both sets

    Symbol: ∪

  • Intersection: New sets has elements that are shared between both sets

    Symbol: ∩

De Morgan's Law is used in boolean algebra to help simplify expressions

De Morgan's Law of Union states that: "The complement of the union of the two sets A and B will be equal to the intersection of A' (complement of A) and B' (complement of B)."

Formula: (A ∪ B)' = A'∩ B'

De Morgan's Law of Intersection states that: "The complement of the intersection of A and B will be equal to the union of A' and B'."

Formula: (A ∩ B)’ = A’ ∪ B’


The following is an example:

Universal set: {1, 3, 5, 6, 8, 9} Set A: {1, 3} Set B: {5, 6, 8}

De Morgan's Law of Union: (A ∪ B)' = {9}

A'∩ B' = {9}

(A ∪ B)' = A'∩ B'

De Morgan's Law of Intersection: (A ∩ B)' = {1, 3, 5, 6, 8, 9}

A’ ∪ B’ = {1, 3, 5, 6, 8, 9}

(A ∩ B)’ = A’ ∪ B’


Translating De Morgan's law into code, De Morgan's Law of Union would mean: !(a || b) = (!a && !b).

De Morgan's Law of Intersection means: !(a && b) = (!a || !b)

public class DeMorgansLaw{
    public static void main(String[] args) {
        boolean rainy = false;
        boolean sunny = false;

        if (!!!(rainy || sunny) && ((!rainy && !sunny) || !(!(rainy || sunny))) ) {
            System.out.println("It's a cloudy day, not too hot, not too cold");
        }
    }
}
DeMorgansLaw.main(null)
It's a cloudy day, not too hot, not too cold

Well this is awfully confusing, how about we clean it up a little?

First, remove all of the !! (negative + negative = positive)

This code reads: If it's not rainy or sunny, and: it's not rainy and it's not sunny OR it's rainy or sunny

Evaluating each part of the code:

  1. If it's not rainy or sunny: True
  2. If it's not rainy and it's not sunny: True
  3. It's rainy or sunny: False

2. and 3. are OR, therefore, it is True.

1. is ANDed to 2. and 3. which is True. -> True AND True is true, therefore, the message is outputted.

public class DeMorgansLaw{
    public static void main(String[] args) {
        boolean rainy = false;
        boolean sunny = false;

        if (!(rainy || sunny) && ((!rainy && !sunny) || (rainy || sunny)) ) {
            System.out.println("It's a cloudy day, not too hot, not too cold");
        }
    }
}
DeMorgansLaw.main(null)
It's a cloudy day, not too hot, not too cold