Mod odule 3: Representing Values in a Com omputer Du Due Da - - PowerPoint PPT Presentation

mod odule 3 representing values in a com omputer du due
SMART_READER_LITE
LIVE PREVIEW

Mod odule 3: Representing Values in a Com omputer Du Due Da - - PowerPoint PPT Presentation

Mod odule 3: Representing Values in a Com omputer Du Due Da 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


slide-1
SLIDE 1

Mod

  • dule 3: Representing

Values in a Com

  • mputer
slide-2
SLIDE 2

2

Du Due Da 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

slide-3
SLIDE 3

3

Le Learn rning goals: : pre-cl 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.
slide-4
SLIDE 4

4

Le Learn rning goals: : in-cl 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.

slide-5
SLIDE 5

5

CP CPSC SC 121: 121: the BI BIG ques questions ns:

  • 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

  • ut if you have mismatched " " or ( )?
  • How can we build a computer that is able to

execute a user-defined program?

slide-6
SLIDE 6

8

Mo Module 3 3 outline

  • Unsigned and signed binary integers.
  • Characters.
  • Real numbers.
  • Hexadecimal.
slide-7
SLIDE 7

Re Review question

  • What does it mean for a binary integer to be

unsigned or signed?

slide-8
SLIDE 8

10

Re Recall the 7-se segm gment disp splay y problem

  • Mapping unsigned integers between decimal and

binary

Number X1 X2 X3 X4 1 1 2 1 3 1 1 4 1 5 1 1 6 1 1 7 1 1 1 8 1 9 1 1

slide-9
SLIDE 9

11

Un Unsig igned ed bin inar ary à dec 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
slide-10
SLIDE 10

De Decim cimal al à uns unsigned gned bi bina nary

  • 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).

slide-11
SLIDE 11

De Decim cimal al à uns unsigned gned bi bina nary

  • 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
  • 3 / 2 = 1 with remainder

1

  • 1 / 2 = 0 with remainder

1

  • Answer is 00110010.
slide-12
SLIDE 12

14

To To negate a (signed) binary integer

The algorithm (also called taking two’s complement

  • f 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?

slide-13
SLIDE 13

Addi Additive In Inver erse

  • Two numbers x and y are “additive inverses” of

each other if they sum to 0.

  • Examples: 3 + (-3) = 0. (-7) + 7 = 0.

15

slide-14
SLIDE 14

16

Cl Clock k ari rithme metic

  • 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!)

slide-15
SLIDE 15

Cl Clock k ari rithme metic

  • 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.

slide-16
SLIDE 16

18

Cl Clock k ari rithme metic

  • 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!)

  • a. 0:00 (midnight)
  • b. 4:00 (4am)
  • c. 8:00 (8am)
  • d. 14:00 (2pm)
  • e. None of the above
slide-17
SLIDE 17

Ne Negative n numbers i in cl clock ck a 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
slide-18
SLIDE 18

Bi Binary y clock k ari rithme metic

  • 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

1 2 3 4 5 6 7

slide-19
SLIDE 19

Bi Binary y clock k ari rithme metic

  • 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 2 3

  • 4
  • 3
  • 2
  • 1
slide-20
SLIDE 20

Tw Two’s Complement

Taking two’s complement of B = b1b2b3...bn: 1 1 1 ...1

  • b1b2b3...bn
  • x1x2x3...xn

+ 1

  • B

22

Flip the bits Add

  • ne
slide-21
SLIDE 21

A Different Vi View of Tw 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.

23

Flip the bits Add

  • ne

1 1 1 ...1 + 1

  • 1 0 0 0 ...0
  • b1b2b3...bk
  • B
slide-22
SLIDE 22

Tw Two’s Complement vs. Cr Crossi ssing the Cl Clock

Two’s complement with k bits: Equivalent to subtracting from 100...000 with k 0s.

24

1 1 1 ...1 + 1

  • 1 0 0 0 ...0
  • b1b2b3...bn
  • B

000 001 010 011 100 101 110 111

1 2 3

  • 4
  • 3
  • 2
  • 1
slide-23
SLIDE 23

Adv Advantages s of f two’s s compl plement

  • Which one(s) are the advantages of the two’s

complement representation scheme?

  • a. There is a unique representation of zero.
  • b. It is easy to tell a negative integer from

a non-negative integer.

  • c. Basic operations are easy. For example,

subtracting a number is equivalent to adding the two’s complement of the number.

  • d. All of (a), (b), and (c).
  • e. None of (a), (b), and (c).
slide-24
SLIDE 24

Adv Advantages s of f two’s s compl plement

  • Why did we choose to let 100 represent -4 rather

than 4?

000 001 010 011 100 101 110 111

1 2 3

  • 4
  • 3
  • 2
  • 1

000 001 010 011 100 101 110 111

1 2 3 4

  • 3
  • 2
  • 1
slide-25
SLIDE 25

Wha What do does s two’s s compl plement mean? n?

  • a. Taking two’s complement of a binary integer

means flip all of the bits and then add 1.

  • b. Taking two’s complement is a mathematical

procedure to negate a binary integer.

  • c. Two’s complement is a representation scheme

we use to map signed binary integers to decimal integers.

  • d. 2 of (a), (b), and (c) are true.
  • e. All of (a), (b), and (c) are true.
slide-26
SLIDE 26

29

