Digital Logic

Intro to Programming Logic COMP-1029

Red River College

Introduction

The purpose of this module is to explore digital logic gates.

Logic gates are the building blocks of computer chips.

We will also learn about Boolean logic, the math behind these logic gates.

Boolean logic is how we program computers to make decisions.

Prerequisite Modules:

Objective

Upon completion of this module, you should be able to:

Questions

Electrical Signals

Signal

In the world of electronics there are two major types of signals:

  • Analog
  • Binary (or Digital)

An electronic analog signal varies over time and can have a different value at any given time.

An electronic binary signal can also vary over time, but it can only be used to represent two separate levels.

The digital circuits that make up all modern computers use binary signals for communication and for information storage.

Why Binary?

Binary

Some reasons for why modern computers use binary signals (as opposed to analog signals):

  • Binary is very simple compared to analog.
  • This simplicity means binary circuits are easy to build.
  • Flawless copies of binary signals can be made.
  • Anything that can be represented with some sort of finite pattern can be represented with a pattern of binary bits.

In electronic circuits binary zero is represented by a low voltage (near zero volts) and binary 1 is represented by a high voltage (often five volts).

Resources

Wikipedia Links:

Boolean Logic

Claudeshannon

Boolean logic is a type of algebra where the variables can only take two possible values. These values are 1 and 0, although we sometimes refer to these as true and false respectively. In other words, Boolean logic is an algebra for binary systems.

As we saw in the history module, Boolean logic was developed by George Boole in the 1840s and was later implemented electronically by Claude Shannon (right) in the 1930s, leading to the advent of electronic computers.

At the heart of nearly all electronic circuits lies this system of Boolean calculation.

Digital Logic

Gates

Instead of studying Boolean algebra on it’s own, we will study the digital components used in electronic circuits that implement Boolean algebra operations.

These components are called digital logic gates. We will be using the symbols shown to the right to represent these gates.

The lines leading to the left of each gate represent input wires, while the line leading from the right of each gate represents the output.

You can think of each gate as performing an algebraic operation on its inputs to create its output. The inputs and outputs will always be the binary values of either true or false.

Resources

Wikipedia Links:

Simply Boolean System

Doorbell

Imagine a house with a doorbell. When the doorbell button is pressed a chime will be heard within the house.

We can model this as a Boolean system:

  • Doorbell button pressed = Binary 1
  • Doorbell button unpressed = Binary 0

(Doorbell Chime) = (Doorbell button pressed)

Another Boolean System

2303463266_25366a5e83

Many cars will signal a buzzer if the engine is on and the driver’s seat-belt is undone.

Modeled as a Boolean System:

  • Engine: On/Off
  • Seat-belt: Undone/Fastened

Buzzer = (Engine is On?) AND (Seat-belt is Undone?)

Stated another way:

Buzzer = (Engine is On?) AND (Seat-belt is NOT Fastened?)

Truth Tables

A truth table is a mathematical table used in Boolean Logic to represent all possible combinations of input and output.

Let’s describe our seat-belt example as a truth table:

A = Engine is On? (True or False)
B = Seat-belt is Undone? (True or False)
X = Turn on buzzer. (True or False)

| A | B | X |
-------------
| F | F | F |
| F | T | F |
| T | F | F |
| T | T | T |

In other words, the buzzer will only sound when both A and B are True.

Another way to build this truth table would be using zeros and ones:

| A | B | X |
-------------
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |

Resources

Wikipedia Links

NOT Gate

Not_gate

The simplest logic gate is the NOT gate, sometimes called an inverter.

  • It has one input and one output.
  • The output is always the opposite of the input.

The truth table for a NOT gate:

| A | X |
---------
| F | T |
| T | F |

In other words, X is true when A is NOT true.

Resources

Wikipedia Links:

AND Gate

And_gate

An AND gate has two inputs and one output.

The output is always false, unless both inputs are true.

The truth table for an AND gate:

