 Download Download Presentation CSE111: Great Ideas in Computer Science

# CSE111: Great Ideas in Computer Science

Télécharger la présentation ## CSE111: Great Ideas in Computer Science

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. CSE111: Great Ideas in Computer Science Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:50 645-4739 alphonce@buffalo.edu

2. 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).

3. cell phones off (please)

4. Agenda • Review from last class • binary numbers • binary arithmetic • Today’s topics • fixed-width representations • two’s complement

5. Review • Binary numbers • Digits are ‘0’ and ‘1’ • Weight of positions are powers of two • Binary arithmetic • Same procedure as for base 10

6. Counting Decimal (base 10) Binary (base 2) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 etc. 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 etc.

7. Number systems Decimal (base 10) 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 • 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

8. Binary Arithmetic • Operations in base 2 work the same as in base 10. • Addition: 2 + 3 = 5 10 +11 101

9. Exercises • Compute the following sums, in base 2 5 + 1 = 6 8 + 8 = 16 10 + 12 = 22

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

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

12. Fixed-width encodings • Suppose we have a four-bit wide representation. • We then have 24 = 2*2*2*2 = 16 distinct bit patterns:

13. Exercises • Compute the following sums, in a four-bit wide base 2 representation: 5 + 1 = 6 8 + 8 = 16 10 + 12 = 22

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

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

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!

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.

18. Desirable properties of our representation X – Y = X + (-Y) X + (-X) = 0 0 = - 0 - (-X) = X

19. Two’s complement

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 

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.

22. Examples • Compute the representation of -1: 0001  original representation 1110  one’s complement 1111  two’s complement • Representation of -1 is 1111.

23. Check: x + (–x) = 0 0001 + 1111 0000 • Remember, representation is 4 bits wide, and we discard any overflow carry bit.

24. Check other properties too!(done on board) X – Y = X + (-Y) X + (-X) = 0 0 = - 0 - (-X) = X

25. Questions?