mod odule 3 representing values in a com omputer du due
play

Mod odule 3: Representing Values in a Com omputer Du Due Da - PowerPoint PPT Presentation

Mod odule 3: Representing Values in a Com omputer Du Due Da Dates Assignment #1 is due Thursday January 19th at 4pm Pre-class quiz #4 is due Thursday January 19th at 7pm. Assigned reading for the quiz: Epp, 4th edition: 2.3


  1. Mod odule 3: Representing Values in a Com omputer

  2. Du Due Da Dates • Assignment #1 is due Thursday January 19th at 4pm • Pre-class quiz #4 is due Thursday January 19th at 7pm. • Assigned reading for the quiz: • Epp, 4th edition: 2.3 • Epp, 3rd edition: 1.3 • Come to my office hours (ICCS X563)! • Wednesday 4-5pm, Thursday 2-3pm, and Friday 12-1pm 2

  3. Le Learn rning goals: : pre-cl class • By the start of this class you should be able to • Convert unsigned integers from decimal to binary and back. • Take two's complement of a binary integer. • Convert signed integers from decimal to binary and back. • Convert integers from binary to hexadecimal and back. • Add two binary integers. 3

  4. Le Learn rning goals: : in-cl class • By the end of this module, you should be able to: • Critique the choice of a digital representation scheme , including describing its strengths, weaknesses, and flaws (such as imprecise representation or overflow), for a given type of data and purpose, such as • fixed-width binary numbers using a two’s complement scheme for signed integer arithmetic in computers • hexadecimal for human inspection of raw binary data. 4

  5. CP CPSC SC 121: 121: the BI BIG ques questions ns: • We will make progress on two of them: • How does the computer (e.g. Dr. Racket) decide if the characters of your program represent a name, a number, or something else? How does it figure out if you have mismatched " " or ( )? • How can we build a computer that is able to execute a user-defined program? 5

  6. Mo Module 3 3 outline • Unsigned and signed binary integers. • Characters. • Real numbers. • Hexadecimal. 8

  7. Re Review question • What does it mean for a binary integer to be unsigned or signed ?

  8. Re Recall the 7-se segm gment disp splay y problem • Mapping unsigned integers between decimal and binary Number X1 X2 X3 X4 0 0 0 0 0 1 0 0 0 1 2 0 0 1 0 3 0 0 1 1 4 0 1 0 0 5 0 1 0 1 6 0 1 1 0 7 0 1 1 1 8 1 0 0 0 9 1 0 0 1 10

  9. Un Unsig igned ed bin inar ary à dec decimal • The binary value • Represents the decimal value • Examples: • 010101 = 2 4 + 2 2 + 2 0 = 16 + 4 + 1 = 21 • 110010 = 2 5 + 2 4 + 2 1 = 32 + 16 + 2 = 50 11

  10. De Decim cimal al à uns unsigned gned bi bina nary • Divide x by 2 and write down the remainder • The remainder is 0 if x is even, and 1 if x is odd. • Repeat this until the quotient is 0. • Write down the remainders from right (first one) to left (last one).

  11. De Decim cimal al à uns unsigned gned bi bina nary • Example: Convert 50 to a 8-bit binary integer • 50 / 2 = 25with remainder 0 • 25 / 2 = 12 with remainder 1 • 12 / 2 = 6 with remainder 0 • 6 / 2 = 3 with remainder 0 • 3 / 2 = 1 with remainder 1 • 1 / 2 = 0 with remainder 1 • Answer is 00110010.

  12. To To negate a (signed) binary integer The algorithm (also called taking two’s complement of the binary number): • Flip all bits: replace every 0 bit by a 1, and every 1 bit by a 0. • Add 1 to the result. Why does it make sense to negate a binary integer by taking its two’s complement? 14

  13. Addi Additive In Inver erse • Two numbers x and y are “additive inverses” of each other if they sum to 0. • Examples: 3 + (-3) = 0. (-7) + 7 = 0. 15

  14. Cl Clock k ari rithme metic • Pre-class quiz #3b: • It is currently 18:00, that is 6pm. Without using numbers larger than 24 in your calculations, what time will it be 22 * 7 hours from now? (Don’t multiply 22 by 7!) 16

  15. Cl Clock k ari rithme metic • 3:00 is 3 hour from midnight. • 21:00 is 3 hours to midnight. • 21 and 3 are additive inverses in clock arithmetic: 21 + 3 = 0. • Any two numbers across the clock are additive inverses of each other. • 21 is equivalent to -3 in any calculation.

  16. Cl Clock k ari rithme metic • Pre-class quiz #3b: • It is currently 18:00, that is 6pm. Without using numbers larger than 24 in your calculations, what time will it be 22 * 7 hours from now? (Don’t multiply 22 by 7!) a. 0:00 (midnight) b. 4:00 (4am) c. 8:00 (8am) d. 14:00 (2pm) e. None of the above 18

  17. Ne Negative n numbers i in cl clock ck a arithmetic • Put negative numbers across the clock from -3 0 the positive ones (where the additive inverses are). -6 • Since 21 + 3 = 0, we could put -3 where 21 -9 is. -12

  18. Bi Binary y clock k ari rithme metic • Suppose that we have 3 bits to work with. • If we are only representing positive values or zero: 000 111 001 0 7 1 2 6 110 010 5 3 4 101 011 100

  19. Binary Bi y clock k ari rithme metic • If we want to represent negative values... • We can put them across the clock from the positive ones (where the additive inverses are). 000 111 001 0 -1 1 2 -2 110 010 -3 3 -4 101 011 100

  20. Two’s Complement Tw Taking two’s complement of B = b 1 b 2 b 3 ...b n : Flip 1 1 1 ...1 the - b 1 b 2 b 3 ...b n bits ---------- x 1 x 2 x 3 ...x n Add + 1 one ---------- -B 22

  21. A Different Vi View of Tw Two’s Complement Taking two’s complement of B = b 1 b 2 b 3 ...b k : 1 1 1 ...1 Flip 1 1 1 ...1 the + 1 - b 1 b 2 b 3 ...b k bits ---------- ---------- 1 0 0 0 ...0 x 1 x 2 x 3 ...x k Add - b 1 b 2 b 3 ...b k + 1 one ---------- ---------- -B -B Equivalent to subtracting from 100...000 with k 0s. 23

  22. Tw Two’s Complement vs. Cr Crossi ssing the Cl Clock Two’s complement with k bits: 1 1 1 ...1 000 + 1 111 001 0 ---------- -1 1 1 0 0 0 ...0 2 -2 - b 1 b 2 b 3 ...b n 110 010 ---------- -3 3 -B -4 101 011 Equivalent to subtracting 100 from 100...000 with k 0s. 24

  23. Adv Advantages s of f two’s s compl plement • Which one(s) are the advantages of the two’s complement representation scheme? a. There is a unique representation of zero. b. It is easy to tell a negative integer from a non-negative integer. c. Basic operations are easy. For example, subtracting a number is equivalent to adding the two’s complement of the number. d. All of (a), (b), and (c). e. None of (a), (b), and (c).

  24. Adv Advantages s of f two’s s compl plement • Why did we choose to let 100 represent -4 rather than 4? 000 000 111 001 111 001 0 0 -1 -1 1 1 2 -2 2 -2 110 010 110 010 -3 3 -3 3 4 -4 101 011 101 011 100 100

  25. Wha What do does s two’s s compl plement mean? n? a. Taking two’s complement of a binary integer means flip all of the bits and then add 1. b. Taking two’s complement is a mathematical procedure to negate a binary integer. c. Two’s complement is a representation scheme we use to map signed binary integers to decimal integers. d. 2 of (a), (b), and (c) are true. e. All of (a), (b), and (c) are true.

  26. Ne Negative b binary à dec decimal • Example: convert the 6-bit signed binary integer 101110 to a decimal number. 1. Convert the binary integer directly to decimal (2 + 4 + 8 + 32 = 46) 2. Subtract 2^6 from it (2^6 because of 6-bit) (46 – 64 = -18). 3. Answer is -18. 29

  27. Ne Negative b binary à dec decimal • The signed binary value • represents the integer • Example: convert the 6-bit signed binary integer 101110 to a decimal number. • -2 5 + 2 3 + 2 2 + 2 1 = -32 + 8 + 4 + 2 = -18. • Answer is -18. 30

  28. Ne Negative d deci cimal à bi bina nary • Example: convert -18 to a 6-bit signed binary number. 1. Add 2^6 to it (2^6 because of 6-bit) (-18 + 2^6 = -18 + 64 = 46). 2. Convert the positive decimal integer directly to binary. 46 / 2 = 23 … 0, 23 / 2 = 12 ... 1, 12 / 2 = 6 ... 0, 6 / 2 = 3 ... 0, 3 / 2 = 1 ... 1, 1 / 2 = 0 ... 1. 3. Answer is 110010.

  29. Qu Questions to ponder: r: • With n bits, how many distinct values can we represent? • What are the smallest and largest n -bit unsigned binary integers? • What are the smallest and largest n -bit signed binary integers? 32

  30. Mo More questions to ponder: r: • Why are there more negative n -bit signed integers than positive ones? • How do we tell if an unsigned binary integer is: negative, positive, zero? • How do we tell if a signed binary integer is: negative, positive, zero? • How do we negate a signed binary integer? • On what value does this “negation” not behave like negation on integers? • How do we perform the subtraction x – y ? 33

  31. Mo Module 3 3 outline • Unsigned and signed binary integers. • Characters. • Real numbers. • Hexadecimal. 34

  32. How do computers represent ch charact cters? • It uses sequences of bits (like for everything else). • Integers have a “natural” representation of this kind. • There is no natural representation for characters. • So people created arbitrary mappings: • EBCDIC: earliest, now used only for IBM mainframes. • ASCII: American Standard Code for Information Interchange7-bit per character, sufficient for upper/lowercase, digits, punctuation and a few special characters. • UNICODE:16+ bits, extended ASCII for languages other than English 35

  33. Re Representing characters What does the 8-bit binary value 11111000 represent? a.-8 b.The character ø c.248 d.More than one of the above e.None of the above. Show Answer 36

  34. Mo Module 3 3 outline • Unsigned and signed binary integers. • Characters. • Real numbers. • Hexadecimal. 38

Recommend


More recommend