Presentation is loading. Please wait.

Presentation is loading. Please wait.

Snick  snack 1 CPSC 121: Models of Computation 2008/9 Winter Term 2 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others.

Similar presentations


Presentation on theme: "Snick  snack 1 CPSC 121: Models of Computation 2008/9 Winter Term 2 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others."— Presentation transcript:

1 snick  snack 1 CPSC 121: Models of Computation 2008/9 Winter Term 2 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

2 Lecture Prerequisites Read Section 1.5. Solve problems like Exercise Set 1.5, #1-16, 27-36, and 41-46. Complete the open-book, untimed quiz on WebCT that was due before class.

3 Learning Goals: Pre-Class By the start of class, you should be able to: –Convert positive numbers from decimal to binary and back. –Convert positive numbers from hexadecimal to binary and back. –Take the two’s complement of a binary number. –Convert signed (either positive or negative) numbers to binary and back. –Add binary numbers.

4 Learning Goals: In-Class By the end of this unit, 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 (1) fixed-width binary numbers using a two’s complement scheme for signed integer arithmetic in computers or (2) hexadecimal for human inspection of raw binary data.

5 Prelude: Unsigned Integers We can choose any arbitrary arrangement of Ts and Fs to represent numbers... But, we might as well choose something convenient. If we let F correspond to 0 and T to 1, then our representation is... 5 #pqr 0FFF 1FFT 2FTF 3FTT 4TFF 5TFT 6TTF 7TTT

6 Prelude: Unsigned Integers...base 2 numbers. When we represent negative numbers, the choice is also arbitrary, but may as well be convenient: Just one representation for zero Easy to tell negative from positive (or non- negative?) Basic operations easy... 6 #pqr 0000 1001 2010 3011 4100 5101 6110 7111

7 Prelude: Additive Inverse The “additive inverse” of a number x is another number y such that x + y = 0. What is the additive inverse of 3? What is the additive inverse of -7? 7 We want to be able to add signed binary numbers. We need x + -x to be 0. And, we want addition to be easy to implement.

8 Problem: Clock Arithmetic Problem: It’s 0500h. How many hours until midnight? Give an algorithm that requires a 24-hour clock, a level, and no arithmetic. 8 A level is a carpentry tool, essentially a straightedge that indicates when it is either horizontal or vertical.

9 Concept Q: Clock Arithmetic It’s 1830. Without using any numbers larger than 24 in your calculations, what time will it be 22*7 hours from now? (In other words, don’t multiply 22 by 7!) a.Got it. Easy to calculate. b.Got it. Took some notes and figuring. c.Don’t see how to get this without some arithmetic on larger numbers.

10 Clock Arithmetic: Food for Thought 0 -3 -6 -9 -12 000 001 010 011 100 101 110 111 If 111 is the additive inverse of 001 anyway, why not make 111 be -001?

11 Aside: Two’s Complement vs. Crossing the Clock To take the two’s complement of a k-bit binary number: –Invert every bit –Add 1 to the number That’s the same as: –Subtract from k 1s: 11...111 –Add 1 to the number Which is the same as: subtract from 1 followed by k 0s: 100...000 To “cross the clock” from a number, subtract the number from the number of “ticks” on the clock (e.g., 24 for a 24- hour clock). With k bits, we can represent 2 k numbers (so, we have 2 k “ticks”). 2 k is 100...000 in binary, with k+1 bits. In other words, a 1 followed by k 0s. 11 So, why two’s complement? Because it turns numbers into their “normal” additive inverses.

12 Problem: 1/3 Scottish Problem: Can you be 1/3 Scottish? 12

13 Concept Q: 1/3 Scottish Which of the following numbers can be precisely represented with a finite number of digits/bits using a “decimal point”-style representation in base 10 but not base 2? a.1/9 b.1/8 c.1/7 d.1/6 e.None of these. 13

14 Problem: Number Rep Breakdown Problem: Explain what’s happening in each of these… 14

