Download presentation
Presentation is loading. Please wait.
1
CPSC 121: Models of Computation 2016W2
Number Representation Steve Wolfman, based on notes by Patrice Belleville and others TODO (future terms): correct term This work is licensed under a Creative Commons Attribution 3.0 Unported License.
2
Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: “Additive Inverse” Problems and Discussion Clock Arithmetic and Two’s Complement 1/3 Scottish and Fractions in Binary Programs and Numbers Programs as Numbers Next Lecture Notes
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. Discuss point of learning goals.
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. Congratulate them on learning the core of the material for # rep and pointed out that we’ll be doing the (exciting and important) icing: comparing and contrasting representations to decide which ones to choose. Discuss point of learning goals.
5
Where We Are in The Big Stories
Theory Hardware How do we model computational systems? Now: showing that our logical models can connect smoothly to models of number systems. How do we build devices to compute? Now: enabling our hardware to work with data that’s more meaningful to humans. (And once we have numbers, we can represent pictures, words, sounds, and everything else!)
6
Photo by Philippe Semanaz (CC by/sa)
Motivating Problem Understand and avoid cases like those at: Death of 28 people caused by failure of an anti-missile system, caused in turn by the misuse of one representation for fractions. Explosion of a $7 billion space vehicle caused by failure of the guidance system, caused in turn by misuse of a 16-bit signed binary value. (Both representations are discussed in these slides.) Photo by Philippe Semanaz (CC by/sa)
7
Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: “Additive Inverse” Problems and Discussion Clock Arithmetic and Two’s Complement 1/3 Scottish and Fractions in Binary Programs and Numbers Programs as Numbers Next Lecture Notes
8
Prelude: Unsigned Integers
We can choose any 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... # p q r F 1 T 2 3 4 5 6 7
9
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. # p q r 1 2 3 4 5 6 7 But... What does it mean for basic operations to be “easy”?
10
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? We want to be able to add signed binary numbers. We need x + -x to be 0. And, we want subtraction to be just addition “plus” negation.
11
Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: “Additive Inverse” Problems and Discussion Clock Arithmetic and Two’s Complement 1/3 Scottish and Fractions in Binary Programs and Numbers Programs as Numbers Next Lecture Notes This section introduces modular arithmetic as “clock arithmetic”. It REGULARLY comes in handy to be able to answer a question/explain a problem by just saying “that’s clock arithmetic!” Of course, introducing modular arithmetic would accomplish the same thing, but I thought this would be more comfortable as a first step... And this way we don’t need any formal notation discussions or mentions of “groups” and such.
12
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. [2011W1] From the quiz, “identifying the chapter" and its dangers: "I would convert to binary numbers and then.." Point: to firmly entrench the idea of “crossing the clock”. How is it done: level the level with the right side aligned to 5. The left is aligned to 19, which is 5’s additive inverse. What’s ? 0! Works for ANY number, even for 12.. as long as we’ll accept a degenerate case. (12 is 12’s additive inverse.) Fun modern world note: at least some of your students probably HAVE levels: on their iPhones. (2009) A level is a carpentry tool, essentially a straightedge that indicates when it is either horizontal or vertical.
13
Clock Arithmetic 0500 is five hours from midnight is five hours to midnight. 5 and 19 are “additive inverses” in clock arithmetic: = 0. So are any other numbers that are “across the clock” from each other. That’s even true for 12. Its additive inverse is itself!
14
Clock Arithmetic Problem
It’s 18 hundred. 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!) 0 hundred (midnight) 4 hundred 8 hundred 14 hundred None of these Emphasize: adding 22 is the same as subtracting 2 because 22+2 = 0; so, in “clock arithmetic”, 22 = -2. Answer is b. (Clock arithmetic is also known as modular arithmetic in mathematics.)
15
Clock Arithmetic: Food for Thought
If we wanted negative numbers on the clock, we’d probably put them “across the clock” from the positives. After all, if is already 0, why not put -3 where 21 usually goes? -3 -6 -9 -12
16
Unsigned Binary Clock Here’s a 3-bit unsigned binary clock, numbered from 0 (000) to 7 (111). 000 111 001 110 010 101 011 100
17
Crossing the Clock To “cross the clock”, go as many ticks left from the top as you previously went right from the top. Here’s a clock labelled with 0 (000) to 3 (011) and -1 (111) to -4 (100). 000 111 001 110 010 A good crowd question: what should we do with 100? Clearly 4 or -4, but which one? Why -4? How about: so one of our “easy properties” continues to hold: we can tell positives and negatives apart just by looking at the first bit. (Because every number that starts with 1 is negative.) Note: of course, #s starting with 0 are NON-negative, not necessarily positive, but we can’t really get away from that. EMPHASIZE (worth another slide?) that this means addition of negative numbers JUST WORKS. That’s because adding something like 7 is ALREADY the same as subtracting 1. So, computers don’t need special circuits to add signed numbers. (Indeed, your computer probably doesn’t even know when it does addition whether the numbers being added are signed or unsigned! (Same assembly language instruction, though overflow flags can probably be checked for the separate cases ex post facto.)) 101 011 100
18
Two’s Complement Taking two’s complement of B = b1b2b3...bn: b1b2b3...bn x1x2x3...xn + 1 -B Flip the bits Add one
19
A Different View of Two’s Complement
Taking two’s complement of B = b1b2b3...bn: b1b2b3...bn x1x2x3...xn + 1 -B Flip the bits - b1b2b3...bn -B Add one Or... Just subtract from
20
Two’s Complement vs. Crossing the Clock
Two’s complement with k bits: Equivalent to subtracting from with k 0s. “Crossing the clock” with k bits: Equivalent to subtracting from with k 0s. - b1b2b3...bn -B 000 111 001 110 010 101 011 100 Two’s complement turns numbers into their “normal”, “cross-the-clock” additive inverses.
21
Problem: Why Two’s complement?
Why make the negation of 010 be 110? (In other words: why use the two’s complement scheme?) already equals 0 (that is, 000). 110 isn’t being used for any other purpose. 110 is the easiest negation to calculate. 000 111 001 110 010 2013W2: removing two of the options to make this easier. [2011W1] Not editing this prior to class; goal is to check that students follow the reason we select two’s complement as our numbering scheme. See how it goes and report post-mortem? THIS COULD USE EDITING. See lecture post-mortem notes. Comments on: c. Taking the two’s complement is a pain in the butt. We could, for example use ones’ complement, in which case taking the negation would JUST be flipping the bits. That’s clearly easier, but it’s not as good. Why not? d. Sure 110 is being used! If we weren’t using it for negative values, we’d have more positive values! The bad news is that we can ONLY represent 2n values with n bits, no matter what. So, if we want negatives, we must sacrifice positives (or zero)! e. That’s certainly true and states WHAT two’s complement is. But WHY two’s complement? That leaves a and b, both of which are great answers... But let’s REALLY think like clock arithmeticians. We have a 3 bit clock available to us. What are the only eight values on that clock? OK, so, is 1000 a value on that clock? If not, what DOES equal, even BEFORE we throw in negative numbers? Don’t believe me? Try Java code like: int i = ; // That’s 2^30, or ¼ as much as fits in a 32 bit number. System.out.println(i+i+i+i); // Prints 0. Why? 101 011 100
22
Summary Questions (1 of 2)
With n bits, how many distinct values can we represent? What’s the smallest/largest n-bit unsigned binary integer? What’s the smallest/largest n-bit signed (using two’s complement) binary integer? Why the “extra” negative number? How many representations for each number do we have with unsigned/signed binary integers?
23
Summary Questions (2 of 2)
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?
24
Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: “Additive Inverse” Problems and Discussion Clock Arithmetic and Two’s Complement 1/3 Scottish and Fractions in Binary Programs and Numbers Programs as Numbers Next Lecture Notes
25
Problem: 1/3 Scottish Problem: Can you be 1/3 Scottish?
Solicit responses. Good funny ones: “How many chromosomes do we have again?” “No, I cannot since I’m 100% Chinese.” (nearly identical results 2011W1 and previously (2009W1?)) [2011W1] (SO YOUR MOTHER IS SCOTTISH-AMERICAN AND FATHER IS SCOTTISH-IRISH). THAT WAY, YOU COULD BE SCOTTISH-IRISH-AMERICAN? CRUCIAL to get across, both to respect people’s ethnic/political identities and to make a point: We are MODEL-BUILDERS. To build a model, we MUST clearly specify the problem. Many problems admit multiple models that lead to different answers. If we don’t think about that up front, our model will be worthless. We’re going to use the model of parentage “endowing” 50% of each parent’s “ish-ness”. That’s a terribly coarse, even silly model. (By that model, I’m (Steve) not Canadian at all because my great-grandparents and some of my grandparents weren’t born in Canada.) It’s handy for us, but it’s not necessarily what ethnic identity is about! From 2010W2: “One needs to make many (wrong) assumptions in order to even approach this problem. First we must identify what "Scottish" means. Are we dealing with nationality, ethnic identity, some sort of odd idea about genetics (Scottish "blood"), or some jumbled folk sense of "Scottish" that jumbles the above three ideas? ... However, this is a crazy assumption because how can we say a chromosome is 100% or some other percentage some ethic identity?<br/>As a person of colour, I suggest changing this question in the future as it might be offensive to some minorities (who growing up in a predominately "White" country have to deal with conflicting identities and sometimes the guilt of wanting to be more "White").”
26
Problem: 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! Solicit responses. Good funny ones: “How many chromosomes do we have again?” “No, I can’t. I’m 100% Chinese.” CRUCIAL to get across, both to respect people’s ethnic/political identities and to make a point: We are MODEL-BUILDERS. To build a model, we MUST clearly specify the problem. Many problems admit multiple models that lead to different answers. If we don’t think about that up front, our model will be worthless. We’re going to use the model of parentage “endowing” 50% of each parent’s “ish-ness”. That’s a terribly coarse, even silly model. (By that model, I’m (Steve) not Canadian at all because my great-grandparents and some of my grandparents weren’t born in Canada.) It’s handy for us, but it’s not necessarily what ethnic identity is about! From 2010W2: “One needs to make many (wrong) assumptions in order to even approach this problem. First we must identify what "Scottish" means. Are we dealing with nationality, ethnic identity, some sort of odd idea about genetics (Scottish "blood"), or some jumbled folk sense of "Scottish" that jumbles the above three ideas? ... However, this is a crazy assumption because how can we say a chromosome is 100% or some other percentage some ethic identity?<br/>As a person of colour, I suggest changing this question in the future as it might be offensive to some minorities (who growing up in a predominately "White" country have to deal with conflicting identities and sometimes the guilt of wanting to be more "White").”
27
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: ?? Dad needs to be “Not” because if he was “Scot”, I’d be at least ½ Scottish, and that’s already too much.
28
Can you be one-third Scottish?
Mom: ?? Dad: ?? This time, Dad (grandpa) needs to be Scot, because if he’s not, I can’t be more than ½ Scottish. Mom takes up the slack: 2*(2/3 – ½) = 2*(1/6) = 1/3. Mom: 2/3 Dad: Not
29
Can you be one-third Scottish?
Mom: ?? Dad: ?? Mom: 1/3 Dad: Scot If someone notices the pattern, call attention to it here, else... Mom: 2/3 Dad: Not
30
Can you be one-third Scottish?
And so on... Can you be one-third Scottish? Mom: 1/3 Dad: Scot Mom: 2/3 Dad: Not Mom: 1/3 Dad: Scot What’s happening here? Repeats! Mom: 2/3 Dad: Not
31
Now, focus on Dad... We can represent fractions in binary by making “Scottish family trees”: Mom: 1/3 Dad: Scot Mom: 2/3 Dad: Not Mom: 1/3 Dad: Scot Be sure to emphasize the comparison with a decimal number. In decimal, would be 0/10ths + 1/100th + 0/1000ths + 1/10,000th. In binary, it’s 0/2 + ¼ + 0/8 + 1/ That approaches 1/3. (Those terms are 5/16, just a smidge to small to be 1/3.) Mom: 2/3 Dad: Not
32
0 . 0 1 1 Here’s 0.375 in binary... Mom: 0.0 Dad: Scot Mom: 0.5
Mom: 0.5 Dad: Scot For any number < 1, we can determine the “binary point” representation of that number by checking how we would make a family tree that Scottish. Furthermore, if you can be “1/x” Scottish (with a finite number of ancestors), then you can represent “1/x” as a “binary point” number with a finite number of bits. Mom: 0.75 Dad: Not
33
“Fixed-Point” Binary Values
… 2-1 or “one-halfs” place 2-3 or “one-eights” place 2-2 or “one-quarters” place 2-4 or “one-sixteenths” place 2-6 or “one-sixty-fourths” place 2-5 or “one-thirty-seconds” place Both Java and Racket use the IEEE floating point standard for their inexact approximations of real numbers. Racket tries to very flexibly move between representations. Java does not, at least not with primitives! Many languages use “floating point” numbers (scientific notation in base 2, essentially) to represent “real numbers”: Java float and double, Racket inexact.
34
“Fixed-Point” Binary Values
35
Problem: Base 10 vs. Base 2 Can we represent 1/9 with a finite number of digits in base 10 or base 2? Yes (in both). Yes in base 10, but not in base 2. Yes in base 2, but not in base 10. No (in both). I don’t know. [2011W1] Let’s try this with a couple of examples first. NOTES from old version (asking which in base 10 but not base 2 of 1/9, 1/8, 1/7, 1/6, and none of these): EMPHASIZE: the lesson is NOT that computers can’t represent 1/3. They absolutely can, just not with this choice of representation scheme. Note: we can do a “reduction” proof that 1/9 and 1/6 can’t be represented in binary (and base 10) based on our knowledge that 1/3 can’t be. Consider: if 1/6 COULD be represented with a finite # of bits, we could just shift the “binary point” by one location right to multiply by 2 and get 1/3 with a finite # of bits. But, that’s impossible (contradiction); so, we must not be able to represent 1/6 w/a finite # of bits. Similarly, 1/3 = 1/9 + 1/9 + 1/9 (or 2*1/9 + 1/9). Also, can ask “can you be [1/7] Scottish?” Point out how we tell whether a fraction terminates in base 10: can the denominator be made into a power of 10 (by multiplying it by an integer)? Same is true for base 2: is the denominator a power of 2 (nothing but a power of 2 can be made into a power of 2 by multiplying it by an integer!).
36
Problem: Base 10 vs. Base 2 Can we represent 1/8 with a finite number of digits in base 10 or base 2? Yes (in both). Yes in base 10, but not in base 2. Yes in base 2, but not in base 10. No (in both). I don’t know. [2011W1] Let’s try this with a couple of examples first.
37
Problem: Base 10 vs. Base 2 Can we represent 1/5 with a finite number of digits in base 10 or base 2? Yes (in both). Yes in base 10, but not in base 2. Yes in base 2, but not in base 10. No (in both). I don’t know. [2011W1] Let’s try this with a couple of examples first.
38
What’s Representable? In base 10: Anything that, multiplied by some power of 10, becomes an integer can be represented with a finite number of digits with our fixed-point scheme. In base 2: The same except multiplying by a power of 2. Answer: because every power of 10 is also a power of 2 multiplied by a power of 5 or, to put it another way, if we need to multiply by 2^k to get an integer, we can multiply by 10^k instead and get the same integer times 5^k (which will still be an integer). So why does it seem that base 10 can represent with a finite number of digits anything that base 2 can?
39
So... Computers Can’t Represent 1/3?
No! Using a different representation scheme (e.g., a rational number with a separate integer numerator and denominator), computers can perfectly represent 1/3! (You know that from Racket!) The point is: Representations that use a finite number of bits (all of them) have weaknesses. Know those weaknesses and their impact on your computations!
40
Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: “Additive Inverse” Problems and Discussion Clock Arithmetic and Two’s Complement 1/3 Scottish and Fractions in Binary Programs and Numbers Programs as Numbers Next Lecture Notes
41
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( ); // 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)); } [2011W1] Did these early! We used to cover this MUCH earlier in the course. Some students may have seen it in the “extra” slides. Fun little program. HAVE IT LOADED on your computer!
42
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( ); // 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)); } Sum of s is NOT 1.0 with Java’s double-precision floats. Of course, the lower loop wraps around surprisingly quickly. Again, HAVE THIS ON YOUR COMPUTER. BUT, make sure they COMMIT to predictions (preferably with discussion) before you run it. See clicker Q on next slide.
43
Number Representation Prediction
// Let's add up 10 dimes. System.out.println("10 dimes gives us:"); System.out.println( ); // 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); What will this print? First 1.0 and then nothing because it runs forever. First 1.0 and then some other value, because it won’t run forever. First something OTHER than 1.0 and then nothing because it runs forever. First something OTHER than 1.0 and then some other value, because it won’t forever. None of these This could use some reformatting so it’s clearer and easier to read. Yuck! Still, it commits them to a prediction! You may want to change the questions to be more “surface” questions like: What will this print? First 1.0 and then nothing because it runs forever. First 1.0 and then some other value, because it won’t run forever. First something OTHER than 1.0 and then nothing because it runs forever. First something OTHER than 1.0 and then some other value, because it won’t run forever. None of these. Sum of s is NOT 1.0 with Java’s double-precision floats. Of course, the lower loop wraps around surprisingly quickly.
44
But Racket solves this… right?
Racket’s number representation scheme is flexible and powerful (but mostly not as “efficient” or “compact” as Java’s, which usually doesn’t matter!). But no representation is perfect, and every representation could be more flexible. ;; What do these evaluate to? ( ) (* (sqrt 2) (sqrt 2))
45
Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: “Additive Inverse” Problems and Discussion Clock Arithmetic and Two’s Complement 1/3 Scottish and Fractions in Binary Programs and Numbers Programs as Numbers Next Lecture Notes
46
Preface: Java Byte Code
Programs in the Java programming language are compiled to a language called “byte code” that is then interpreted on a particular computer. Why? Byte code is hard for humans to write, read, and understand... but it’s easy to write a program that reads and executes it (compared to writing a program to directly read and execute Java source code). So, if you create a new type of computer tomorrow, and I want to run Java code on it, I don’t have to write a program that works on your computer and knows how to execute Java; I just need to write a program that knows how to execute byte code. Java byte code is also designed to be compact so it’s cheap to transmit across the internet.
47
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 The opcode for istore_1 is Here’s a typical “hex” view of ~1/5th of the previous program’s byte code. Where is i = 100? a b This REALLY IS the bytcode for the previous program (well 1/5th of it). We can get the answer by converting various decimal values to hex and then just looking for them. So, 100 = 64 in hex 16 = 10 in hex 60 = 3c in hex A has all of these and is, in fact, the correct answer. (Though it could be just coincidence that it has them, it’s NOT.) I used emacs’s hexl-find-file to open the bytecode. I used jclasslib’s bytecode viewer to decompile the byte code. I used the Java VM reference at and to look up the bytecode values. Much of the remainder of the code is actually string constants for the various printlns, in which 0x64 appears repeatedly for the letter ‘d’. b d c e: None of these.
48
Problem: Binary 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? Because we would have to translate all the opcodes and values to binary. Because many bytecode files would have no binary representation. Because the binary representation of the file would be much longer. Because data like (100 in base 2) might not show up as the sequence of numbers It wouldn’t be much more difficult. IMPORTANT to proceed to the next example appropriately: On this one, conclude that the reason binary would be more difficult is because it’s long, but otherwise it would be fine. BUT, what’s really happening is not that binary is a good way to represent the underlying hexadecimal but that hexadecimal is a handy way for humans to work with the underlying binary. For reasons we’ve already discussed, the computer IS going to use binary as its representation. So, for example, 100 WILL be represented as the 8-bit value We don’t have much choice about that.
49
Problem: Decimal 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? Because we would have to translate all the opcodes and values to decimal. Because many bytecode files would have no decimal representation. Because the decimal representation of the file would be much longer. Because data like 100 might not show up as the sequence of numbers It wouldn’t be much more difficult. [2011W1] The big misconception last year (2010W2) was that (b) is correct. So, maybe open a file and show it in hexadecimal and/or binary to point out it really is just a HUGE number (though we usually think of it as made up of its byte-level parts.. Which is why hex is handy!). Emacs’s hexl-mode works nicely. So, having established that we use binary underneath and hex is a handy way to work with it; why not use decimal instead? Let them struggle; the answer is d, which they’re unlikely to settle on. So: c. Not really much longer. About 20% longer (not much). (The ratio is the log base 10 of 16.) a and b are as irrelevant as they were before. I argue for d, which makes e wrong. So, why is d right? Show this IN A CALCULATOR (Microsoft’s scientific calculator accessible by running “calc” and using some menu options to get to “programmer mode” (as of Windows 7) works nicely): Put in a MUCH simpler “binary file” in hexadecimal 10643C. Convert to decimal, saying “will the 100 be in there”? And, it isn’t. DON’T FORGET TO SAVE THAT NUMBER! (Write on board?) Now, say OK it’s worse, what if we add 1 to the The hex changes from 64 to 65 as we expect. What happens to the decimal? It changes by some weird amount. [Show the amount in the calculator. Find the difference.] Aha! 256! Why 256? Because it’s 2^8. What if this were in the middle of a file? It would change by some GIGANTIC power of 2, very hard to understand in decimal (and might change MANY MANY digits of the number).
50
Outline Prereqs, Learning Goals, and Quiz Notes
Prelude: “Additive Inverse” Problems and Discussion Clock Arithmetic and Two’s Complement 1/3 Scottish and Fractions in Binary Programs and Numbers Programs as Numbers Next Lecture Notes
51
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. Discuss point of learning goals.
52
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. Discuss point of learning goals.
53
Next Lecture Prerequisites
Read Section 1.3 (Epp 3rd ed) or 2.3 (Epp 4th ed). Complete the open-book, untimed quiz on Connect that is due before next class. TODO (future terms): when we inevitably either dump Connect or it rebrands, fix the reference! Solve problems like Exercise Set 1.3, #1, 3, 4, 6-32, Of these, we’re especially concerned about problems like and 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.
54
(on your own if you have time, not required)
Some Things to Try... (on your own if you have time, not required)
55
Representing Characters
note: will come back to “hex American Standard Code or Information Interchange
56
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? Easy to say 2^4 – 1 with a 1g, 2g, 4g, and 8g weight, but that’s wrong, you can do more. Why? You have THREE choices of what to do with each weight: leave it off the scales, put it opposite the item you’re weighing, or put it WITH the item you’re weighing. Now, you can do 3^4-1: 1g, 3g, 9g, 27g. So, for example, to get: 0: nothing 1: 1 opp 2: 1 same, 3 opp 3: 3 opp 4: 1 opp, 3 opp 5: 1 same, 3 same, 5 opp Etc. Can you see how this forms a trinary encoding? ?g ?g ?g ?g
57
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
58
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?
59
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 positive number, greater than 70. 70. A positive number, less than 70. 0. A negative number. 70. Clock arithmetic.
60
Problem: Number Rep Breakdown
Problem: Explain what’s happening in each of these…
61
Program for Introductions: Testing
Java version with 100: 9900 Java version with : Java version with : Reminder: this is to calculate n*(n-1), the number of introductions for a group of size n. Talk about parallels to CPSC 111
62
Program for Introductions: Testing
Racket version with 100: 9900 Racket version with : Racket version with : Reminder: this is to calculate (* n (- n 1)), the number of introductions for a group of size n.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.