Introduction to Boolean Logic
Friday, 28 September 2018
Article Index
Introduction to Boolean Logic
Binary arithmetic and flip-flops
Flip Flops - Time Enters the Logic
More Logic

## Flip-flops

Computers also include “sequential” logic, which includes an element of time.

For example, a flip-flop, again perfectly good jargon, is a circuit that changes state like a pendulum flips from side to side.

The odd thing is that you can make a flip-flop from two Not gates and it is true that most sequential logic can be understood in terms of combinations of logic gates. However, this is an incomplete explanation and requires something beyond Boolean logic.

For example, Boolean logic cannot cope with the statement:

`A = NOT A`

If A is true then NOT A is false and so A must be false but this means that NOT A is true, which means that A must be true, which means, and so on…

So from the point of view of Boolean logic this is nonsense.

Now imagine that the on one side of a blackboard you write “A is true” and on the other side you write “Not A is true”. Now when you read “A is true” you are happy with this. When you turn the blackboard over you see “Not A is true” and so conclude A is false. As you keep flipping the board over the value of A flips from true to false and back again.

This turning over of the blackboard is the extra part of the theory needed to deal with sequential logic - it shows how time changes things. So computer hardware is not just one large statement in Boolean logic, which just happens to be true or false.

Its state changes with every tick of its system clock.

## Boolean logic in programming

Boolean logic is fundamental to the design of computer hardware even if it isn’t the whole story. The same holds true for programming.

A program also needs the element of time built into it to make it work and this takes it beyond the bounds of simple Boolean logic. However there are times when simple common sense reasoning lets even the best programmer down.

For example, a common task is making a decision using an IF statement:

`IF (A>0 AND A<10) THEN do something`

The bracket following the IF is almost pure Boolean logic and the something only gets done if it works out to be true.

So far so simple, so simple in fact that many programmers decide that they don’t need to know anything about Boolean logic at all; a serious error.

Consider how you would change this example so that the “something” was done when the condition was false. The simplest way is to write:

`IF NOT(A>0 AND A<10) THEN do something`

However this offends some programmers who want to rewrite the condition not using the NOT.

Try it and see what you come up with.

A common mistake is to use:

`IF (A<=0 AND A>=10) THEN do something`

This isn’t correct and if you don’t believe it simply write out the truth table for both. The correct NOT of the condition is:

`IF (A<=0 OR A>10) THEN do something`

The switch from AND to OR shocks many experienced programmers and it is a source of many programming errors.

My advice is that if you can write a logical condition in a natural form but you really need the NOT of it then just write NOT in front of it!

Otherwise learn De Morgan’s laws:

`NOT(A AND B)=NOT(A) OR NOT(B)`

and

`NOT(A OR B) = NOT(A) AND NOT(B)`

Boolean logic is important as a fundamental way of thinking about very simple situations which involve combinations of two states.

What it is not is a theory of thought or of the way computers work. It is not even essential to designing or using a computer but it does make both tasks easier.

If you’re serious about computing you need to be at home with Boolean logic in much the same way that you are at home with arithmetic - binary arithmetic of course.

## The Universal Gate

One final and slightly deep thought.

How many operators do you need to implement Boolean logic?

The obvious answer is three - AND, OR and NOT - but this isn't correct.

The correct answer is that you only need one operation. Clearly it can't be just any operation. For example, it is impossible to make an OR from combinations of AND - try it.

However if you pick an operation that includes an element of the NOT operator then you can make anything you like using it.

For example the AND operator isn't universal but the NAND, i.e. Not AND is. The truth table for NAND is just NOT(P AND Q):

 P Q P NAND Q F F T F T T T F T T T F

To make a NOT all you have to do is write:

` P NAND P= NOT(P)`
 P P P NAND P F F T T T F

Once you have a NOT you can make an AND:

` NOT(P NAND Q)= P AND Q`

The only operation that remains is to make an OR out of NAND and NOT and the solution to this is one of  De Morgan’s laws:

`NOT(P AND Q)=NOT(P) OR NOT(Q)`

A little work soon gives:

`NOT(P) NAND NOT(Q)=P OR Q`

So with a NAND operator you can build a NOT, an AND and an OR. This means that a single operator does everything that the usual three can do.

In practical terms this means that every circuit in a computer could be built using just a NAND gate. In practice electronic engineers like to use a variety of gates because it is simpler.

NAND isn't the only universal operator.

You might guess that NOR, i.e. NOT(P OR Q) is also universal but what about XOR? There isn't a hint of a NOT gate here - is there?

However, what about P XOR True?

 P True P XOR P F T T T T F

Yes, P XOR True = NOT P and from this you can build an AND and an OR operator. XOR Is also a universal operator for Boolean logic.

It is something to think about that the whole of logic can be built from just one operation.

Last Updated ( Friday, 28 September 2018 )