Вы находитесь на странице: 1из 51

CSE20 - Discrete Mathematics

Dr. Cynthia Bailey Lee Dr. Mia Minnes


csemoodle.ucsd.edu ted.ucsd.edu and www.math.ucsd.edu/~minnes/cse20 and minnes@math.ucsd.edu

iClicker frequency BB

Minnes (CSE20)

6/5/2013

1 / 16

Todays topics

Computer arithmetic
Adding two 1-digit binary numbers Adding two 4-digit binary numbers

Computer number systems


Hexadecimal Twos complement

Minnes (CSE20)

6/5/2013

2 / 16

Designing adder hardware

Can represent each column in the long addition of binary number using a truth table: p 0 0 1 1 q 0 1 0 1 Will we have a carry? p+q

Minnes (CSE20)

6/5/2013

3 / 16

Designing adder hardware

Can represent each column in the long addition of binary number using a truth table: p 0 0 1 1 q 0 1 0 1 Will we have a carry? 0 0 0 p+q 0 1 1

Minnes (CSE20)

6/5/2013

3 / 16

Designing adder hardware

Can represent each column in the long addition of binary number using a truth table: p 0 0 1 1 q 0 1 0 1 Will we have a carry? 0 0 0 1 p+q 0 1 1 0

Minnes (CSE20)

6/5/2013

3 / 16

Designing adder hardware

Can represent each column in the long addition of binary number using a truth table: Which hardware implements addition? Carry: p q Sum: p q

Minnes (CSE20)

6/5/2013

3 / 16

Designing adder hardware, contd


Later columns in the sum may need to incorporate the carry bits. Carry in is bit carried over from previous column Carry out is bit that will carry over to next column p 0 0 0 0 1 1 1 1 q 0 0 1 1 0 0 1 1 Carry in? 0 1 0 1 0 1 0 1 Carry out? p+q

Minnes (CSE20)

6/5/2013

4 / 16

Designing adder hardware, contd


Later columns in the sum may need to incorporate the carry bits. Carry in is bit carried over from previous column Carry out is bit that will carry over to next column p 0 0 0 0 1 1 1 1 q 0 0 1 1 0 0 1 1 Carry in? 0 1 0 1 0 1 0 1 Carry out? 0 0 0 1 p+q 0 1 1 0

Minnes (CSE20)

6/5/2013

4 / 16

Designing adder hardware, contd


Later columns in the sum may need to incorporate the carry bits. Carry in is bit carried over from previous column Carry out is bit that will carry over to next column p 0 0 0 0 1 1 1 1 q 0 0 1 1 0 0 1 1 Carry in? 0 1 0 1 0 1 0 1 Carry out? 0 0 0 0 1 p+q 0 1 1 1 0

Minnes (CSE20)

6/5/2013

4 / 16

Designing adder hardware, contd


Later columns in the sum may need to incorporate the carry bits. Carry in is bit carried over from previous column Carry out is bit that will carry over to next column p 0 0 0 0 1 1 1 1 q 0 0 1 1 0 0 1 1 Carry in? 0 1 0 1 0 1 0 1 Carry out? 0 0 0 1 0 1 p+q 0 1 1 0 1 0

Minnes (CSE20)

6/5/2013

4 / 16

Designing adder hardware, contd


Later columns in the sum may need to incorporate the carry bits. Carry in is bit carried over from previous column Carry out is bit that will carry over to next column p 0 0 0 0 1 1 1 1 q 0 0 1 1 0 0 1 1 Carry in? 0 1 0 1 0 1 0 1 Carry out? 0 0 0 1 0 1 1 p+q 0 1 1 0 1 0 0

Minnes (CSE20)

6/5/2013

4 / 16

Designing adder hardware, contd


Later columns in the sum may need to incorporate the carry bits. Carry in is bit carried over from previous column Carry out is bit that will carry over to next column p 0 0 0 0 1 1 1 1 q 0 0 1 1 0 0 1 1 Carry in? 0 1 0 1 0 1 0 1 Carry out? 0 0 0 1 0 1 1 1 p+q 0 1 1 0 1 0 0 1

Minnes (CSE20)

6/5/2013

4 / 16

Designing adder hardware, contd


