SM242 Week 2 (Notes based on Epp Chapter 1) I. Logic (continued) 20. Logic Gates Consider the circuit below which has 2 switches in series: S1 S2 L Light When will the light turn on? We can represent the light's operation using a table: S1 open open shut shut S2 open shut open shut Light off off off on Change the words open and off to F and the words shut and on to T and the table becomes S1 F F T T S2 F T F T This is the truth table for the logical connective. We can say that the circuit above corresponds to S1 S2. Light F F F T Now consider the circuit that has 2 switches in parallel. S1 S2 L Light When will the light turn on? We can represent the light's operation using a table S1 S2 Light open open off open shut on shut open on shut shut on Changing the words open and off to F and the words shut and on to T and the table becomes 1 S1 F F T T S2 F T F T Light F T T T This is the truth table for the logical connective. We can say that the circuit above corresponds to S1 S2. Notes: Usually, when designing circuits, the symbol 0 is used to denote F and the symbol 1 is used to denote T. A compound expression as we are using it here (in the context of logic circuits) is called a Boolean expression. We will develop a more formal definition of a Boolean expression later in the course. To a Computer Scientist, the computer’s hardware is comprised of digital logic circuits. Digital logic circuits are built from just a handful of primitive elements, called logic gates, combined in various ways. In a digital logic circuit, only two values may be present. The values may be 5 and + 5 volts. Or the values may be 0.5 and 3.5 volts. Or the values may be… you get the picture. To allow consideration of all of these possibilities, we will say that digital logic circuits allow the presence of two logical values: 0 and 1. So, signals in a digital logic circuit take on the values of 0 or 1. Logic gates are devices which compute functions of these binary signals. In this course we will consider three logic gates that are used to construct digital logic circuits: A. Not gate (inverter) Has one input and one output. If the input, P, is 1, the output is 0. If the input is 0, the output is 1. Inverter or NOT gate P 0 1 ~P 1 0 Corresponds to the ~ connective B. And gate – has two inputs and one output. If both inputs, say P and Q, are both 1 then the output is 1. Otherwise, the output is 0. P Q PQ 0 0 1 1 0 1 0 1 0 0 0 1 AND gate Corresponds to the connective 2 C) Or gate – has two inputs and one output. If either or both inputs, say P and Q, are 1, the output is 1. Otherwise, the output is 0. OR gate P Q PQ 0 0 1 1 0 1 0 1 0 1 1 1 Corresponds to the connective. 21. Combinational circuits A. A combinational circuit is a digital logic circuit whose output is determined only by the current values of the inputs (with no dependence on past input values). A combinational circuit is built up by combining our three basic logic gates with the following provisos: The output of a gate may not eventually feed back to that same gate. The output lines from 2 different gates cannot be combined. The following are not combinational circuits: A B A C D B E B. Example. The air conditioning plant on a ship is controlled by a logic circuit. The air conditioner will turn on (logical “1”) if the temperature is above 80F (logical “1”). If the motor is overheating (logical “1”), however, a signal is sent to turn the air conditioner off. Sketch (within the box) the logic circuit to accomplish this design. temp > 80F air conditioner motor overheating Answer: 3 C. Our study of combinational circuits will involve five (very much related!) tasks (Task 1) Given a digital logic circuit, write the corresponding Boolean expression. (Task 2) Given a Boolean expression, draw a digital logic circuit that represents this expression. (Task 3) Given a digital logic circuit, construct the corresponding truth table. (Task 4) Given a truth table, construct a digital logic circuit that implements this truth table. (Task 5) Given a digital logic circuit, design a simpler digital logic circuit that performs the equivalent function. TASK 1: Given a digital logic circuit, write the corresponding Boolean expression. The Boolean expression corresponding to a digital logic circuit can be determined by evaluating the effect of the logic gates on the input expressions. Example: Determine the Boolean expression corresponding to the digital logic circuit shown. p q Example. Write the Boolean expression for the logic circuit shown below. p q Evaluating the effect of the logic gates on the input expressions, we see: 4 TASK 2: Given an expression, draw a digital logic circuit that represents this expression. The basic approach is to write the expression on the right side of the page. Draw the circuit from right to left, by working from the outermost part of the expression. Example: Construct a digital logic circuit that will implement the Boolean expression (p ~ q) ~ p At the highest level, we are ~q q p ~ q p ~ q ~ p p ~ p Now, let’s continue to deconstruct this by looking at the top term, ~q q p ~ q p p ~ q ~ p ~ p Finally, ~q q p ~ q p p ~ q ~ p ~ p Example: Draw a digital logic circuit that represents the Boolean expression 5 x (y ~ (x ~ z)) TASK 3: Given a digital logic circuit, construct the corresponding truth table. There are many convenient ways to do this. We can determine the corresponding Boolean expression (TASK 1) and construct a truth table as we did in Week 1. Or, alternatively, we can trace through the circuit for each possible combination of input values. Example: Construct the truth table corresponding to the circuit shown. p q Substitute p = 0 and q = 0 and r = 0 and trace through the circuit gate by gate. Repeat this for the other combinations of input values. The resulting truth table is p q output Example. Construct the truth table for the circuit shown below. p q p q output Have you seen this truth table before? 6 Example. Write the Boolean expression for the logic circuit shown below, and then construct the truth table for it. p q r Evaluating the effect of the logic gates on the input expressions, we see that the output is: How many rows will be in this truth table? The truth table is p q r Aside an aside (and not related to the example above), note that for AND gates and OR gates, we will allow more than two inputs. For a multi-input AND gate, the output is one if ALL of the inputs are 1. For a multi-input OR gate, the output is 1 is ANY one of the inputs is a 1 (or if more than one input is a 1). The example below shows what we mean. Example. Design a digital logic circuit to implement the Boolean expression ~ A B ~ C D (a) (b) You only have 2-input gates available. You have gates with any number of inputs allowed. 7 if: TASK 4: Given a truth table, construct a digital logic circuit that implements this truth table. Suppose the input expressions are x1 , x 2 ,... x n . a. Identify the rows of the truth table that have an output of 1. b. For each such row, form the Boolean expression. z1 z2 ... z n where zi x i if x i 1 in this row, and z i ~ x i if x i 0 in this row. c. After step b., we will have one Boolean expression corresponding to each row that had an output of 1. Take the disjunction of all these Boolean expressions. The resulting expression has the truth table under evaluation. (As an aside, an expression such as this, which is a disjunction of several terms, where each of these terms is a conjunction of irreducible expressions, is said to be disjunctive normal form.) d. Form the digital logic circuit from this Boolean expression using the technique discussed in TASK 2 above. Example: Construct a logic circuit for the truth table shown. p q output 0 0 1 1 0 1 0 1 0 1 0 1 Solution: p q output 0 0 1 1 0 1 0 1 0 1 0 1 8 Example: Construct a logic circuit for the truth table shown. A B C 9 Output TASK 5: Given a digital logic circuit, design a simpler digital logic circuit that performs the equivalent function. a. Find the Boolean expression that corresponds to this logic circuit (TASK 1 above). b. Use the Table on page 14 of the text to find a simpler logically equivalent Boolean expression. c. Given this simpler Boolean expression, draw the digital logic circuit that represents this expression (TASK 2 above). This simpler circuit will perform precisely the same way as the original (more complex) circuit. Example. Suppose we find that a logic circuit has the Boolean expression ~ x ~ y ~ z ~ x ~ y z Can this circuit be simplified? ~ x ~ y ~ z ~ x ~ y z Note: In IC220 (Computer Architecture and Organization) you will learn an important additional technique (the Karnaugh Map) to accomplish TASK 5 above. 22. Examples Question 1 You have been tasked with designing the sprinkler system for the Commanding Officer’s stateroom. The CO’s stateroom has three smoke alarms. Since the CO will be very angry if his stateroom is drenched by a false or spurious alarm, you decide that the sprinkler system will not be activated by any single smoke alarm. The sprinkler system will activate if any two of the smoke alarms activate (or if all three activate). a. Write a truth table that describes how the sprinkler operation will depend on the status of the three smoke alarms. (Clearly state what you intend by logical 1 and logical 0.) b. Based on your truth table, design the digital logical circuit. Let logical 1 mean the alarm/sprinkler is on. 10 Question 2 Let’s pretend that there are four midshipman who are very good friends (remember, this is just pretending). Their names are: MIDN Miller MIDN Porter MIDN Lees MIDN Wolz Although they are all good friends, MIDN Lees and MIDN Wolz are youngsters, so they “kinda-sorta” get, shall we say, “outvoted” when it comes to some decision-making. Our four midshipmen always eat dinner together. In fact, they always eat dinner together in one of two locations: King Hall or California Pizza Kitchen (CPK). Each day, at 1645, they take a vote to decide where all four will eat. The voting works as follows: If there is a clear majority, the majority wins. If there is a tie vote, they all eat in King Hall with one exception: If MIDN Miller and MIDN Porter agree on CPK, they pull rank and their choice (CPK) wins out. Design a digital logic circuit that will allow our four midshipmen to cast their votes electronically. Based on the votes, the logic circuit will decide (based on the rules above) where our midshipmen will be eating dinner that day. (To make our task of grading easier, please use the following convention: Let 1 denote a vote for CPK and 0 denote a vote for King Hall.) Let 1 = CPK, 0 = King Hall 11 23. Number Representations A. Decimal Integers and Decimal Notation The decimal number system has 10 digits (0,1,2,3…9). Since it is based on 10 distinct symbols, we say the base is 10. In decimal notation, we write a number as a string of digits. To interpret a decimal number, we multiply each digit by the power of 10 associated with that digit’s position. Example: Consider the decimal number: 6349. This number is: 6 3 4 9 B. The Binary Number System The binary number system has two digits (0 and 1). The base is two. Just as with decimal notation, we write a number as a string of digits, but now each digit is 0 or 1. To interpret a binary number, we multiply each digit by the power of 2 associated with that digit’s position. Example: Consider the binary number 1011. This number is: 1 0 1 1 = C. Converting a binary number to a decimal number To convert a binary number to a decimal number, write the binary number as a sum of powers of 2. Example: Express the binary number 1011 as a decimal number. Note: We must be careful that the base is understood. When we say “11” above, we mean the number 11 in base 10, not the number 11 in base 2. If the base is not clear from context, it can be made explicit by including the base as a subscript as in: 10112 1110 Example: Express the binary number 110110 as a decimal number. 12 D. Converting a decimal integer to a binary number 1. Method 1. Express the decimal number as a sum of powers of 2. To do this: i. find the highest power of two less than or equal to the decimal number. The binary representation will have a one in this position. ii. Now subtract this power of two from the original decimal number. iii. If this new decimal number is zero, we’re done. Otherwise return to step i. above. Example: Convert the decimal number 78 to binary. Think to yourself: Self, what is the largest power of 2 that is less than or equal to 78. 25 32 is a power of 2 that is less than 78, but is it the largest? So, the binary representation of 78 will have a one in the 26 64 position: _____ 26 64 _____ 25 32 _____ 2 4 16 _____ 23 8 _____ 22 4 _____ 21 2 _____ 20 1 Now, subtracting 64 from our number 78 gives 78 64 14 . Thus, 14 is now the number we are working with. What is the largest power of 2 that is less than or equal to 14? _____ 26 64 _____ 25 32 _____ 2 4 16 _____ 23 8 _____ 22 4 _____ 21 2 _____ 20 1 Now, subtracting 8 from our number 14 gives 14 8 6 . Thus, 6 is now the number we are working with. What is the largest power of 2 that is less than or equal to 6? Answer: _____ 26 64 _____ 25 32 _____ 2 4 16 _____ 23 8 _____ 22 4 _____ 21 2 _____ 20 1 Now, subtracting 4 from our number 6 gives 6 4 2 . Thus, 2 is now the number we are working with. What is the largest power of 2 that is less than or equal to 2? 13 _____ 26 64 _____ 25 32 _____ 2 4 16 _____ 23 8 _____ 22 4 _____ 21 2 _____ 20 1 Now, subtracting 2 from our number 2 gives 0, so we are done. Filling in the zeros, we have our answer: The decimal number 78 in binary is _____ 26 64 _____ 25 32 _____ 2 4 16 _____ 23 8 _____ 22 4 _____ 21 2 _____ 20 1 Example: Convert the decimal number 201 to binary. At the risk of insulting your intelligence, we ask that you “memorize” the binary representations of the decimal digits 1 through 16. 2. Method 2. Repeated division by 2 i. Divide the number by 2 to obtain a quotient and remainder. (The remainder will be 0 or 1) ii. If the quotient is zero, we’re done (proceed to step iii). Otherwise, go back to step 1., treating our quotient as the number under examination. iii. The sequence of remainders forms the binary representation of the number. Example: Convert the decimal number 53 to binary. 14 Example: Convert the decimal number 201 to binary. Using the algorithm, your answer should match the representation shown on the prior page. E. Binary Arithmetic 1. Example Translate the decimal first grade problem 1 + 1 = 2 to binary notation. We see that when we add two binary ones together, we have a carry into the next column. This idea of “carrying over to the next column” when adding binary numbers together is analogous to carrying over when adding decimal numbers: if the result of the addition is a number too big to be represented as a single digit in the number system, we carry a one over to the next column. 2. Example Translate the second grade decimal problem 1 + 1 + 1 = 3 to binary notation. We see that when we add three binary ones together, we have a carry into the next column. 3. Example. Perform the addition of the two binary numbers: + 1 0 1 1 0 1 1 0 1 1 ------------- + 1 1 1 1 1 0 1 -------- F. Negative Binary Numbers 1. Two’s complement. To find the two’s complement of a binary number, change each 1 to a zero and each zero to a one (i.e., invert all the bits), then add one to this. 2. Example: What is the two’s complement of the binary number 10010010 ? 15 3. Negative binary numbers using two’s complement notation. To find the representation of a negative decimal number: First find the binary representation of the number without the negative sign Then take the two’s complement. The result is the representation of the negative number. 4. Example: Express the decimal integer –13 as an eight bit binary number. 5. Example: Express the decimal integer 53 as an eight bit binary number. 6. Why is the two’s complement notation a good representation for negative numbers? A binary number, added to its two’s complement, results in zero. This is “how its ‘sposed to be: a number added to its negative should give zero. Example: 53: -53: 0 0 1 1 0 1 0 1 1 1 0 0 1 0 1 1 7. Note that given the binary representation of a negative number in two’s complement notation, we can use the “inverting all the bits and adding one” idea to find the corresponding decimal equivalent. Example. What is the decimal equivalent of this 32-bit two’s complement number? 1111 1111 1111 1111 1111 1111 1111 1100 G. Storage of integers (C++) Positive integers are stored in four bytes by converting the integer to a binary number. The right most 31 bits are used. The left most bit is set to zero (indicating a positive number). 16 Negative integers are stored in four bytes by converting the integer to a binary number in two’s complement notation. The right most 31 bits are used. The left most bit will be a one. Can you explain the output of the C++ program shown below? #include<iostream> using namespace std; int main( ) { int number = 2147483646; cout << number << endl; number = number + 1; cout << number << endl; number = number + 1; cout << number << endl; number = number + 1; cout << number << endl; number = number + 1; cout << number << endl; return 0; } H. Digital Logic Circuits for Addition Question: Design a digital logic circuit that will add two single bit binary numbers. How many inputs? How many outputs? What is the truth table? What can be used to implement the carry bit? 17 What can be used to implement the sum bit? The circuit is: This circuit is called a half-adder. Example. Adding Three Bits (Full Adder) Design a digital logic circuit to add three bits together. Call the three inputs A, B and C. What is the truth table? Using the techniques you learned earlier, you can show that the Boolean expression for the Carry bit, after some simplification, is What would be the logic circuit for the Carry bit? You should convince yourself that the Carry bit can be implemented by two XORs. A B Carry C This circuit (i.e., these two circuits together) is called a full adder. 18 Sum Sum Example. Consider representing the half-adder and the full-adder as shown below: Carry A B A Half adder Carry B Full adder Sum Carry C Sum A Sum Design a digital logic circuit that can add together 2-bit adder, i.e., an adder that will add the bits WX to B the bits YZ. Carry A B Half adder Carry Sum I. Hexadecimal Representation of numbers The hexadecimal number system has 16 digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F). The base is 16. Just as with decimal notation, we write a number as a string of digits, but now each digit is one of the 16 possible hexadecimal digits. To interpret a hexadecimal number, we multiply each digit by the power of 16 associated with that digit’s position. Example: Consider the hexadecimal number 1A9B. This number is: 1 A 9 B J. Converting a hexadecimal number to a decimal number To convert a hexadecimal number to a decimal number, write the hexadecimal number as a sum of powers of 16. Example: Express the hexadecimal number 3CB as a decimal number. 19 K. Converting a hexadecimal number to a binary number We can convert directly from hexadecimal notation to the equivalent binary representation by using the following procedure: Convert each hexadecimal digit to a four digit binary number Concatenate the resulting four bit binary numbers Example: Convert the hexadecimal number 4DA9 to binary. Example: Convert the hexadecimal number 13F to binary. L. Converting a binary number to a hexadecimal number We can convert directly from binary notation to the equivalent hexadecimal representation by using the following procedure: Starting at the right, collect the bits in groups of 4 Convert each group of 4 bits into the equivalent hexadecimal number Concatenate the resulting hexadecimal numbers Example: Convert 110110101001 to hexadecimal. Example Suppose the first byte of a variable is stored at memory location numbered: 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 We say that this memory location is the address of the variable. What is this variable’s address in hexadecimal notation? 0000 0000 0001 0010 1111 1111 0111 1100 Suppose the next variable is stored at the location four bytes later. What is the address of this location? 20 24. Arguments Historical note: The study of arguments dates back to Aristotle, who developed rules for reasoning sometime around 350 B.C. Leibniz, in the 1600’s, first considered using symbols to mechanize the process of reasoning. Boole and DeMorgan (1800’s) established the subject of symbolic logic along the lines that are still used today. A. Argument. An argument is a series of statements P1 , P2 ,.... Pn called premises (or assumptions or hypothesis), followed by a statement C called the conclusion (or assertion). B. Deductive logic: the set of tools that can be used to analyze the form of an argument to determine if a conclusion necessarily follows from the truth of the preceding statements. Only the argument's form is considered, not the content. C. Example. Consider the two arguments: ARGUMENT 1: If SM242 is easy or I do all the SM242 homework, then I will do well in the course. I do all the SM242 homework. Therefore, I will do well in the course. Here, the first two sentences are the premises. The premises are presumed to be true. The conclusion is: “I will do well in the course.” (Do you think this is a valid argument?) ARGUMENT 2: If I get 7 hours sleep or I drink 5 cups of coffee then, I will be awake in SM242. I drank 5 cups of coffee. Therefore, I will be awake in SM242. Here, the first two sentences are the premises. The premises are presumed to be true. The conclusion is: “I will be awake in SM242.” (Do you think this argument is valid?) These two arguments have the same logical form. If we use p, q and r to represent the statements in the argument, then both arguments have the form: Or, using symbolic logic: D. Example. Consider the following argument. If MIDN Waymouth lives in Bancroft Hall, then MIDN Waymouth is in the Navy MIDN Waymouth lives in Bancroft Hall MIDN Waymouth is in the Navy. This argument has the form 21 25. Validity of arguments An argument is valid iff whenever all the premises are true, then the conclusion is also true. We can’t have a valid argument with true premises and a false conclusion. The truth of a valid argument’s conclusion follows necessarily from the truth of the premises. (Note that it is not the case that the conclusion of a valid argument is always true. It is the case, however, that if you grant all the hypotheses in a valid argument, then you must also grant the conclusion.) This gives us the following method for testing an argument’s validity: 1. Identify the premises and the conclusion. 2. Write a truth table showing the truth-values of all the premises and the conclusion. 3. Identify the rows in which all the premises are true. If the conclusion is also true in each of these rows, the argument is valid. Otherwise, the argument is invalid. A. Example: Consider the argument: He is a CS major, an IT major or an ESE major. He is not an ESE major. Therefore he is a CS major or an IT major. Symbolically: p q r F F F F F T F T F F T T T F F T F T T T F T T T 22 B. Example: Consider the following argument: If I am a midshipman then I eat lunch in King Hall or I do not eat lunch. If I eat lunch in King Hall then I am a midshipman and I do eat lunch. If I am a midshipman then I do eat lunch. Symbolically p q r F F F F F T F T F F T T T F F T F T T T F T T T C. Note: If we designate the argument premises as P1 , P2 ,.... Pn and the conclusion as C, then our method above for showing that an argument is valid is the same as showing that always true ( a tautology). 23 ( P1 P2 .... Pn ) C is