CSE 20 DISCRETE MATH Fall 2017 http://cseweb.ucsd.edu/classes/fa17/cse20-ab/
Today's learning goals Describe and use algorithms for integer operations based on their expansions Relate algorithms for integer operations to bitwise boolean operations Correctly use XOR and bit shifts List the truth tables and meanings for negation, conjunction, disjunction, exclusive or. Evaluate the truth value of a compound proposition given truth values of its constituent variables. Relate boolean operations to applications in combinatorial circuits
Algorithms For base 2 representations: If n = (a k-1 a 1 a 0 ) 2 then 4n =
CENTR 105: AB CENTR 115: BA Algorithms For base 2 representations: If n = (a k-1 a 1 a 0 ) 2 then 4n = To change your remote frequency 1. Press and hold power button until flashing 2. Enter two-letter code 3. Checkmark / green light indicates success If n = (a k-1 a 1 a 0 ) 2 then n is divisible by 4 exactly when A. k=2 B. a 0 = 1 C. a 1 = 0 D. a 1 = a 0 E. None of the above
Base expansion Rosen p. 246 Notation: for positive integer n Write when (a k-1 a 1 a 0 ) b n = a k-1 b k-1 + + a 1 b + a 0 Base b expansion of n
Terminology Base b expansion aka positional representation base b: Coefficients between 0 and b-1 (inclusive) Leading coefficient nonzero Fixed-width base b expansion: Coefficients between 0 and b-1 (inclusive) Pad leading coefficients as 0 to match desired width
Representing more? Base b expansions can express any positive integers Fixed width base b expansions can express nonnegative integers [0, b k -1] What about negative integers? rational numbers? other real numbers? stay tuned for CSE 30, CSE 140
Fixed width representation, circuits Rosen p. 20 Combinatorial (Logic) Circuit Inputs e.g. coefficients in fixed-width binary representation Outputs e.g. flag OR coefficients in fixed-width binary representation
Circuits for arithmetic Input: fixed-width 3-bit representation of n Output: 4n When might output not be correct? Overflow?
Circuits for arithmetic Input: fixed-width 3-bit representation of n Output: n mod 4
More complicated arithmetic Rosen p. 251, 826 In base b, s k-1 s 1 s 0 + t k-1 t 1 t 0 Algorithm relies on: one symbol sum, carry
Arithmetic + Representations For decimal: one digit sum table 0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1 1 2 3 4 5 6 7 8 9 0 2 2 3 4 5 6 7 8 9 0 1 3 3 4 5 6 7 8 9 0 1 2 4 4 5 6 7 8 9 0 1 2 3 5 5 6 7 8 9 0 1 2 3 4 6 6 7 8 9 0 1 2 3 4 5 7 7 8 9 0 1 2 3 4 5 6 8 8 9 0 1 2 3 4 5 6 7 9 9 0 1 2 3 4 5 6 7 8
Arithmetic + Representations For binary: one bit sum table 0 1 0 0 1 1 1 0
Arithmetic + Representations Alternatively, Input Output x y Sum Carry 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1
Arithmetic + Representations Rosen p. 251,826 Alternatively, XOR AND x y Input Output x y Sum Carry 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 Half adder logic circuit
Computer bit operations Rosen p. 11 x y x v y x OR y x ^ y x AND y x y x XOR y 0 0 0 0 0 0 1 1 0 1 1 0 1 0 1 1 1 1 1 0 "x OR y is 1 if at least one of x or y is 1" "x AND y is 1 if both x and y are 1" "x XOR y is 1 if exactly one of x and y is 1"
Ground? For n à 4n circuit, needed to pad with zeros. How? - Ground: constant 0 - Contradiction: a circuit (function) that always outputs 0 Which of the following is a contradiction? A. x OR x B. x AND x C. x XOR x D. All of the above E. None of the above
Logic gates Rosen p. 21 p p p q p q p q p q Inverter OR gate AND gate p T F p F T XOR gate New contradiction possible:
Logic Rosen Section 1.1 Use gates and circuits to express arithmetic. Precisely express theorems and invariant statements. Make valid arguments to prove theorems.
Circuits Propositions 0 (off) False 1 (on) (Order switched!) True p q p v q p ^ q p q T T T T F T F T F T F T T F T F F F F F
Definitions Rosen p. 2-4 Proposition: declarative sentence that is T or F (not both) Propositional variable: variables that represent propositions. Compound proposition: new propositions formed from existing propositions using logical operators. Truth table: table with 1 row for each of the possible combinations of truth values of the input and an additional column that shows the truth value of the result of the operation corresponding to a particular row.
Truth table: (p v q) v (p v r) How many rows are in the truth table for (p v q) v (p v r)? A. 1 B. 2 C. 3 D. 4 E. None of the above How do we fill in these rows? - Inputs - Output Precedence order of operations on p. 11
Truth tables Can use truth table to compute truth value of compound proposition. Also, can specify logical operator by truth table. Next time: how to prove two tables are equivalent? and how to reverse engineer compound proposition from a truth table?
Reminders Homework 1 due Friday at 11pm Algorithms, number representations Office hours