outline
play

Outline snack Prereqs, Learning Goals, and Quiz Notes Prelude: - PDF document

snick Outline snack Prereqs, Learning Goals, and Quiz Notes Prelude: Additive Inverse CPSC 121: Models of Computation Problems and Discussion 2016W2 Clock Arithmetic and Twos Complement Number Representation 1/3


  1. snick  Outline snack • Prereqs, Learning Goals, and Quiz Notes • Prelude: “Additive Inverse” CPSC 121: Models of Computation • Problems and Discussion 2016W2 – Clock Arithmetic and Two’s Complement Number Representation – 1/3 Scottish and Fractions in Binary – Programs and Numbers Steve Wolfman, based on notes by – Programs as Numbers Patrice Belleville and others • Next Lecture Notes 1 2 This work is licensed under a Creative Commons Attribution 3.0 Unported License. Learning Goals: Pre-Class Learning Goals: In-Class By the start of class, you should be able to: By the end of this unit, you should be able to: – Convert positive numbers from decimal to binary – Critique the choice of a digital representation scheme — including describing its strengths, and back. weaknesses, and flaws (such as imprecise – Convert positive numbers from hexadecimal to representation or overflow) — for a given type of binary and back. data and purpose, such as (1) fixed-width binary – Take the two’s complement of a binary number. numbers using a two’s complement scheme for – Convert signed (either positive or negative) signed integer arithmetic in computers or (2) numbers to binary and back. hexadecimal for human inspection of raw binary – Add binary numbers. data. 3 4 Where We Are in Motivating Problem The Big Stories Understand and avoid cases like those at: Theory Hardware Photo by Philippe Semanaz http://www.ima.umn.edu/~arnold/455.f96/disasters.html (CC by/sa) How do we model How do we build devices to computational systems? compute? Death of 28 people caused by failure of an anti-missile system, caused in turn by the misuse of one representation for fractions. Now : showing that our Now : enabling our Explosion of a $7 billion space vehicle caused by failure of the logical models can hardware to work with guidance system, caused in turn by misuse of a 16-bit signed binary data that’s more connect smoothly to value. models of number meaningful to systems. humans. (And once (Both representations are discussed in these slides.) we have numbers, we can represent pictures, words, sounds, 5 6 and everything else!) 1

  2. Outline Prelude: Unsigned Integers • Prereqs, Learning Goals, and Quiz Notes We can choose any arrangement of Ts • Prelude: “Additive Inverse” # p q r and Fs to represent 0 F F F • Problems and Discussion numbers... 1 F F T – Clock Arithmetic and Two’s Complement 2 F T F But, we might as well 3 F T T – 1/3 Scottish and Fractions in Binary choose something 4 T F F convenient. – Programs and Numbers 5 T F T If we let F correspond to 6 T T F – Programs as Numbers 7 T T T 0 and T to 1, then our • Next Lecture Notes representation is... 7 8 Prelude: Unsigned Integers Prelude: Additive Inverse ...base 2 numbers. The “additive inverse” of a number x is When we represent another number y such that x + y = 0. # p q r negative numbers, the 0 0 0 0 choice is also arbitrary, 1 0 0 1 but may as well be What is the additive inverse of 3? 2 0 1 0 convenient: 3 0 1 1 • Just one representation What is the additive inverse of -7? 4 1 0 0 for zero 5 1 0 1 • Easy to tell negative 6 1 1 0 from positive (or non- negative?) 7 1 1 1 • Basic operations easy. We want to be able to add signed binary numbers. We need x + -x to be 0. 9 10 But... What does it mean for basic operations to be “easy”? And, we want subtraction to be just addition “plus” negation. Outline Problem : Clock Arithmetic • Prereqs, Learning Goals, and Quiz Notes Problem: It’s 0500h. How many hours until • Prelude: “Additive Inverse” midnight? Give an • Problems and Discussion algorithm that – Clock Arithmetic and Two’s Complement requires a 24-hour clock, a level, and no – 1/3 Scottish and Fractions in Binary arithmetic. – Programs and Numbers – Programs as Numbers • Next Lecture Notes A level is a carpentry tool, essentially a straightedge 15 that indicates when it is either horizontal or vertical. 16 2

  3. Clock Arithmetic Clock Arithmetic Problem It’s 18 hundred. Without using numbers 0500 is five hours from midnight. 1900 is five hours to midnight. larger than 24 in your calculations, what time will it be 22*7 hours from now? ( Don’t multiply 22 by 7!) 5 and 19 are “additive inverses” in clock arithmetic: 5 + 19 = 0. a. 0 hundred (midnight) b. 4 hundred So are any other numbers that c. 8 hundred are “across the clock” from d. 14 hundred each other. e. None of these (Clock arithmetic is also known as 17 18 That’s even true for 12. Its additive inverse is itself! modular arithmetic in mathematics.) Clock Arithmetic: Unsigned Binary Clock Food for Thought If we wanted negative numbers on the clock, Here’s a 3 -bit unsigned 0 we’d probably put them 000 binary clock, -3 “across the clock” from 111 001 numbered from 0 the positives. (000) to 7 (111). -6 110 010 After all, if 3 + 21 is -9 already 0, why not put -3 -12 101 011 where 21 usually goes? 100 19 20 Two’s Complement Crossing the Clock Taking two’s complement of B = b 1 b 2 b 3 ...b n : To “cross the clock”, go 1 1 1 ...1 Flip 000 as many ticks left the - b 1 b 2 b 3 ...b n 111 001 from the top as you bits ---------- previously went right x 1 x 2 x 3 ...x n 110 010 from the top. Add + 1 one Here’s a clock labelled ---------- with 0 (000) to 3 (011) 101 011 -B and -1 (111) to -4 100 (100). 21 22 3

  4. Two’s Complement vs. A Different View of Two’s Complement Crossing the Clock Taking two’s complement of B = b 1 b 2 b 3 ...b n : Two’s complement with k “Crossing the clock” with k bits: bits: 1 1 1 ...1 1 1 1 ...1 1 1 1 ...1 Flip + 1 000 the - b 1 b 2 b 3 ...b n + 1 ---------- 111 001 bits ---------- 1 0 0 0 ...0 ---------- 110 010 - b 1 b 2 b 3 ...b n x 1 x 2 x 3 ...x n 1 0 0 0 ...0 Add 101 011 ---------- + 1 100 one -B - b 1 b 2 b 3 ...b n ---------- ---------- Equivalent to subtracting Equivalent to subtracting -B -B from 100...000 with k 0s. from 100...000 with k 0s. Two’s complement turns numbers into their Or... Just subtract from 100...0 23 24 “normal”, “cross -the- clock” additive inverses. Problem: Why Two’s Summary Questions (1 of 2) complement? • With n bits, how many distinct values can we represent? Why make the negation of 010 be 110? (In other words: why use the two’s complement scheme?) • What’s the smallest/largest n -bit unsigned binary integer? a. 010 + 110 already equals 0 (that is, 000). 000 b. 110 isn’t being used 111 001 • What’s the smallest/largest n - bit signed (using two’s complement) for any other purpose. binary integer? c. 110 is the easiest 110 010 negation to calculate. • Why the “extra” negative number? 101 011 • How many representations for each number do we have with unsigned/signed binary integers? 100 25 26 Summary Questions (2 of 2) Outline • • Prereqs, Learning Goals, and Quiz Notes How do we tell if an unsigned binary integer is: negative, positive, zero? • Prelude: “Additive Inverse” • How do we tell if a signed binary integer is: negative, positive, zero? • Problems and Discussion – Clock Arithmetic and Two’s Complement • How do we negate a signed binary integer? – 1/3 Scottish and Fractions in Binary • On what value does this “negation” not behave like negation on – Programs and Numbers integers? – Programs as Numbers • How do we perform the subtraction x – y? • Next Lecture Notes 27 28 4

  5. Problem : 1/3 Scottish Problem : 1/3 Scottish Problem : Can you be 1/3 Scottish? Problem : Can you be 1/3 Scottish? To build a model, we must clearly specify the problem. Many problems admit multiple models that lead to fundamentally different results. We’re going to use the model of parentage “endowing” 50% of each parent’s “ ish- ness”. That’s a coarse, even silly model. (By that model, none of us could possibly be at all Canadian, since humans did not originate in Canada.) Our model is handy for us, but it’s not necessarily what people’s identity is about! 29 30 Can you be one-third Scottish? Can you be one-third Scottish? Focus on Mom (and Mom’s Mom and so on). We’ll just make Dad “Scot” or “Not” as needed at each step. Mom: Dad: Mom: Dad: ?? ?? ?? ?? Mom: Dad: 2/3 Not And so on... Can you be one-third Scottish? Can you be one-third Scottish? Mom: Dad: 1/3 Scot Mom: Dad: Dad: Mom: ?? ?? 2/3 Not Mom: Dad: Mom: Dad: 1/3 Scot 1/3 Scot What’s happening here? Mom: Dad: Mom: Dad: 2/3 Not 2/3 Not 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend