CSE111: Great Ideas in Computer Science

25
CSE111: Great Ideas in Computer Science Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00- 11:50 645-4739 [email protected]

description

CSE111: Great Ideas in Computer Science. Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 [email protected]. Announcements. No recitations this week. First meeting of recitations in week of 1/25-1/29. - PowerPoint PPT Presentation

Transcript of CSE111: Great Ideas in Computer Science

Page 1: CSE111: Great Ideas in Computer Science

CSE111: Great Ideas in Computer Science

Dr. Carl Alphonce219 Bell Hall

Office hours: M-F 11:00-11:50

[email protected]

Page 2: CSE111: Great Ideas in Computer Science

Announcements

• No recitations this week. First meeting of recitations in week of 1/25-1/29.

• Extra copies of syllabus available at course web-site (address is on UB Learns).

2

Page 3: CSE111: Great Ideas in Computer Science

cell phones off(please)

3

Page 4: CSE111: Great Ideas in Computer Science

Agenda

• Review from last class– binary numbers– binary arithmetic

• Today’s topics– fixed-width representations– two’s complement

4

Page 5: CSE111: Great Ideas in Computer Science

5

Review

• Binary numbers– Digits are ‘0’ and ‘1’– Weight of positions are powers of two

• Binary arithmetic– Same procedure as for base 10

Page 6: CSE111: Great Ideas in Computer Science

6

Counting

Decimal (base 10)

0123456789

10111213

etc.

Binary (base 2)

01

1011

100101110111

100010011010101111001101

etc.

Page 7: CSE111: Great Ideas in Computer Science

7

Number systems

Decimal (base 10)• Each position is weighted by

a power of 10.• E.g. 734 =

– 7*100 + 3*10 + 4*1– 7*102 + 3*101 + 4*100

• E.g. 1101 = – 1*1000 + 1*100 + 0*10 + 1*1– 1*103 + 1*102 + 0*101 + 1*100

Binary (base 2)• Each position is weighted by a

power of 2.• E.g. 111 =

– 1*4 + 1*2 + 1*1 = “seven”– 1*22 + 1*21 + 1*20

• E.g. 1101 = – 1*8 + 1*4 + 0*2 + 1*1 = “thirteen”– 1*23 + 1*22 + 0*21 + 1*20

Page 8: CSE111: Great Ideas in Computer Science

8

Binary Arithmetic

• Operations in base 2 work the same as in base 10.

• Addition: 2 + 3 = 510

+11101

Page 9: CSE111: Great Ideas in Computer Science

9

Exercises

• Compute the following sums, in base 2

5 + 1 = 6 8 + 8 = 16 10 + 12 = 22

Page 10: CSE111: Great Ideas in Computer Science

10

Setting up the exercises

• Compute the following sums, in base 2

5 + 1 = 6 8 + 8 = 16 10 + 12 = 22

101 1000 1010+ 001 + 1000 + 1100

Page 11: CSE111: Great Ideas in Computer Science

11

Solving up the exercises

• Compute the following sums, in base 2

5 + 1 = 6 8 + 8 = 16 10 + 12 = 22

101 1000 1010+ 001 + 1000 + 1100 110 10000 10110

Page 12: CSE111: Great Ideas in Computer Science

12

Fixed-width encodings

• Suppose we have a four-bit wide representation.• We then have 24 = 2*2*2*2 = 16 distinct bit

patterns:0000 10000001 10010010 10100011 10110100 11000101 11010110 11100111 1111

Page 13: CSE111: Great Ideas in Computer Science

13

Exercises

• Compute the following sums, in a four-bit wide base 2 representation:

5 + 1 = 6 8 + 8 = 16 10 + 12 = 22

Page 14: CSE111: Great Ideas in Computer Science

14

Setting up the exercises

• Compute the following sums, in a four-bit wide base 2 representation:

5 + 1 = 6 8 + 8 = 16 10 + 12 = 22

101 1000 1010+ 001 + 1000 + 1100

Page 15: CSE111: Great Ideas in Computer Science

15

Solving the exercises

• Compute the following sums, in a four-bit wide base 2 representation:

5 + 1 = 6 8 + 8 = 16 10 + 12 = 22

101 1000 1010+ 001 + 1000 + 1100 110 0000 0110

Page 16: CSE111: Great Ideas in Computer Science

16

What can they represent?

• Up to sixteen distinct things:– E.g. the numbers 0 through 15, or 1

through 16, or 10 through 25, or …– (notice the limited range!)

– Types of animals (aardvark, bat, cat, dog, …, octopus, penguin)

– Anything else we want!

Page 17: CSE111: Great Ideas in Computer Science

17

How about negative and non-negative numbers?

• How can we assign the bit strings from 0000 to 1111 to numbers, including negative numbers?

• There are many ways to do this, but some ways are better than others.

• Keep in mind, we only have the symbols ‘0’ and ‘1’ to use.

Page 18: CSE111: Great Ideas in Computer Science

18

Desirable properties of our

representationX – Y = X + (-Y)

X + (-X) = 0

0 = - 0

- (-X) = X

Page 19: CSE111: Great Ideas in Computer Science

19

Two’s complement

0000 0 1000 -80001 1 1001 -70010 2 1010 -60011 3 1011 -50100 4 1100 -40101 5 1101 -30110 6 1110 -20111 7 1111 -1

Page 20: CSE111: Great Ideas in Computer Science

20

How does it work?

• Given representation for x, compute representation of –x as follows:– First compute the 1’s complement– Then compute the 2’s complement

Page 21: CSE111: Great Ideas in Computer Science

21

How does it work?

• Given representation for x, compute representation of –x as follows:– First compute the 1’s complement by

inverting all the bits (change 0 to 1, and 1 to 0)

– Then compute the 2’s complement by adding 1, and ignoring any overflow carry bit.

Page 22: CSE111: Great Ideas in Computer Science

22

Examples

• Compute the representation of -1:

0001 original representation1110 one’s complement1111 two’s complement

• Representation of -1 is 1111.

Page 23: CSE111: Great Ideas in Computer Science

23

Check: x + (–x) = 0

0001+ 1111 0000

• Remember, representation is 4 bits wide, and we discard any overflow carry bit.

Page 24: CSE111: Great Ideas in Computer Science

24

Check other properties too!(done on board)

X – Y = X + (-Y)

X + (-X) = 0

0 = - 0

- (-X) = X

Page 25: CSE111: Great Ideas in Computer Science

25

Questions?