15 What Doesn’t Work is NOT Always Obvious (1 of 2) Class Main { public static void main(String[] args) { // Let's add up 4 quarters. System.out.println("4 quarters gives us:"); System.out.println(0.25 + 0.25 + 0.25 + 0.25); // Let's do something a hundred times. int i = 100; do { // Make i one smaller. i--; } while (i > 0); System.out.println("Done!"); System.out.println("i ended up with the value: " + i); System.out.println("It went down by: " + (100 - i)); }

16 What Doesn’t Work is NOT Always Obvious (2 of 2) Class Main { public static void main(String[] args) { // Let's add up 10 dimes. System.out.println("10 dimes gives us:"); System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1); // Let's try do something a hundred times.. // but accidentally go forever int i = 100; do { // Make i one LARGER. Oops! i++; } while (i > 0); System.out.println("Done!"); System.out.println("i ended up with the value: " + i); System.out.println("It went down by: " + (100 - i)); }

17 Problem: Java Byte Code Problem: When compiled to bytecode, i = 100 might be “push 100; store in variable 1”. The “opcode” for bipush (push a byte) is 16 10. The opcode for istore_1 is 60 10. Here’s a typical “hex” view of ~1/5 th of the previous program’s byte code. Where is i = 100 ? 17

18 Concept Q: Java Byte Code Why would the same task (finding a particular snippet of code in a bytecode file) be much more difficult if the file were represented in binary? a.Because we would have to translate all the opcodes and values to binary. b.Because many bytecode files would have no binary representation. c.Because the binary representation of the file would be much longer. d.Because data like 1100100 (100 in base 2) might not show up as the sequence of numbers 1 1 0 0 1 0 0. e.It wouldn’t be much more difficult. 18

19 Concept Q: Java Byte Code Why would the same task (finding a particular snippet of code in a bytecode file) be much more difficult if the file were represented in decimal? a.Because we would have to translate all the opcodes and values to decimal. b.Because many bytecode files would have no decimal representation. c.Because the decimal representation of the file would be much longer. d.Because data like 100 might not show up as the sequence of numbers 1 0 0. e.It wouldn’t be much more difficult. 19

20 Next Lecture Learning Goals: Pre-Class By the start of class, you should be able to: –Use truth tables to establish or refute the validity of a rule of inference. –Given a rule of inference and propositional logic statements that correspond to the rule’s premises, apply the rule to infer a new statement implied by the original statements.

21 Next Lecture Prerequisites Read Section 1.3. Solve problems like Exercise Set 1.3, #1, 3, 4, 6-32, 36-44. Of these, we’re especially concerned about problems like 12-13 and 39- 44. Many of these problems go beyond the pre-class learning goals into the in-class goals, but they’re the tightest fit in the text. Complete the open-book, untimed quiz on WebCT that is due before next class.

22 snick  snack More problems to solve... (on your own or if we have time)

23 Problem: Weighty Numbers Problem: You have a balance scale and four weights. You may choose the mass of the weights, as long as they’re in whole units of grams. What’s the largest number n such that you can exactly measure every weight 0…n ? 23 ?g

24 24 Problem: Representing Data Problem: Devise two different ways to represent each of the following with bits: black-and-white images text the shape of your face

25 25 Representing Characters

26 Problem: 256-hour Clock Arithmetic Problem: Imagine you’ve built a computer that uses 256-hour clock faces, each with a single dial, as storage units. How would you store, add, subtract, and negate integers? 26

27 Concept Q: 256-hour Clock Arithmetic Java has a type called “byte” that is an 8-bit signed integer. What will the following code print? byte b = 70; b = b + 64; a.A positive number, greater than 70. b.70. c.A positive number, less than 70. d.0. e.A negative number.

28 Problem: Number Rep Breakdown Problem: Explain what’s happening in each of these… 28

29 Program for Introductions: Testing Java version with 100: 9900 Java version with 8675309: 265642364 Java version with 1526097757: -645820308 Reminder: this is to calculate n*(n-1), the number of introductions for a group of size n.

30 Program for Introductions: Testing Haskell version with 100: 9900 Haskell version with 8675309: 75260977570172 Haskell version with 1526097757: 2328974362394333292 Reminder: this is to calculate n*(n-1), the number of introductions for a group of size n.


Download ppt "Snick  snack 1 CPSC 121: Models of Computation 2008/9 Winter Term 2 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others."

Similar presentations


Ads by Google