cpsc 121 models of computation
play

CPSC 121: Models of Computation Convert unsigned integers from - PowerPoint PPT Presentation

Pre-Class Learning Goals By the start of this class you should be able to CPSC 121: Models of Computation Convert unsigned integers from decimal to binary and back. Take two's complement of a binary integer. Unit 3 Convert signed


  1. Pre-Class Learning Goals  By the start of this class you should be able to CPSC 121: Models of Computation  Convert unsigned integers from decimal to binary and back.  Take two's complement of a binary integer. Unit 3  Convert signed integers from decimal to binary and back. Representing Numbers and Other Values  Convert integers from binary to hexadecimal and back. in a Computer  Add two binary integers. Based on slides by Patrice Belleville and Steve Wolfman Unit 3: Representing Values 2 Quiz 3 Feedback In-Class Learning Goals  Overall: By the end of this unit, you should be able to:  Issues:  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  Clock Arithmetic and Can you be 1/3rd Scottish? scheme for signed integer arithmetic in computers  We will get back to these questions soon.  hexadecimal for human inspection of raw binary data. Unit 3: Representing Values Unit 3: Representing Values 3 4 1

  2. Motivating Problem Where We Are in The Big Stories Theory: Hardware:  Understand and avoid cases like those at: Photo by Philippe Semanaz  How do we model  How do we build devices to (CC by/sa) http://www.ima.umn.edu/~arnold/455.f96/disasters.html computational systems? compute?  Death of 28 people caused by failure of an anti-missile system, caused in turn by the misuse of one Now: Now: representation for fractions.  Showing that our logical  Enabling our hardware to  Explosion of a $7 billion space vehicle caused by failure models can connect work with numbers. smoothly to models of  And once we have of the guidance system, caused in turn by misuse of a number systems. numbers, we can represent 16-bit signed binary value. pictures, words, sounds, and everything else!  (Both representations are discussed in these slides.) Unit 3: Representing Values Unit 3: Representing Values 5 6 Outline Representing Numbers  We can choose any arrangement of Ts and Fs to represent numbers...  Unsigned and signed binary integers.  If we use F -> 0 and T->1, the representation is a true  Characters. base 2 numbers:  Real numbers. Number V1 V2 V3 V4 Number b 3 b 2 b 1 b 0 0 F F F F 0 0 0 0 0 1 F F F T 1 0 0 0 1 2 F F T F 2 0 0 1 0 3 F F T T 3 0 0 1 1 4 F T F F 4 0 1 0 0 5 F T F T 5 0 1 0 1 6 F T T F 6 0 1 1 0 7 F T T T 7 0 1 1 1 8 T F F F 8 1 0 0 0 9 T F F T 9 1 0 0 1 Unit 3: Representing Values Unit 3: Representing Values 7 8 2

  3. Representing Unsigned Integers Number Systems  A number can be written using any base.  The binary value  What can you say about the following numbers? b b ... b b b 100001 2   n 1 n 2 2 1 0  represents the integer 33 10   n 1 n 2 2 1 1020 3 b 2 + b 2 + ... + b 2 + b 2 + b   n 1 n 2 2 1 0 21 16 or written differently A. First number is different than the rest  B. Second number is different than the rest i b 2 i C. Third number is different than the rest where each b i is either 0 or 1 D. All are the same number E. All are different numbers Unit 3: Representing Values Unit 3: Representing Values 9 10 Clock Arithmetic Clock Arithmetic  Problem: It’s 05:00h. How  05:00 is five hours from midnight. many hours until midnight?  19:00 is five hours to midnight.  5 and 19 are “additive inverses” in  Give an algorithm that clock arithmetic: 5 + 19 = 0. requires a 24-hour clock, a level, and no arithmetic.  So. what is the additive inverse of 15?  How did you find it?  How else can you write 15? That’s even true for 12. Its additive inverse is itself! Unit 3: Representing Values Unit 3: Representing Values 11 12 3

  4. Clock Arithmetic Open-Ended Quiz # Question  Suppose it is 15:00 (3:00PM, that is). If we wanted negative What time was it 8* 21 hours ago? numbers on the clock,  Don’t multiply 21 by 8! 0 we’d probably put them  Must not use numbers larger than 24 in -3 “across the clock” from your calculation' the positives. A. 21:00 -6 B. 4:00 After all, if 3 + 21 is C. 8:00 already 0, why not put -3 -9 D. 15:00 where 21 usually goes? -12 E. None of these Unit 3: Representing Values Unit 3: Representing Values 13 14 Open-Ended Quiz # Question Unsigned Binary Clock  Here’s a 3 -bit  Suppose it is 15:00 (3:00PM, that is). What time will it be 13 * 23 hours unsigned binary from now? clock, numbered from 000  Don’t multiply 13 by 23! 0 (000) to 7 (111). 111 001 A. 13:00 110 010 B. 2:00 C. 22:00 101 011 D. 15:00 100 E. None of these Unit 3: Representing Values Unit 3: Representing Values 15 16 4

  5. Two’s complement for Negative Integers Crossing the Clock  To “cross the clock”,  In a 3-bit signed numbers: go as many ticks left  2 3 is the same as 000 000 from the top as you  the negation of a number x is the number 2 3 – x. (1000) previously went right -1 001  Why does this make sense? (111) from the top.  For 3-bit integers, what is 111 + 001? -2 010  Here’s a clock labelled A. 110 (110) with 0 (000) to 3 (011) B. 111 -3 011 and -1 (111) to -4 C. 1000 (101) -4 (100). D. 000 (100) E. Error: we can not add these two values. Unit 3: Representing Values Unit 3: Representing Values 17 18 Two's Complement for n-Bit Integers Exercice  Unfolding the  Also note that clock into a line:  What is 10110110 in decimal, assuming it's a signed  first bit determines the sign 10…00 -2 n-1 8-bit binary integer? o 0-positive, 1- negative A. 182  -x has the same binary n  B. -74 -2 2 x 11…01 representation as -1 C. 74 11… 11  And 0 00… 00 D. -182   1 1 00…01    n n x = x + 2 2 1 E. None of the above 00…10 2 Add 1 Flip bits from 0 to 1 and from 1 to 0 2 n-1 -1 01…11 Unit 3: Representing Values Unit 3: Representing Values 19 20 5

  6. Converting Decimal to Binary Converting Binary to Decimal  How do we convert a positive decimal integer n to  Theorem: for n-bit signed integers, the binary value b b ... b b b binary?   n 1 n 2 2 1 0  The last bit is 0 if n is even, and 1 if n is odd. represents the integer     To find the remaining bits, we divide n by 2, ignore n 1 n 2 2 1 b 2 + b 2 + ... + b 2 + b 2 + b   n 1 n 2 2 1 0 the remainder, and repeat.  What do we do if n is negative? or written differently    n 1 i b 2 + b 2  n 1 i  Examples:  Examples: Unit 3: Representing Values Unit 3: Representing Values 21 22 Summary Questions More Summary Questions  With n bits, how many distinct values can we represent?  How do we tell quickly if a unsigned binary integer is negative, positive, or zero?  What are the smallest and largest n-bit unsigned binary  How do we tell quickly if a signed binary integer is negative, integers? positive, or zero?  How do we negate a signed binary integer?  What are the smallest and largest n-bit signed binary integers?  There is one signed n-bit binary integer that we should not try to negate.  Which one?  Why are there more negative n-bit signed integers than positive ones?  What do we get if we try negating it? Unit 3: Representing Values Unit 3: Representing Values 23 24 6

  7. Outline Representing Characters  How do computers represent characters?  Unsigned and signed binary integers.  They use sequences of bits (like for everything else).  Characters.  Integers have a “natural” representation of this kind.  There is no natural representation for characters.  Real numbers.  So people created arbitrary mappings: o EBCDIC: earliest, now used only for IBM mainframes. o ASCII: American Standard Code for Information Interchange • 7-bit per character, sufficient for upper/lowercase, digits, punctuation and a few special characters. o UNICODE: • 16+ bits, extended ASCII for languages other than English Unit 3: Representing Values Unit 3: Representing Values 25 26 Representing Characters Outline  Unsigned and signed binary integers.  What may the binary value 11111000 represent?  Characters. A. -8 B. The character ø  Real numbers. C. 248 D. More than one of the above E. None of the above. Unit 3: Representing Values Unit 3: Representing Values 27 28 7

Recommend


More recommend