Module 3: Representing Values in a Computer 9/10/2018 Module 3: Representing Values in a Computer 8 * 21 = 8 * -3 = - 24 = 0 13 * 23 = 13 * -1 = - 13 = 11 1 1
Due Dates Assignment #1 is due Thursday January 19th at 4pm 9/10/2018 Due 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 2 2
Learning goals: pre-class 9/10/2018 Learning goals: pre-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 3 3
Learning goals: in-class 9/10/2018 Learning goals: in-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 4 4
CPSC 121: the BIG questions: 9/10/2018 CPSC 121: the BIG questions: 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 5 5
Module 3 outline Unsigned and signed binary integers. Characters. 9/10/2018 Module 3 outline Unsigned and signed binary integers. Characters. Real numbers. Hexadecimal. 8 8 8
Review question What does it mean for a binary integer to be unsigned or signed?
Recall the 7-segment display problem 9/10/2018 Recall the 7-segment display problem Mapping unsigned integers between decimal and binary Number X1 X2 X3 X4 1 2 3 4 5 6 7 8 9 10 10 10
Unsigned binary decimal 9/10/2018 Unsigned binary decimal The binary value Represents the decimal value Examples: 010101 = 24 + 22 + 20 = 16 + 4 + 1 = 21 110010 = 25 + 24 + 21 = 32 + 16 + 2 = 50 11 11 11
Decimal unsigned binary 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).
Decimal unsigned binary 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.
To negate a (signed) binary integer 9/10/2018 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 14 14
Additive Inverse Two numbers x and y are “additive inverses” of each other if they sum to 0. Examples: 3 + (-3) = 0. (-7) + 7 = 0.
Clock arithmetic Pre-class quiz #3b: 9/10/2018 Clock arithmetic 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!) To avoid using a calculator, we want the numbers to be small. So we can eyeball it and figure out the product. With time, we operate in base 24. It is exactly the same time every 24 hours. - 8 * 21 = - 8 * -3 = 24 = 0 13 * 23 = 13 * -1 = - 13 = 11 -21 is the same as +3, which is exactly what we did with 2's complement (but in base 24 instead of 2^n). 16 16 16
Clock arithmetic 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.
Clock arithmetic Pre-class quiz #3b: 9/10/2018 Clock arithmetic 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!) 0:00 (midnight) 4:00 (4am) 8:00 (8am) 14:00 (2pm) None of the above To avoid using a calculator, we want the numbers to be small. So we can eyeball it and figure out the product. With time, we operate in base 24. It is exactly the same time every 24 hours. - 8 * 21 = - 8 * -3 = 24 = 0 13 * 23 = 13 * -1 = - 13 = 11 -21 is the same as +3, which is exactly what we did with 2's complement (but in base 24 instead of 2^n). 18 18 18
Negative numbers in clock arithmetic Put negative numbers across the clock from the positive ones (where the additive inverses are). Since 21 + 3 = 0, we could put -3 where 21 is. -3 -6 -9 -12
Binary clock arithmetic Suppose that we have 3 bits to work with. If we are only representing positive values or zero: 000 001 010 011 100 101 110 111 7 1 6 2 5 3 4
Binary clock arithmetic If we want to represent negative values... We can put them across the clock from the positive ones (where the additive inverses are). 000 001 010 011 100 101 110 111 -1 1 -2 2 -3 3 -4
Two’s Complement Taking two’s complement of B = b1b2b3...bn: 1 1 1 ...1 - b1b2b3...bn ---------- x1x2x3...xn + 1 -B Flip the bits Add one
A Different View of Two’s Complement Taking two’s complement of B = b1b2b3...bk: 1 1 1 ...1 - b1b2b3...bk ---------- x1x2x3...xk + 1 -B Equivalent to subtracting from 100...000 with k 0s. Flip the bits 1 1 1 ...1 + 1 ---------- 1 0 0 0 ...0 - b1b2b3...bk -B Add one
Two’s Complement vs. Crossing the Clock Two’s complement with k bits: Equivalent to subtracting from 100...000 with k 0s. 1 1 1 ...1 + 1 ---------- 1 0 0 0 ...0 - b1b2b3...bn -B 000 001 010 011 100 101 110 111 -1 1 -2 2 -3 3 -4
Advantages of two’s complement Which one(s) are the advantages of the two’s complement representation scheme? There is a unique representation of zero. It is easy to tell a negative integer from a non-negative integer. Basic operations are easy. For example, subtracting a number is equivalent to adding the two’s complement of the number. All of (a), (b), and (c). None of (a), (b), and (c).
Advantages of two’s complement Why did we choose to let 100 represent -4 rather than 4? 000 001 010 011 100 101 110 111 000 001 010 011 100 101 110 111 -1 1 -1 1 -2 2 -2 2 -3 3 -3 3 -4 4
What does two’s complement mean? Taking two’s complement of a binary integer means flip all of the bits and then add 1. Taking two’s complement is a mathematical procedure to negate a binary integer. Two’s complement is a representation scheme we use to map signed binary integers to decimal integers. 2 of (a), (b), and (c) are true. All of (a), (b), and (c) are true.
Negative binary decimal 9/10/2018 Negative binary decimal Example: convert the 6-bit signed binary integer 101110 to a decimal number. Convert the binary integer directly to decimal (2 + 4 + 8 + 32 = 46) Subtract 2^6 from it (2^6 because of 6-bit) (46 – 64 = -18). Answer is -18. Revise questions to eliminate alternative interpretations. Happy to clarify the question before you have to answer. We are meant to work through these problems together, not meant to be a real test. Please ask questions! I am often happy to have you try again. Don’t stress too much about the clicker grade. You will have a few marks of grace in the end. 3% in total. 2% simply by participation. 1% for correctness. We’ll do about 60 clicker questions in the term. So each question counts towards 0.02%. Interactivity of the lectures. If people ask questions, I’d rather stop and answer questions than moving on. It is really important to address your questions as soon as possible. We are behind. We are at about the same place as the other sections. Actually this is much more synchronized than when I taught this the first time. So we are not really behind. Also, we set our own schedule and decide how much materials we need to cover. Some of you were worried that we were behind according to the pre-class quiz schedule, but we set the schedule ourselves. Technically we could have made pre-class quiz 4 due on Wednesday instead of on Monday. So it’s our choice and we’ll adapt the schedule to our actual progress. Can’t hear questions and answers clearly. I will repeat the questions and answers as much as I can. Also, a great feedback is that sometimes I tend to rely on the students’ answers without actually doing it myself. That’s a great point. I will try to give my official answer in addition to the students’ feedback. A lot of you are asking me to go through more exercises and harder problems in class. I understand that you are worried about exams. Unfortunately, we are extremely limited time in lectures. So you have to do most of the practice on your own. I would say. Take advantage of the time in tutorial. Work on more practice problems. You can come to my office hour and I can help through some exercises. If you can find the time to practice, I can find the time to help you. Important to practice. Once you work on some problems yourself, you’ll realize that understanding a solution doesn’t mean you can reproduce the solution yourself. I will try to briefly review textbook readings in class. 29 29 29
Negative binary decimal 9/10/2018 Negative binary decimal The signed binary value represents the integer Example: convert the 6-bit signed binary integer 101110 to a decimal number. -25 + 23 + 22 + 21 = -32 + 8 + 4 + 2 = -18. Answer is -18. If b_{n-1} = 0: trivial If b_{n-1} = 1: write 2^n-x = b_{n-1}2^{n-1} + b_{n-2}2^{n-2} + ... + b_12 + b_0 then subtract 2^n from both sides 110_2 n = 3 b_2 = 1, b_1 = 1, b_0 = 0 b_2 * 2^2 + b_1 * 2^1 + b_0 * 2^0 = - 1 * 2^2 + 1 * 2^1 + 0 * 2^0 = -4 + 2 + 0 = -2 30 30 30
Negative decimal binary Example: convert -18 to a 6-bit signed binary number. Add 2^6 to it (2^6 because of 6-bit) (-18 + 2^6 = -18 + 64 = 46). 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. Answer is 110010.
9/10/2018 Questions to ponder: 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? N bits, can represent 2^n integers. Unsigned binary integers: Smallest: 0 Largest: 2^n – 1 Signed binary integers: Smallest: - 2^(n-1) Largest: 2^(n-1) – 1 32 32 32
More questions to ponder: 9/10/2018 More questions to ponder: 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? Why are there more negative n-bit signed integers than positive ones? Because 0 starts with a leading bit of 0. How do we tell quickly if a signed binary integer is negative, positive or zero? Starts with 1, it’s negative. Starts with 0, it’s zero if all bits are zero. Otherwise, it is positive. There is one signed n-bit binary integer that we should not try to negate. Which one? What do we get if we try negating it? 100. if we think of it as representing -4, negating it gives us -4 again (instead of +4). 33 33 33
Module 3 outline Unsigned and signed binary integers. Characters. 9/10/2018 Module 3 outline Unsigned and signed binary integers. Characters. Real numbers. Hexadecimal. 34 34 34
How do computers represent characters? 9/10/2018 How do computers represent characters? 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 35 35
Representing characters 9/10/2018 Representing characters What does the 8-bit binary value 11111000 represent? -8 The character 248 More than one of the above None of the above. ø Show Answer 36 36 36
Module 3 outline Unsigned and signed binary integers. Characters. 9/10/2018 Module 3 outline Unsigned and signed binary integers. Characters. Real numbers. Hexadecimal. 38 38 38
Can someone be 1/3rd Scottish? 9/10/2018 Can someone be 1/3rd Scottish? No, but what if I once wore a kilt? Not normally, since every person's genetic code is derived from two parents, branching out in halves going back. However, someone with a chromosome abnormality that gives them three chromosomes (trisomy), a person might be said to be one-third/two-thirds of a particular genetic marker. 39 39 39
Can someone be 1/3rd Scottish? 9/10/2018 Can someone be 1/3rd Scottish? I have come to the conclusion that no, you cannot be one- third Scottish. I will provide my reasoning with the following two examples. Say there are two progenitors, P and Q. P is Korean and Q is American. If P and Q have a child, PQ, he will be 1/2 Korean and 1/2 American. Now say that PQ grows up to the legal age (no pedophillia in here) and enters a romantic relationship with another progenitor, R, who is Scottish. PQ and R have a child, PQR, and he will be 1/2 Scottish, 1/4 Korean, and 1/4 American. The second example, we can say PQ conceives a child, PQUT, with someone named UT, who is 1/2 Ethiopian and 1/2 African. PQUT would be 1/4 of each ethnicity. We can see that you will only be (1/2)^n - where n is a nonnegative integer - of an ethnicity (n in this context means which generation it is). Notice that the denominator will always be a multiple of 2. Therefore, you can never be 1/3 of any ethnicity. 40 40 40
Can someone be 1/3rd Scottish? 9/10/2018 Can someone be 1/3rd Scottish? While debated, Scotland is traditionally said to be founded in 843AD, approximately 45 generations ago. Your mix of Scottish, will therefore be n/245; using 245/3 (rounded to the nearest integer) as the numerator gives us 11728124029611/245 which give us approximately 0.333333333333342807236476801 which is no more than 1/1013 th away from 1/3. 41 41 41
Can someone be 1/3rd Scottish? 9/10/2018 Can someone be 1/3rd Scottish? If we assume that two Scots have a child, and that child has a child with a non-Scot, and this continues in the right proportions, then eventually their Scottishness will approach 1/3: This is of course discounting the crazy citizenship laws we have these days, and the effect of wearing a kilt on one's heritage. 42 42 42
Can someone be 1/3rd Scottish? 9/10/2018 Can someone be 1/3rd Scottish? In a mathematical sense, you can create 1/3 using infinite sums of inverse powers of 2 1/2 isn't very close 1/4 isn't either 3/8 is getting there... 5/16 is yet closer, so is 11/32, 21/64, 43/128 etc 85/256 is 0.33203125, which is much closer, but which also implies eight generations of very careful romance amongst your elders. 5461/16384 is 0.33331298828125, which is still getting there, but this needs fourteen generations and a heck of a lot of Scots and non-Scots. 43 43 43
Can someone be 1/3rd Scottish? To answer this question, we need to make some assumptions. What does being Scottish mean? Nationality? Ethic identity? How does the Scottish-ness of a parent influence the Scottish-ness of a child? Our model of ancestry: Each parent gives you 50% of an ancestry.
Can someone be 1/3rd Scottish? 9/10/2018 Can someone be 1/3rd Scottish? Suppose we start with people who are either 0% or 100% Scottish. After 1 generation, how Scottish can a child be? After 2 generations, how Scottish can a grand- child be? What about 3 generations? What about n generations? After 1 generation: 0, 1/2, 1 Integer multiples of 1/2^1 After 2 generations: 0, 1/4, 2/4, 3/4, 1 Integer multiples of 1/2^2 After 3 generations: 0, 1/8, 2/8, 3/8, …, 1 Integer multiples of 1/2^3 What about n generations? Integer multiples of 1/2^n Many real/fractional numbers, like 1/3, do not have finite binary representations, and thus cannot be represented exactly in binary. 45 45 45
Fractions in base 10 and base 2 5/8 = 0.625 (in base 10) = 5 * 1/23 = 0.101 (in base 2) 1/3 = 0.3333333333….........(in base 10) = 0.25 + 0.0625 + 0.15625 + … = 1/22 + 1/24 + 1/26 + ... = 0.010101.....(in base 2)
Representing real numbers in binary 9/10/2018 Representing real numbers in binary Which of the following values have a finite binary expansion? 1/4 1/6 1/9 More than one of the above. None of the above. Repeatedly double the number to be converted, record if the result is at least 1, and then throw away the integer part. Double the number. Write 1 if the result is at least 1. Otherwise, write 0. Throw away the integer part. Repeat. 5/32 0 5/32 * 2 = 10/32 0 10/32 * 2 = 20/32 0 20/32 * 2 = 40/32 = 1 + 8/32 1 8/32 * 2 = 16/32 0 16/32 * 2 = 32/32 = 1 + 0 1 5/32 = 0.00101 ============================== 1/3 0 1/3 * 2 = 2/3 0 2/3 * 2 = 4/3 = 1 + 1/3 1 … 1/3 = 0.0101010101010 =========================== 1/4 0 1/4 * 2 = 2/4 0 2/4 * 2 = 1 = 1 + 0 1 1/4 = 0.01 1/5 0 1/5 * 2 = 2/5 0 2/5 * 2 = 4/5 0 4/5 * 2 = 8/5 = 1 + 3/5 1 3/5 * 2 = 6/5 = 1 + 1/5 1 1/5 = 0.00110011 ▷ 47 47 47
Representing real numbers in binary 9/10/2018 Representing real numbers in binary Which fractions have a finite binary expansion? In decimal: 1/8 = 0.125 1/10 = 0.1 In binary: 1/8 = 1/10 = 1/3 = 0.010101010101010101010101010101010101... 1/8 = 0.001 1/10=0.0001100110011001100110011001100110011... 49 49 49
How does computer represent values of the form xxx.yyyy? 9/10/2018 How does computer represent values of the form xxx.yyyy? Java uses scientific notation 1724 = 1.724 x 103 But in binary, instead of decimal. 1724 = 1.1010111100 x 21010 Only the mantissa and exponent need to be stored. The mantissa has a fixed number of bits (24 for float, 53 for double). Scheme/Racket uses this for inexact numbers. mantissa exponent 50 50 50
Floating point computations 9/10/2018 Floating point computations Computations involving floating point numbers are imprecise. The computer does not store 1/3, but a number that's very close to 1/3. The more computations we perform, the further away from the “real” value we are. Floating point numbers are not stored exactly. We store a number that is very close to the real number. For each number, we make a small error. As we perform more and more computations, these small errors accumulate and become noticeable. Example: If numbers are stored exactly, then the output should be 1. In this case, what do you think the output will be? 51 51 51
Floating point computations 9/10/2018 Floating point computations Predict the output of: (* #i0.01 0.01 0.01 100 100 100) 1 Not 1, but a value close to 1. Not 1, but a value far from 1. I don’t know. What does this function do? Start with x = 0. We will repeatedly add 0.1 to x. If x = 1.0, we will return the number of times we have added 0.1 to x. ▷ 52 52 52
Floating point computations 9/10/2018 Floating point computations Consider the following: What value will (addfractions 0) return? 10 d) More than 11 11 e) No value will be printed Less than 10 (define (addfractions x) (if (= x 1.0) (+ 1 (addfractions (+ x #i0.1))))) What does this function do? Start with x = 0. We will repeatedly add 0.1 to x. If x = 1.0, we will return the number of times we have added 0.1 to x. ▷ 53 53 53
Module 3 outline Summary Unsigned and signed binary integers. 9/10/2018 Module 3 outline Summary Unsigned and signed binary integers. Characters. Real numbers. Hexadecimal. 55 55 55
Module 3.4: Hexadecimal As you learned in CPSC 110, a program can be 9/10/2018 Module 3.4: Hexadecimal As you learned in CPSC 110, a program can be Interpreted: another program is reading your code and performing the operations indicated. Example: Scheme/Racket Compiled: the program is translated into machine language. Then the machine language version is executed directly by the computer. 56 56 56
9/10/2018 Module 3.4: Hexadecimal What does a machine language instruction look like? It is a sequence of bits! Y86 example: adding two values. In human-readable form: addl %ebx, %ecx. In binary: 0110000000110001 Here is a machine language instruction to add two values. Take values from registers ebx and ecx, add them, and store the result in register ecx. %ecx %ebx Addition 57 Arithmetic operation 57 57
9/10/2018 Module 3.4: Hexadecimal Long sequences of bits are painful to read and write, and it's easy to make mistakes. Should we write this in decimal instead? Decimal version: 24625. Problem: Solution: use hexadecimal 6031 Is there a shorter and more concise way to represent this? Nice thing about hexadecimal: every 4 bits is one hexadecimal number. We can not tell what operation this is. %ecx %ebx Addition 58 Arithmetic operation 58 58
Red leaning towards purple. 9/10/2018 Module 3.4: Hexadecimal Another example: Suppose we make the text in a web page use color 15728778. What color is this? Red leaning towards purple. Written in hexadecimal: F00084 Another example: what color does 15728708 represent? Red Green Blue 59 59 59