0% found this document useful (0 votes)
107 views14 pages

COMP 102:: Computers and Computing Lecture 3: Truth Tables and Logic Gates

- The document discusses building an automatic voting machine using logical variables and expressions. It defines variables to represent each voter's vote and the winner. - It then shows how to write a logical expression to determine the winner based on the votes and check it using a truth table. The truth table lists all possible input combinations and corresponding outputs. - An alternative logical expression can be directly extracted from the truth table. The document also discusses implementing logical expressions using basic logic gates like AND, OR, and NOT, and how more complex gates can be built by combining simpler ones.

Uploaded by

kallady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views14 pages

COMP 102:: Computers and Computing Lecture 3: Truth Tables and Logic Gates

- The document discusses building an automatic voting machine using logical variables and expressions. It defines variables to represent each voter's vote and the winner. - It then shows how to write a logical expression to determine the winner based on the votes and check it using a truth table. The truth table lists all possible input combinations and corresponding outputs. - An alternative logical expression can be directly extracted from the truth table. The document also discusses implementing logical expressions using basic logic gates like AND, OR, and NOT, and how more complex gates can be built by combining simpler ones.

Uploaded by

kallady
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

COMP 102: Computers and Computing

Lecture 3: Truth Tables and Logic Gates


Instructor: Joelle Pineau ([email protected])

Class web page: www.cs.mcgill.ca/~jpineau/comp102

Practice example

•  You are given the responsibility of building an automatic voting


machine.
–  Assume there are 2 candidates.

–  Assume there are 3 voters, everyone gets a single vote.

–  The candidate with the most votes wins.

•  What logical variables would you use?

•  Can you write a logical expression, which evaluates who wins (True =
Candidate A, False = Candidate B)?

COMP-102: Computers and Computing 2 Joelle Pineau

1
Practice example

•  Input logical variables:


–  V1 = Vote of person 1 (True=Candidate A, False=Candidate B)
–  V2 = Vote of person 2 (True=Candidate A, False=Candidate B)
–  V3 = Vote of person 3 (True=Candidate A, False=Candidate B)

•  Output logical variables


WINNER = {True=Candidate A wins, False=Candidate B wins}

•  Logical expression:
WINNER = (V1 AND V2) OR (V1 AND V3) OR (V2 AND V3)

How would you check if the logical expression is correct?

COMP-102: Computers and Computing 3 Joelle Pineau

Checking logical expressions

•  Computer must be ready for any input, and must compute


correct results in all cases.

•  Must go through all possible input combinations:


•  V1=True, V2=True, V3=True WINNER = ?
•  V1=True, V2=True, V3=False WINNER = ?
•  V1=True, V2=False, V3=True WINNER = ?
•  V1=True, V2=False, V3=False WINNER = ?
•  V1=False, V2=True, V3=True WINNER = ?
•  V1=False, V2=True, V3=False WINNER = ?
•  V1=False, V2=False, V3=True WINNER = ?
•  V1=False, V2=False, V3=False WINNER = ?

COMP-102: Computers and Computing 4 Joelle Pineau

2
Truth table
•  Write-up a table with all possible input combinations, and check the
output the output for each row.
Inputs: Outputs:
V1 V2 V3 WINNER
0 0 0 0 (= Candidate B)
0 0 1 0 (= Candidate B)
0 1 0 0 (= Candidate B)
0 1 1 1 (= Candidate A)
1 0 0 0 (= Candidate B)
1 0 1 1 (= Candidate A)
1 1 0 1 (= Candidate A)
1 1 1 1 (= Candidate A)

•  This is called a Truth Table.

COMP-102: Computers and Computing 5 Joelle Pineau

Comparing logical expressions

•  Recall our previous expression:

WINNER= (V1 AND V2) OR (V1 AND V3) OR (V2 AND V3)

•  You can also extract the logical expression directly from the Truth
Table:

WINNER = ( (NOT V1) AND V2 AND V3) OR

(V1 AND (NOT V2) AND V3) OR

(V1 AND V2 AND (NOT V3)) OR

(V1 AND V2 AND V3)

COMP-102: Computers and Computing 6 Joelle Pineau

3
Extracting logical expression from the truth table

•  Recall:
WINNER = ( (NOT V1) AND V2 AND V3) OR
(V1 AND (NOT V2) AND V3) OR
(V1 AND V2 AND (NOT V3)) OR
(V1 AND V2 AND V3)