Later columns in the sum may need to incorporate the carry bits. Carry in is bit carried over from previous column Carry out is bit that will carry over to next column p 0 0 0 0 1 1 1 1 q 0 0 1 1 0 0 1 1 Carry in? 0 1 0 1 0 1 0 1 Carry out? 0 0 0 1 0 1 1 1 p+q 0 1 1 0 1 0 0 1

We can write down logic propositions for each of the output columns: cout = p+q =
Minnes (CSE20) 6/5/2013 4 / 16

Designing adder hardware, contd


Later columns in the sum may need to incorporate the carry bits. Carry in is bit carried over from previous column Carry out is bit that will carry over to next column p 0 0 0 0 1 1 1 1 q 0 0 1 1 0 0 1 1 Carry in? 0 1 0 1 0 1 0 1 Carry out? 0 0 0 1 0 1 1 1 p+q 0 1 1 0 1 0 0 1

We can write down logic propositions for each of the output columns: cout = (p q cin ) (p (q cin )) p+q =
Minnes (CSE20) 6/5/2013 4 / 16

Designing adder hardware, contd


Later columns in the sum may need to incorporate the carry bits. Carry in is bit carried over from previous column Carry out is bit that will carry over to next column p 0 0 0 0 1 1 1 1 q 0 0 1 1 0 0 1 1 Carry in? 0 1 0 1 0 1 0 1 Carry out? 0 0 0 1 0 1 1 1 p+q 0 1 1 0 1 0 0 1

We can write down logic propositions for each of the output columns: cout = (p q cin ) (p (q cin )) (q cin ) (p (q cin )) p+q =
Minnes (CSE20) 6/5/2013 4 / 16

Designing adder hardware, contd


Later columns in the sum may need to incorporate the carry bits. Carry in is bit carried over from previous column Carry out is bit that will carry over to next column p 0 0 0 0 1 1 1 1 q 0 0 1 1 0 0 1 1 Carry in? 0 1 0 1 0 1 0 1 Carry out? 0 0 0 1 0 1 1 1 p+q 0 1 1 0 1 0 0 1

We can write down logic propositions for each of the output columns: cout = (p q cin ) (p (q cin )) (q cin ) (p (q cin )) p + q = (p q ) cin
Minnes (CSE20) 6/5/2013 4 / 16

Adder gate reuse

Logic propositions have many equivalent forms. However, some lend themselves better to reuse. cout = (q cin ) (p (q cin )) p + q = (p q ) cin

Minnes (CSE20)

6/5/2013

5 / 16

Adder gate reuse

Logic propositions have many equivalent forms. However, some lend themselves better to reuse. cout = (q cin ) (p (q cin )) (p q ) p + q = (p q ) cin (p q ) cin

Minnes (CSE20)

6/5/2013

5 / 16

Adder gate reuse


Logic propositions have many equivalent forms. However, some lend themselves better to reuse. cout = (q cin ) (p (q cin )) (p q ) p + q = (p q ) cin 4-bit adder, with carry (p q ) cin

Minnes (CSE20)

6/5/2013

5 / 16

Hexadecimal
Number systems pros and cons: Decimal
Pros familiar, relatively concise Cons Hard to translate to signals Cons Verbose

Binary
Pros compatible with logic, circuits

For example, the number 1000 uses 4 digits but would require 9 bits: 1000d = 1111101000b

Minnes (CSE20)

6/5/2013

6 / 16

Hexadecimal
Number systems pros and cons: Decimal
Pros familiar, relatively concise Cons Hard to translate to signals Cons Verbose

Binary
Pros compatible with logic, circuits

For example, the number 1000 uses 4 digits but would require 9 bits: 1000d = 1111101000b Hexadecimal system is base 16. Notation: number in hex starts with 0x . Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B , C , D , E , F Since 16 = 24 , one hex digit replaces 4 binary digits.
Minnes (CSE20) 6/5/2013 6 / 16

Number systems
Decimal
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Minnes (CSE20)

Hex
0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13

Binary
0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 11000 11001 11010 11011
6/5/2013 7 / 16

Hexadecimal example

What is the binary representation of 0x 5ABF 1?

A. 10110101011111100010 *B. 01011010101111110001 C. 00010010101001010111 D. 10101110101011101001 E. None of the above

Minnes (CSE20)

6/5/2013

8 / 16

What about negative numbers?

One option:
Use unsigned representation for the absolute value of the number. Have an additional ag bit with 1 meaning the number is negative and 0 meaning the number is positive. Problem: circuits for arithmetic get really messy!