| A | B | X |
-------------
| F | F | F |
| F | T | F |
| T | F | F |
| T | T | T |

In other words, X will be true when both A AND B are true.

Does this truth table look familiar? (Hint: Compare it to our selt-belt example.)

Resources

Wikipedia Links:

OR Gate

Or_gate

An OR gate has two inputs and one output.

The output is always true, unless both inputs are false.

The truth table for an OR gate:

| A | B | X |
-------------
| F | F | F |
| F | T | T |
| T | F | T |
| T | T | T |

In other words, X will be true when both A OR B are true.

Resources

Wikipedia Links:

XOR Gate

Xor_gate

An XOR gate has two inputs and one output.

The output is true, unless both inputs are the same.

The truth table for an XOR gate:

| A | B | X |
-------------
| F | F | F |
| F | T | T |
| T | F | T |
| T | T | F |

In other words, X is true when A is NOT equal to B.

Resources

Wikipedia Links:

Negated Gates

Pre_nand

It is common in digital circuit design for a NOT gate to follow directly after one of the other gate types.

To simplify our circuit diagrams there are a series of negated gates to use in this situation.

NAND Gate

Nand_gate

A NAND gate has two inputs and one output. It is equivalent to an AND gate followed by a NOT gate.

The output is always true, unless both inputs are true.

The truth table for an NAND gate:

| A | B | X |
-------------
| F | F | T |
| F | T | T |
| T | F | T |
| T | T | F |

Resources

Wikipedia Links:

NOR

Nor_gate

A NOR gate has two inputs and one output. It is equivalent to an OR gate followed by a NOT gate.

The output is always false, unless both inputs are false.

The truth table for an OR gate:

| A | B | X |
-------------
| F | F | T |
| F | T | F |
| T | F | F |
| T | T | F |

Resources

Wikipedia Links:

XNOR

Xnor_gate

An XNOR gate has two inputs and one output. It is equivalent to an XOR gate followed by a NOT gate.

The output is true, when both inputs are the same. For this reason it is sometimes called an equality gate.

The truth table for an XNOR gate:

| A | B | X |
-------------
| F | F | T |
| F | T | F |
| T | F | F |
| T | T | T |

Resources

Wikipedia Links:

Systems of Gates

Gate_system

Gates can be chained together to create more complex structures.

The truth table for the system to the right:

| A | B | C | X |
-----------------
| F | F | F | F |
| F | F | T | F |
| F | T | F | T |
| F | T | T | F |
| T | F | F | T |
| T | F | T | F |
| T | T | F | T |
| T | T | T | F |

In other words, X will be true whenever A OR B is true unless C is true.

A friendlier way to look at this truth table would be to change the positions of columns and replace the T/F for 1/0:

| C | A | B | X |
-----------------
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
| 1 | 1 | 1 | 0 |

Here it is more obvious that X is false anytime C is true, and that when C is false X will be true if either A or B is true.

How would the truth table change if the OR gate was replaced by an XOR?

Building a Adding and Counting Circuits

Let’s see how these digital logic gates can be used to implement two useful combinational circuits:

  1. A circuit to sum two four bit binary numbers.
  2. A circuit which can count from 0 to 15.

Resources

Two Digital Logic Simulators:

Boolean Word Problems

Ticket Time Problem

You are driving to school and you pass a speed trap. If your speed is 60 or less, you arrive at school without incident. If your speed is 61 or greater, you’ll get a speeding ticket.

A Boolean logic equation that describes the above situation might be:

Ticketed = (Driving speed is greater than 60.)

Let’s add a twist to the ticketing rules:

On your birthday your speed can be 5 higher than the regular speed limit.

What might the Boolean logic equation for ticketing look like now?

What's Next?

Boolean logic is not only used at the circuit level, it is also used for all decisions made in computer programs.

Armed with our knowledge of Boolean logic we can now begin to exploring problem solving with algorithms.