•  How do we get this logical expression:


–  Consider each line in the table.
•  If the line has OUTPUT=1, this line must be included in the logical expression as
a sub-expression.
•  The sub-expression includes all variables, where true variables are included
without modification and negative variables are preceded by NOT operator.
•  The variables in a sub-expression are separated by “AND” logical operators.
–  Sub-expressions are separated by “OR” logical operators.

COMP-102: Computers and Computing 7 Joelle Pineau

Pros / cons of the two logical expressions

•  Compare:

E1: WINNER = (V1 AND V2) OR (V1 AND V3) OR (V2 AND V3)

E2: WINNER = ( (NOT V1) AND V2 AND V3) OR


(V1 AND (NOT V2) AND V3) OR
(V1 AND V2 AND (NOT V3)) OR
(V1 AND V2 AND V3)

•  E1 is more compact.

•  E2 we can get directly from the truth table.

COMP-102: Computers and Computing 8 Joelle Pineau

4
How do we implement a logical expression?

•  Assume we have logic gates (or blocks) that implement each


logical operator.

Input A
Logic Output Logic Output
Input A
gate Input B gate

Single input Double input

•  Logic gates are the building blocks of digital electronics and are
used to build telecommunication devices, computers, etc.

COMP-102: Computers and Computing 9 Joelle Pineau

Logic gates and their truth table: AND

•  Truth table for the AND operator:


Input A: Input B: Output:
0 0 0
0 1 0
1 0 0
1 1 1

•  The AND gate is usually drawn as a half-moon.


•  It is a double input gate.
Input A
AND Output
Input B

COMP-102: Computers and Computing 10 Joelle Pineau

5
Logic gates and their truth table: OR

•  Truth table for the OR operator:


Input A: Input B: Output:
0 0 0
0 1 1
1 0 1
1 1 1

•  The OR gate is usually drawn as a crescent-shape.


•  It is a double input gate.
Input A
OR Output
Input B

COMP-102: Computers and Computing 11 Joelle Pineau

Logic gates and their truth table: NOT

•  Truth table for the NOT operator:


Input: Output:
0 1
1 0

•  The NOT gate is usually drawn as a triangle, with a small circle.


•  The circle is there to indicate the output inverts the input.
•  It is a single input gate.

Input NOT Output

COMP-102: Computers and Computing 12 Joelle Pineau

6
Can we implement E1 and E2?

•  Problem:
–  E1 needs a 3-input OR gate.
–  E2 needs a 3-input AND gate.

•  Solution: Make it by grouping gates

A A
OR
B OR Output = B OR Output
C
C

•  And similarly for AND gates.

COMP-102: Computers and Computing 13 Joelle Pineau

More complicated gates: NAND

•  NAND = Not AND

•  This corresponds to an AND gate followed by a NOT gate.


–  Output = NOT ( A AND B )

A
Output
B

COMP-102: Computers and Computing 14 Joelle Pineau

7
More complicated gates: NOR

•  NOR = Not OR

•  This corresponds to an OR gate followed by a NOT gate.


–  Output = NOT ( A OR B )

A
Output
B

COMP-102: Computers and Computing 15 Joelle Pineau

More complicated gates: EX-OR

•  EX-OR = EXclusive OR

•  This corresponds to an OR gate, but the output is negative if


both inputs are true.

A
Output
B

COMP-102: Computers and Computing 16 Joelle Pineau

8
Combining logic gates

•  Logic gates can be combined to produce complex logical expressions.

E.g.: WINNER = (V1 AND V2) OR (V1 AND V3) OR (V2 AND V3)

V1 AND
OR
OR Output
V2 AND

AND
V3

•  Logic gates can also be combined to substitute for another type of gate.

COMP-102: Computers and Computing 17 Joelle Pineau

Example

A OR NOT
Output
B

A NOT

AND Output
B NOT

Is there a unique set of blocks to represent a given expression? No!

(Hint: Just write out the truth table for each set of gates, and see whether they
are the same.)

COMP-102: Computers and Computing 18 Joelle Pineau

9
De Morgan’s Theorem
Logical gates Logical expression
A OR NOT
Output NOT (A OR B)
B

A NOT

AND Output (NOT A) AND (NOT B)


B NOT

Theorem: If neither A nor B is true, then both A and B must be false.