Minnes (CSE20)

6/5/2013

9 / 16

What about negative numbers?

One option:
Use unsigned representation for the absolute value of the number. Have an additional ag bit with 1 meaning the number is negative and 0 meaning the number is positive. Problem: circuits for arithmetic get really messy!

Another option: Twos complement


This is a signed number system. The most signicant bit is used to ag the sign of the number: 1 meaning the number is negative and 0 meaning the number is positive. But, the rest of the bits do not just follow the unsigned representation.

Minnes (CSE20)

6/5/2013

9 / 16

Twos complement

For N Z in decimal notation: If N 0, use unsigned notation to compute twos complement. If N < 0,
Write |N | in unsigned binary Insert additional 0 in front (as most signicant bit) Flip all the bits Add 1.

Minnes (CSE20)

6/5/2013

10 / 16

Twos complement example

What is twos complement notation for 100?

Minnes (CSE20)

6/5/2013

11 / 16

Twos complement example

What is twos complement notation for 100? Write | 100| in unsigned binary 100 d = Insert additional 0 in front (as most signicant bit) Flip all the bits Add 1.

Minnes (CSE20)

6/5/2013

11 / 16

Twos complement example

What is twos complement notation for 100? Write | 100| in unsigned binary 100 d = 1100100 b Insert additional 0 in front (as most signicant bit) Flip all the bits Add 1.

Minnes (CSE20)

6/5/2013

11 / 16

Twos complement example

What is twos complement notation for 100? Write | 100| in unsigned binary 100 d = 1100100 b Insert additional 0 in front (as most signicant bit) 01100100 Flip all the bits Add 1.

Minnes (CSE20)

6/5/2013

11 / 16

Twos complement example


What is twos complement notation for 100? Write | 100| in unsigned binary 100 d = 1100100 b Insert additional 0 in front (as most signicant bit) 01100100 Flip all the bits 10011011 Add 1. 10011011 +00000001 10011100
Minnes (CSE20) 6/5/2013 11 / 16

Translating from twos complement to decimal


Given bn bn1 b1 b0 in twos complement, its absolute value is 2bn + 2bn1 + + 2b1 + 2b0

Minnes (CSE20)

6/5/2013

12 / 16

Translating from twos complement to decimal


Given bn bn1 b1 b0 in twos complement, its absolute value is 2bn + 2bn1 + + 2b1 + 2b0 What is decimal value of 11010 in twos complement? A. B. *C. D. E. 10 8 6 26 None of the above
Minnes (CSE20) 6/5/2013 12 / 16

Most signicant bit


Notice that for positive numbers, repeating the MSB does not change the value of the number being represented 01010 b = 00001010 b = 00000000000001010 b

Minnes (CSE20)

6/5/2013

13 / 16

Most signicant bit


Notice that for positive numbers, repeating the MSB does not change the value of the number being represented 01010 b = 00001010 b = 00000000000001010 b Cool fact: The same is true about negative numbers: For example, 11010 b = 11111010 b = 111111111111111010 b

Minnes (CSE20)

6/5/2013

13 / 16

Most signicant bit


Notice that for positive numbers, repeating the MSB does not change the value of the number being represented 01010 b = 00001010 b = 00000000000001010 b Cool fact: The same is true about negative numbers: For example, 11010 b = 11111010 b = 111111111111111010 b Lets conrm this for this example: 11010 b = 24 + 23 + 21 = 16 + 8 + 2 = 6 d 11111010 b = 27 +26 +25 +24 +23 +21 = 128+64+32+16+8+2 = 6 d

Minnes (CSE20)

6/5/2013

13 / 16

Proof

Theorem For all m 1, n 1, b0 , . . . , bn {0, 1}, 1m bn b0 b = 1bn b0 b . Proof. By induction on m. Base step:

Minnes (CSE20)

6/5/2013

14 / 16

Proof

Theorem For all m 1, n 1, b0 , . . . , bn {0, 1}, 1m bn b0 b = 1bn b0 b . Proof. By induction on m. Base step:

Minnes (CSE20)

6/5/2013

14 / 16

Proof