Ne Negative b binary à dec decimal

  • Example: convert the 6-bit signed binary integer

101110 to a decimal number.

  • 1. Convert the binary integer directly to decimal

(2 + 4 + 8 + 32 = 46)

  • 2. Subtract 2^6 from it (2^6 because of 6-bit)

(46 – 64 = -18).

  • 3. Answer is -18.
slide-27
SLIDE 27

30

Ne Negative b binary à dec 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.
slide-28
SLIDE 28

Ne Negative d deci cimal à bi bina nary

  • Example: convert -18 to a 6-bit signed binary

number.

  • 1. Add 2^6 to it (2^6 because of 6-bit)

(-18 + 2^6 = -18 + 64 = 46).

  • 2. 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.

  • 3. Answer is 110010.
slide-29
SLIDE 29

32

Qu Questions to ponder: r:

  • 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?

slide-30
SLIDE 30

33

Mo More questions to ponder: r:

  • 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?
slide-31
SLIDE 31

34

Mo Module 3 3 outline

  • Unsigned and signed binary integers.
  • Characters.
  • Real numbers.
  • Hexadecimal.
slide-32
SLIDE 32

35

How do computers represent ch charact cters?

  • 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

slide-33
SLIDE 33

36

Re Representing characters

What does the 8-bit binary value 11111000 represent? a.-8 b.The character c.248 d.More than one of the above e.None of the above.

ø

Show Answer

slide-34
SLIDE 34

38

Mo Module 3 3 outline

  • Unsigned and signed binary integers.
  • Characters.
  • Real numbers.
  • Hexadecimal.
slide-35
SLIDE 35

39

Ca Can so some meone be 1/ 1/3r 3rd Sc Scottish sh?

  • 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.

slide-36
SLIDE 36

40

Ca Can so some meone be 1/ 1/3r 3rd Sc Scottish sh?

  • 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.

slide-37
SLIDE 37

41

Ca Can so some meone be 1/ 1/3r 3rd Sc Scottish sh?

While debated, Scotland is traditionally said to be founded in 843AD, approximately 45 generations ago. Your mix of Scottish, will therefore be n/2

45; using 2

45/3 (rounded to

the nearest integer) as the numerator gives us 11728124029611/2

45 which give us

approximately 0.333333333333342807236476801 which is no more than 1/10

13 th away from

1/3.

slide-38
SLIDE 38

42

Ca Can so some meone be 1/ 1/3r 3rd Sc Scottish sh?

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.

slide-39
SLIDE 39

43

Ca Can so some meone be 1/ 1/3r 3rd Sc Scottish sh?

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.

slide-40
SLIDE 40

Ca Can so some meone be 1/ 1/3rd

rd Sc

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%
  • f an ancestry.
slide-41
SLIDE 41

45

Ca Can so some meone be 1/ 1/3rd

rd Sc

Scottish?

  • Suppose we start with people who are either 0%
  • r 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?
slide-42
SLIDE 42

46

Re Representing real numbers in binary

Which of the following values have a finite binary representation? a.1/4 b.1/6 c.1/9 d.More than one of the above. e.None of the above.

slide-43
SLIDE 43

Fr Frac actio tions ns in in bas base e 10 and and bas base e 2

  • 5/8 = 0.625 (in base 10) = 0.101 (in base 2)
  • Can be represented exactly in both.
  • 1/3 = 0.333…(in base 10) = 0.0101...(in base 2)
  • Can be represented exactly in neither.
  • Could you come up with a number that can be

represented exactly in base 10 but not in base 2?

  • Could you come up with a number that can be

represented exactly in base 2 but not in base 10?

slide-44
SLIDE 44

48

Ho How w do does es co computer represent va values

  • f
  • f t

the f for

  • rm xxx.

xxx.yyyy?

Java uses scientific notation 1724 = 1.724 x 10

3

But in binary, instead of decimal. 1724 = 1.1010111100 x 2

1010

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

slide-45
SLIDE 45

49

Flo Floating ting po poin int t comput putatio tions ns

  • 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.

slide-46
SLIDE 46

50

Flo Floating ting po poin int t comput putatio tions ns

  • Predict the output of:

(* #i0.1 0.1 0.1 10 10 10)

  • a. 1
  • b. Not 1, but a value close to 1.
  • c. Not 1, but a value far from 1.
  • d. I don’t know.

slide-47
SLIDE 47

51

Flo Floating ting po poin int t comput putatio tions ns

Consider the following: What value will (addfractions 0) return? a. 10 b. 11 c. Less than 10 d. More than 11 e. No value will be printed

(define (addfractions x) (if (= x 1.0) (+ 1 (addfractions (+ x #i0.1)))))

slide-48
SLIDE 48

52

Mo Module 3 3 outline

Summary Unsigned and signed binary integers. Characters. Real numbers. Hexadecimal.

slide-49
SLIDE 49

53

Mo Module 3. 3.4: 4: Hexadecima mal

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.

slide-50
SLIDE 50

54

Mo Module 3. 3.4: 4: Hexadecima mal

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

Arithmetic operation Addition %ebx %ecx

slide-51
SLIDE 51

55

Mo Module 3. 3.4: 4: Hexadecima mal

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

Arithmetic operation Addition %ebx %ecx

We can not tell what operation this is.

slide-52
SLIDE 52

56

Mo Module 3. 3.4: 4: Hexadecima mal

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

Red Green Blue