De Morgan’s Law, Boolean Algebra, Truth Table

In this section we will learn about basic algorithm. However algorithm has its roots in De Morgan’s laws on Boolean algebra.

In addition, the main concept of logical expression evolves.

After learning about basic algorithmic steps and sequences, we will understand how data structures depend on logical expression.

Firstly, to build complex algorithm we need to understand De Morgan’s law which is the foundation of truth table.

Secondly, we must know Boolean algebra which we have discussed before.

Finally, let’s know who is Augustus De Morgan. He was a contemporary mathematician of George Boole. Although he did not create the laws using his name, yet it was after his him, since he was the creator.

De Morgan’s laws use Boolean algebra, and in every programming language, we apply it.

What the rule states, we can write this way, where ‘a’ and ‘b’ are two boolean values (true or false):

1. not (a and b) is the same as (not a) or (not b)
2. not (a or b) is the same as (not a) and (not b)

Let’s use a Python code where we can apply the first law.

# not (a is true and b is true) = false
## true and true is true 

# not (a is not true or b is not true) = false
## false or false is false

def notAandB(paramOne, paramTwo):
    additionOfTwoNumbers = paramOne + paramTwo
    if not (paramOne <= 10 and paramTwo >= 15):
        print(f'Addition of two numbers : {additionOfTwoNumbers}')
    else:
        print(f'The number is neither less than equal to 10 nor greater than equal to 15')
     

def notAORnotB(paramOne, paramTwo):
    additionOfTwoNumbers = paramOne + paramTwo
    if((paramOne <= 10 or paramTwo >= 15)):
        print(f'Addition of two numbers : {additionOfTwoNumbers}')
    else:
        print(f'The number is neither less than equal to 10 nor greater than equal to 15')
           
    
notAandB(1, 140)
notAORnotB(11, 14)

'''
The number is neither less than equal to 10 nor greater than equal to 15
The number is neither less than equal to 10 nor greater than equal to 15
'''

Let’s discuss the above code first.

We have passed two values to the fist function.

notAandB(1, 140)

In our logical expression when we check the value, what do we find?

1 is certainly less than or equal 10. Besides, 140 is also greater than or equal to 15.

As a result, “True and True” is True.

However, in the logical expression we have declared it is not true. Right?

if not (paramOne <= 10 and paramTwo >= 15):
...

As a result, the logical expression becomes false.

Therefore if block doesn’t work and we have got the output as follows.

The number is neither less than equal to 10 nor greater than equal to 15

It happened because any “if” block will only work when the logical expression is true.

But in our case, it is false.

Now we’re going to prove the De Morgan’s law in the second function.

DE Morgan’s Law and Truth Table

At the same time De Morgan’s law proves the truth table.

True and True is True.

Lt’s check the next function where we have passed two values just like before.

notAORnotB(11, 14)

However, according to the logical expression of the function what do we find?

11 is certainly greater than or equal to 10. In addition, 14 is also less than or equal to 15.

Therefore both statements are false.

if((paramOne <= 10 or paramTwo >= 15)):

As a result, the if block is not working.

At the same time it proves the truth table.

False or false is false

Let’s remember De Morgan’s first law again.

1. not (a and b) is the same as (not a) or (not b)

Our code and output clearly prove the first law of De Morgan.

Let us write the following code according to De Morgan’s second law.

# not (a is true or b is true) = false
## true or true is true

# (a is not true and b is not true) = false
## false and false is false

def notAandB(paramOne, paramTwo):
    additionOfTwoNumbers = paramOne + paramTwo
    if not (paramOne <= 10 or paramTwo >= 15):
        print(f'Addition of two numbers : {additionOfTwoNumbers}')
    else:
        print(f'The number is neither less than equal to 10 nor greater than equal to 15')
     

def notAORnotB(paramOne, paramTwo):
    additionOfTwoNumbers = paramOne + paramTwo
    if((paramOne <= 10 and paramTwo >= 15)):
        print(f'Addition of two numbers : {additionOfTwoNumbers}')
    else:
        print(f'The number is neither less than equal to 10 nor greater than equal to 15')
           
    
notAandB(1, 140)
notAORnotB(11, 14)

'''
The number is neither less than equal to 10 nor greater than equal to 15
The number is neither less than equal to 10 nor greater than equal to 15
'''

De Morgan’s second law states the following.

2. not (a or b) is the same as (not a) and (not b)

Following the law we have written the if logical expression as follows.

if not (paramOne <= 10 or paramTwo >= 15):

After that we have passed the following values as arguments.

notAandB(1, 140)

As we see, 1 is certainly less than or equal to 10. In addition, 140 is also greater than or equal to 15.

Therefore both statements are true. According to the Truth table we know that true or true is also true.

However, in our case we have followed De Morgan’s law literally. As a result, we have negated the true statement and made it false.

So the if block will not work.

Now consider the second function where the logical expression is as follows.

if((paramOne <= 10 and paramTwo >= 15)):

However, we have passed two false values.

notAORnotB(11, 14)

11 is not less than or equal to 10. Besides, 14 is not greater than or equal to 15.

As a result, our both statements are false. Therefore, if block will not work. At the same time, first function and second function prove De Morgan’s second law and the truth table.

According to the Truth table, we know that false and false are always false..

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Flutter, Dart and Algorithm

Twitter

Comments

One response to “De Morgan’s Law, Boolean Algebra, Truth Table”

  1. […] In our previous discussion we have seen what is De Morgan’s law. […]

Leave a Reply