COMP-102: Computers and Computing 19 Joelle Pineau

How do we choose which expression to implement?

•  Sometimes function can be more compact (recall E1 vs E2).

•  Multiple logic gates (of one type) are placed on a single chip;

it may be more efficient to use all of them, rather than require


another chip (of a different type).

4001 Chip: four 2-input NOR gates

COMP-102: Computers and Computing 20 Joelle Pineau

10
Leveraging this insight
•  Any logical gate can be replaced by a set of NAND gates.

•  This means all we ever


need, to implement any
logical expression,
is a lot of NAND gates!

•  Total number of gates


may be larger, but total
number of chips is usually
smaller.

(Note: Same thing can be done with NOR gates.)

COMP-102: Computers and Computing 21 Joelle Pineau

A harder problem

•  Imagine you play a game of Rock-Paper-Scissors against your friend.

•  Assume you want a computer to automatically decide if you win or not.

•  What logical variables would you use?

•  Can you write a logical expression, which evaluates whether or not you win
(True = win, False = loose)?
E.g. If you play Rock and your friend players Scissor, it returns True, and similarly for other
possible plays.

COMP-102: Computers and Computing 22 Joelle Pineau

11
Rock-Paper-Scissors: Logical variables

•  Input: choice of player 1, choice of player 2

•  Output: outcome of the game (according to the rules)

•  Need to convert input and output to binary representation.

•  Need 2 variables to represent the possible choice of each player


01 = Scissors 10 = Paper 11 = Rock
So we need 4 variables to represent the choice of both players.

•  Need 2 variables to represent the possible outcomes.


10 = Player 1 wins 01 = Player 2 wins 00 = Tie

COMP-102: Computers and Computing 23 Joelle Pineau

Rock-Paper-Scissors: Other representations

•  There are other possible binary representations.

•  Some are equivalent:


–  same expressive power, same number of bits
–  E.g. Scissors = 00, Paper = 01, Rock = 11

•  Some are not equivalent:


–  E.g. Scissors = 0, Paper = 1, Rock = 1 (Fewer bits, less expressive power)
–  E.g. Scissors = 000, Paper = 001, Rock = 011 (Same power, but more bits)

COMP-102: Computers and Computing 24 Joelle Pineau

12
Rock-Paper-Scissors: Truth Table
Input logical variables: Output variables:
Player1: A B Player2: C D E F
Scissors 0 1 Scissors 0 1 Tie 0 0
Scissors 0 1 Paper 1 0 Player 1 wins 1 0
Scissors 0 1 Rock 1 1 Player 2 wins 0 1
Paper 1 0 Scissors 0 1 Player 2 wins 0 1
Paper 1 0 Paper 1 0 Tie 0 0
Paper 1 0 Rock 1 1 Player 1 wins 1 0
Rock 1 1 Scissors 0 1 Player 1 wins 1 0
Rock 1 1 Paper 1 0 Player 2 wins 0 1
Rock 1 1 Rock 1 1 Tie 0 0

What happens to the unspecified input (e.g. 0000)? Doesn’t matter what
the output is!

COMP-102: Computers and Computing 25 Joelle Pineau

Rock-Paper-Scissors: Logical expressions

•  Need two expressions, one for each of the output bits.

E = ( (NOT A) AND B AND C AND (NOT D) ) OR

( (A AND (NOT B) AND C AND D) ) OR

( (A AND B AND (NOT C) AND D) )

F = ( (NOT A) AND B AND C AND D) OR

( A AND (NOT B) AND (NOT C) AND D) OR

( A AND B AND C AND (NOT D) )

COMP-102: Computers and Computing 26 Joelle Pineau

13
Rock-Paper-Scissors: Logical gates

•  Final step! Try this at home.

COMP-102: Computers and Computing 27 Joelle Pineau

Take-home message
•  Know how to build a truth table from a logical problem description.

•  Know how to extract the logical expressions from the truth table.

•  Learn to identify and use the basic gates: AND, OR, NOT.

•  Understand the link between truth tables and logic gates.

•  Know how to use combinations of gates to implement logical


expressions.

•  Understand that many different sets of gates can represent a given


logical expression.

•  Be able to state and understand De Morgan’s theorem.

Homework 2 posted. Due next Thursday.

COMP-102: Computers and Computing 28 Joelle Pineau

14

You might also like