Theorem For all m 1, n 1, b0 , . . . , bn {0, 1}, 1m bn b0 b = 1bn b0 b . Proof. By induction on m. Base step: (m = 1)
WTS n 1b1 , . . . , bn {0, 1} (1bn b0 = 1bn b0 ) Let n 1, b1 . . . , bn {0, 1} then 1 bn b0 = 1 bn b0

Minnes (CSE20)

6/5/2013

14 / 16

Proof

Theorem For all m 1, n 1, b0 , . . . , bn {0, 1}, 1m bn b0 b = 1bn b0 b . Proof. By induction on m. Base step: Induction step: Let m 1.

Assume that for all r 1, c0 , . . . , cr {0, 1}, 1m cn c0 = 1cn c0 . WTS works for m + 1 Let n 1 and let b0 , . . . , bn {0, 1}. Then Ind Assum 1m+1 bn b0 = 1m 1bn b0 = 11bn b0 .

Minnes (CSE20)

6/5/2013

14 / 16

Proof
Theorem For all m 1, n 1, b0 , . . . , bn {0, 1}, 1m bn b0 b = 1bn b0 b . Proof. By induction on m. Base step: Induction step: Let m 1.

Assume that for all r 1, c0 , . . . , cr {0, 1}, 1m cn c0 = 1cn c0 . WTS works for m + 1 Let n 1 and let b0 , . . . , bn {0, 1}. Then Ind Assum = 11bn b0 . 1m+1 bn b0 = 1m 1bn b0 By denition of twos complement, this has value: 2n+2 + 2n+1 + bn 2n + + b0 But, 2n+2 + 2n+1 = 2n+1 (2 + 1) = 2n+1 .

Minnes (CSE20)

6/5/2013

14 / 16

Proof
Theorem For all m 1, n 1, b0 , . . . , bn {0, 1}, 1m bn b0 b = 1bn b0 b . Proof. By induction on m. Base step: Induction step: Let m 1.

Assume that for all r 1, c0 , . . . , cr {0, 1}, 1m cn c0 = 1cn c0 . WTS works for m + 1 Let n 1 and let b0 , . . . , bn {0, 1}. Then Ind Assum = 11bn b0 . 1m+1 bn b0 = 1m 1bn b0 By denition of twos complement, this has value: 2n+2 + 2n+1 + bn 2n + + b0 But, 2n+2 + 2n+1 = 2n+1 (2 + 1) = 2n+1 . So, 1m+1 bn b0 = 2n+1 + bn 2n + + b0 . This is exactly the value of 1bn b0 (in twos complement)!

Minnes (CSE20)

6/5/2013

14 / 16

Signed arithmetic

Goal: a + (b ) = a b .

Minnes (CSE20)

6/5/2013

15 / 16

Signed arithmetic

Goal: a + (b ) = a b . Twos complement makes this work! For example, consider 10011011 + 01001001

Minnes (CSE20)

6/5/2013

15 / 16

Signed arithmetic

Goal: a + (b ) = a b . Twos complement makes this work! For example, consider 10011011 + 01001001 11100100

Minnes (CSE20)

6/5/2013

15 / 16

Signed arithmetic

Goal: a + (b ) = a b . Twos complement makes this work! For example, consider 10011011 + 01001001 11100100 Converting each line to decimal:

Minnes (CSE20)

6/5/2013

15 / 16

Signed arithmetic
Goal: a + (b ) = a b . Twos complement makes this work! For example, consider 10011011 + 01001001 11100100 Converting each line to decimal: (128 + 16 + 8 + 2 + 1) + (64 + 8 + 1)

Minnes (CSE20)

6/5/2013

15 / 16

Signed arithmetic
Goal: a + (b ) = a b . Twos complement makes this work! For example, consider 10011011 + 01001001 11100100 Converting each line to decimal: 101 + 73

Minnes (CSE20)

6/5/2013

15 / 16

Signed arithmetic
Goal: a + (b ) = a b . Twos complement makes this work! For example, consider 10011011 + 01001001 11100100 Converting each line to decimal: 101 + 73 28

Minnes (CSE20)

6/5/2013

15 / 16

Signed arithmetic
Goal: a + (b ) = a b . Twos complement makes this work! For example, consider 10011011 + 01001001 11100100 Converting each line to decimal: 101 + 73 28 Does 11100100b = 28d ? *A. Yes B. No C. ????
Minnes (CSE20) 6/5/2013 15 / 16

Shirt ...

Minnes (CSE20)

6/5/2013

16 / 16