cs1q computer systems lecture 6

45
CS1Q Computer Systems Lecture 6 Simon Gay

Upload: alana-cervantes

Post on 04-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

CS1Q Computer Systems Lecture 6. Simon Gay. Algebraic Notation. Writing AND, OR, NOT etc. is long-winded and tedious. We generally use a more compact notation:. means x AND y. means x OR y. means NOT x. means x XOR y. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS1Q Computer Systems Lecture 6

CS1Q Computer SystemsLecture 6

Simon Gay

Page 2: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 2

Algebraic NotationWriting AND, OR, NOT etc. is long-winded and tedious. We generally use a more compact notation:

xy means x AND y

yx means x OR y

x means NOT x

yx means x XOR y

The operations can be combined to form algebraic expressionsrepresenting logic functions.

Page 3: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 3

Examples of Algebraic NotationThe majority voting function from the last lecture can be written

zxyzxy

The expression)( zyx

means x AND (y OR z)

The expression )( zyx

means x AND NOT (y OR z)

and also x AND (y NOR z)

Page 4: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 4

ExerciseWhat is the meaning of this expression? Draw a circuit for thisfunction, and calculate the truth table. Which logical operation is it?

yxyx

Page 5: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 5

ExerciseWhat is the meaning of this expression? Draw a circuit for thisfunction, and calculate the truth table. Which logical operation is it?

yxyx

(x AND (NOT y)) OR ((NOT x) AND y)

x y yx yx yxyx 0

110

0

101

Page 6: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 6

ExerciseWhat is the meaning of this expression? Draw a circuit for thisfunction, and calculate the truth table. Which logical operation is it?

yxyx

(x AND (NOT y)) OR ((NOT x) AND y)

x y yx yx yxyx 0

110

0

101

0

10

0

Page 7: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 7

ExerciseWhat is the meaning of this expression? Draw a circuit for thisfunction, and calculate the truth table. Which logical operation is it?

yxyx

(x AND (NOT y)) OR ((NOT x) AND y)

x y yx yx yxyx 0

110

0

101

0

10

0100

0

Page 8: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 8

ExerciseWhat is the meaning of this expression? Draw a circuit for thisfunction, and calculate the truth table. Which logical operation is it?

yxyx

(x AND (NOT y)) OR ((NOT x) AND y)

x y yx yx yxyx 0

110

0

101

0

10

0100

0110

0

Page 9: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 9

ExerciseWhat is the meaning of this expression? Draw a circuit for thisfunction, and calculate the truth table. Which logical operation is it?

yxyx

(x AND (NOT y)) OR ((NOT x) AND y)

x y yx yx yxyx 0

110

0

101

0

10

0100

0110

0

Page 10: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 10

ExerciseWhat is the meaning of this expression? Draw a circuit for thisfunction, and calculate the truth table. Which logical operation is it?

yxyx

(x AND (NOT y)) OR ((NOT x) AND y)

x y yx yx yxyx 0

110

0

101

0

10

0100

0110

0

The operation is XOR.

Page 11: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 11

Multi-Input GatesThe AND and OR operations can be generalized to take any numberof inputs. Algebraically, we simply write xyz for the three-input ANDof x, y and z. Similarly we write x+y+z for the three-input OR.

In circuit diagrams we use the same symbols as before, but withadditional input wires:

Definitions: AND is true if all the inputs are true; OR is true ifat least one of the inputs is true.

NAND and NOR can also be defined for any number of inputs, inthe obvious way.

and3 or3

Page 12: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 12

Synthesis of Multi-Input GatesAn n-input AND or OR gate can be synthesized from 2-input gates ofthe same type.

Exercise: check this by using truth tables.

Exercise: how many 2-input AND gates are needed to synthesize ann-input AND gate?

Exercise: what happens if NAND or NOR gates are joined up like this?

Page 13: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 13

Boolean AlgebraThe algebraic properties of the logical operations were studied byGeorge Boole (1815-1864). As a result we have boolean algebraand the Ada datatype Boolean.

The laws of boolean algebra can be used to rewrite expressionsinvolving the logical operations.

Negation is an involution xx (1)

No contradictions 0xx (2)

AND is idempotent xxx (3)

Page 14: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 14

Laws of Boolean AlgebraExcluded middle 1 xx (4)

OR is idempotent xxx (5)

Zero law for AND 00 x (6)

AND is commutative yxxy (7)

Unit law for AND xx 1 (8)

OR is commutative xyyx (9)

Unit law for OR xx 0 (10)

Distributive law xzxyzyx )( (11)

Page 15: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 15

Laws of Boolean AlgebraOne law for OR 11x (12)

OR is associative zyxzyx )()( (13)

AND is associative zxyyzx )()( (14)

The associativity laws (13) and (14) justify writing xyz and x+y+zfor the 3-input versions of AND and OR: it doesn’t matter whetherwe interpret xyz as x(yz) or as (xy)z.

The laws can be verified by thinking about the ordinary meanings ofAND, OR and NOT, or by truth tables.

Distributive law ))(( zxyxyzx (15)

Page 16: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 16

Compare the laws of Boolean algebra with the laws of ordinary

algebra.Are they:

Exactly the same?

Completely different?

Mostly similar with some differences?

Don't know

Page 17: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 17

ComparisonThe most obvious difference is that Boolean negation has noobvious counterpart in ordinary algebra.

If we just look at AND and OR then we can see similarities:

zyxzyx )()(

xzxyzyx )(

and differences: for example ))(( zxyxyzx

is not true in ordinary algebra (e.g. take x=1, y=1, z=1)

Page 18: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 18

ExampleTo verify that x(y+z) = xy + xz we construct the truth tables for theleft and right hand sides of the equation, considering them both asfunctions of x, y and z.

yx z y+z x(y + z) xy xz xy + xz00 000 110 010 101 001 111 011 1

Page 19: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 19

ExampleTo verify that x(y+z) = xy + xz we construct the truth tables for theleft and right hand sides of the equation, considering them both asfunctions of x, y and z.

yx z y+z x(y + z) xy xz xy + xz00 0 000 1 110 0 110 1 101 0 001 1 111 0 111 1 1

Page 20: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 20

ExampleTo verify that x(y+z) = xy + xz we construct the truth tables for theleft and right hand sides of the equation, considering them both asfunctions of x, y and z.

yx z y+z x(y + z) xy xz xy + xz00 0 0 000 1 1 010 0 1 010 1 1 001 0 0 001 1 1 111 0 1 111 1 1 1

Page 21: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 21

ExampleTo verify that x(y+z) = xy + xz we construct the truth tables for theleft and right hand sides of the equation, considering them both asfunctions of x, y and z.

yx z y+z x(y + z) xy xz xy + xz00 0 0 0 000 1 1 0 010 0 1 0 010 1 1 0 001 0 0 0 001 1 1 1 011 0 1 1 111 1 1 1 1

Page 22: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 22

ExampleTo verify that x(y+z) = xy + xz we construct the truth tables for theleft and right hand sides of the equation, considering them both asfunctions of x, y and z.

yx z y+z x(y + z) xy xz xy + xz00 0 0 0 0 000 1 1 0 0 010 0 1 0 0 010 1 1 0 0 001 0 0 0 0 001 1 1 1 0 111 0 1 1 1 011 1 1 1 1 1

Page 23: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 23

ExampleTo verify that x(y+z) = xy + xz we construct the truth tables for theleft and right hand sides of the equation, considering them both asfunctions of x, y and z.

yx z y+z x(y + z) xy xz xy + xz00 0 0 0 0 0 000 1 1 0 0 0 010 0 1 0 0 0 010 1 1 0 0 0 001 0 0 0 0 0 001 1 1 1 0 1 111 0 1 1 1 0 111 1 1 1 1 1 1

Page 24: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 24

ExampleTo verify that x(y+z) = xy + xz we construct the truth tables for theleft and right hand sides of the equation, considering them both asfunctions of x, y and z.

yx z y+z x(y + z) xy xz xy + xz00 0 0 0 0 0 000 1 1 0 0 0 010 0 1 0 0 0 010 1 1 0 0 0 001 0 0 0 0 0 001 1 1 1 0 1 111 0 1 1 1 0 111 1 1 1 1 1 1

The columns for x(y+z) and xy+xz are identical.

Page 25: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 25

ExerciseUsing the laws of boolean algebra, show that xy + x = x.

Page 26: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 26

ExerciseUsing the laws of boolean algebra, show that xy + x = x.

xy + x = xy + x1 (law 8) = x(y + 1) (law 11) = x1 (law 12) = x (law 8)

Working out which law to use next requires some creativity.Truth tables provide a straightforward, systematic way to checkequivalences.

Notice the similarity with the set membership tables used in the Information Management section to verify set identities.

Page 27: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 27

De Morgan’s LawsTwo important laws relate AND, OR and NOT. They are named afterAugustus De Morgan (1806-1871).

NOT(x AND y) = (NOT x) OR (NOT y) NOT(x OR y) = (NOT x) AND (NOT y)

In algebraic notation:

yxyx

yxxy

“Break the line and change the sign.”

Page 28: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 28

Boolean Algebra in Programming

The laws of boolean algebra apply anywhere that logical operationsare used. For example, the Ada code

if ((x=1) and (y=1)) or ((x=1) and (z=2)) thenwhatever

end if;

is equivalent to

if (x=1) and ((y=1)) or (z=2)) thenwhatever

end if;

Page 29: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 29

Circuits from Truth Tables• In Lecture 5 we constructed a logic circuit which

computes the majority voting function.• The function was defined by an English sentence,

and I wrote down a logical expression and then a circuit by thinking about the ordinary meaning of the sentence.

• In general we need a more systematic approach.• We’ll use majority voting as an example, then

progress to functions such as addition.• Start with the truth table as the definition of the

function to be implemented.

Page 30: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 30

Majority Voting Systematicallyy zx r0 00 00 10 01 00 01 10 10 01 00 11 11 01 11 11 1

For r to be 1, it must be the case that:

x=0 and y=1 and z=1or

x=1 and y=0 and z=1

or

x=1 and y=1 and z=0

or

x=1 and y=1 and z=1

Page 31: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 31

Majority Voting Systematicallyy zx r0 00 00 10 01 00 01 10 10 01 00 11 11 01 11 11 1

Alternatively, for r to be 1, it must be the case that:

x=1 and y=1 and z=1or

x=1 and y=1 and z=1

or

x=1 and y=1 and z=1

or

x=1 and y=1 and z=1

Page 32: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 32

Majority Voting Systematicallyy zx r0 00 00 10 01 00 01 10 10 01 00 11 11 01 11 11 1

Alternatively, for r to be 1, it must be the case that:

xyz=1or

xyz=1

or

xyz=1

or

xyz=1

Page 33: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 33

Majority Voting Systematicallyy zx r0 00 00 10 01 00 01 10 10 01 00 11 11 01 11 11 1

Rewriting one more time, we have discovered that:

xyzzxyzyxyzxr

which gives the following circuit.

Page 34: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 34

Majority Voting Systematicallyxyzzxyzyxyzxr

The expressions xyz etc. are called minterms.

The formula for r is said to be in sum of products form,for obvious reasons.

With n variables there are possible minterms. Each minterminvolves all n variables, and each variable is either negated (x ) ornot negated (just x).

n2

Page 35: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 35

Minterms and the Truth Table

y zx r0 00 00 10 01 00 01 10 10 01 00 11 11 01 11 11 1

Each minterm corresponds to one rowof the truth table, i.e. to one combinationof values (0 or 1) of the variables.

The minterm corresponds to the row inwhich the negated variables have value 0and the non-negated variables have value 1.

The formula for r consists of the mintermscorresponding to the truth table rows inwhich r = 1, ORed together.

xyzzxyzyxyzxr

zyxzyxzyxzyxzyxzyxzyxzyx

Page 36: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 36

Structure of the Circuit• Notice the structure of the circuit: NOT gates to

make negated inputs available, AND gates to produce the required minterms, an OR gate to produce the final output.

• In the same way we can construct a circuit for any function.

• With m inputs, and n rows with output value 1: m NOT, n m-input AND, 1 n-input OR.

• This circuit is more complex than the original majority voting circuit. We will have more to say about this later.

Page 37: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 37

Equality TestSuppose we want to design a circuit which implements the equalitytest function on two inputs. That is, we want to compute r as a functionof x and y, where r will be 1 if x and y have the same value, and 0 ifx and y have different values.

For two variables there are 4 possible minterms, which correspond tothe rows of the truth table as follows.

y rx0 101 000 011 11

yxyxyxyx

Page 38: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 38

Equality Test

y rx0 101 000 011 11

yxyxyxyx

The formula for r is the OR of the two mintermscorresponding to the rows in which r = 1.

xyyxr

The circuit:

Page 39: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 39

ParityThe parity of a binary word is determined by the number of 1s in it:if it contains an odd number of 1s then the parity is 1 (or odd);if it contains an even number of 1s then the parity is 0 (or even).

(Mathematically the parity of a number is sometimes said to be odd for odd numbers and even for even numbers. But for binary words,parity is based on the number of 1s.)

Example: 1010 has even parity. 1101 has odd parity. 11111111 has even parity. 00101010 has odd parity.

Page 40: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 40

ParityThe parity function for a 3 bit word xyz is defined by the followingtruth table, which also shows the minterm for each row.

y zx p0 00 00 10 11 00 11 10 00 01 10 11 01 01 01 11 1

zyxzyxzyxzyxzyxzyxzyxzyx

The formula for p is the OR of thefour minterms corresponding to therows in which p = 1.

xyzzyxzyxzyxp

Page 41: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 41

Exercises1. Draw a circuit for the parity function, in the same way that we didfor majority voting.

2. Find an equivalent circuit, which uses just two XOR gates. Provethat it is equivalent, both by truth tables and by using the laws ofboolean algebra.

Page 42: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 42

Applications of ParityParity checking can be used for error detection, for example incomputer memory.

Suppose that each memory location stores an 8 bit word. A memorydevice with parity checking would actually store 9 bits per word,where the 9th bit is the parity of the original 8 bit word. The paritybit is calculated when a word is stored in memory.

0 1 1 0 1 1 1 0 1in both cases, the 9 bitword has even parity

1 0 1 0 0 1 1 0 0

Page 43: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 43

Applications of ParityWhen a 9 bit word is read from memory, its parity is calculated.If a single bit within the word has been corrupted (changed from0 to 1 or from 1 to 0) then the parity of the word will be odd.

0 1 1 0 1 1 1 0 1

0 1 1 0 1 0 1 0 1

corruption

parity is now odd

0 1 1 0 1 1 1 0 1

0 1 1 0 1 1 1 0 0

corruption

parity is now odd

The computer can tell that a memory error has occurred (it could bebecause of a power fluctuation, for example) and do something(but what?)

Page 44: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 44

Applications of ParityThe same idea can be used when transmitting data over a network.Instead of sending an 8 bit word, send a 9 bit word which includesa parity bit. The receiver can check the parity.

Parity checking cannot correct errors, because it is not possible towork out which bit was corrupted. In a networking application, thecorrupted word would be retransmitted.

Parity checking can only detect single bit errors, because if two bitsare changed then the parity remains the same. It might be acceptableto assume that the probability of two errors in the same word is verysmall.

Page 45: CS1Q Computer Systems Lecture 6

Lecture 6 CS1Q Computer Systems - Simon Gay 45

Error Detection and Error CorrectionIn some applications, errors are inevitable and therefore it is essentialto be able to correct (not just detect) errors. For example, radiotransmissions from spacecraft.

Simple code: send each bit three times. When receiving, calculatea majority decision for each group of three bits.

0 send 000

1 send 111

receive 001 means 0

receive 101 means 1

and so on

This code can correct any single-bit error in each group of three.More sophisticated error correcting codes exist. The data transferrate is always reduced, but by how much?