CPSC 121: Models of Computation Unit 3: Representing Values in a - - PowerPoint PPT Presentation

cpsc 121 models of computation
SMART_READER_LITE
LIVE PREVIEW

CPSC 121: Models of Computation Unit 3: Representing Values in a - - PowerPoint PPT Presentation

CPSC 121: Models of Computation Unit 3: Representing Values in a Computer CPSC 121 2011W T2 Unit 3: Representing Values th online quiz is due Sunday, January 22 nd The 4 at 19:00. Assigned reading for the quiz: Epp, 4 th edition:


slide-1
SLIDE 1

CPSC 121 – 2011W T2

CPSC 121: Models of Computation

Unit 3: Representing Values in a Computer

slide-2
SLIDE 2

CPSC 121 – 2011W T2

Unit 3: Representing Values

The 4

th online quiz is due Sunday, January 22 nd

at 19:00.

 Assigned reading for the quiz:

 Epp, 4 th edition: 2.3  Epp, 3 rd edition: 1.3  Rosen, 6 th edition: 1.5 up to the bottom of page 69.  Rosen, 7 th edition: 1.6 up to the bottom of page 75.

slide-3
SLIDE 3

CPSC 121 – 2011W T2

Unit 3: Representing Values

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

CPSC 121 – 2011W T2

Unit 3: Representing Values

Quiz 3 feedback:

 Well done overall.  Can one be 1/3rd Scottish?  We will get back to this question later.  I don't have any Scottish ancestors.  So we will ask if one can be 1/3

Belgian instead (which would you prefer: a bagpipe and a kilt, or belgian chocolate?)

(c) 1979, Dargaud ed. et Albert Uderzo (c) ITV/Rex Features

slide-5
SLIDE 5

CPSC 121 – 2011W T2

Unit 1: Propositional Logic

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

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,

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

slide-6
SLIDE 6

CPSC 121 – 2011W T2

Unit 3: Representing Values

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

 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-7
SLIDE 7

CPSC 121 – 2011W T2

Summary

 Unsigned and signed binary integers.  Characters.  Real numbers.

slide-8
SLIDE 8

CPSC 121 – 2011W T2

Unit 3: Representing Values

Notice the similarities:

Number Value 1 Value 2 Value 3 Value 4 F F F F 1 F F F T 2 F F T F 3 F F T T 4 F T F F 5 F T F T 6 F T T F 7 F T T T 8 T F F F 9 T F F T Number b3 b2 b1 b0 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

CPSC 121 – 2011W T2

Unit 3: Representing Values

Unsigned integers review: the binary value represents the integer

  • r written differently

∑i=0

n−1

bi2

i

bn−12

n−1bn−22 n−2...b2 2 2b12 1b0

bn−1bn−2...b2b1b0

slide-10
SLIDE 10

CPSC 121 – 2011W T2

Unit 3: Representing Values

To negate a (signed) integer:

 Replace every 0 bit by a 1, and every 1 bit by a 0.  Add 1 to the result.

Why does this make sense?

 For 3-bit integers, what is 111 + 1?

a) 110 b) 111 c) 1000 d) 000 e) Error: we can not add these two values.

slide-11
SLIDE 11

CPSC 121 – 2011W T2

Unit 3: Representing Values

Implications for binary representation:

 There is a noticeable pattern if you start at 0 and

repeatedly add 1.

 We can extend the pattern towards the negative

integers as well.

Also note that

 -x has the same binary representation as 

2

n−x

2

n−x=2 n−1−x1

Flip bits from 0 to 1 and from 1 to 0 Add 1

slide-12
SLIDE 12

CPSC 121 – 2011W T2

Unit 3: Representing Values

An open-ended question we weren't able to put on quiz #3:

 Imagine the time is currently 15:00

(3:00PM, that is). How can you quickly answer the following two questions without using a calculator:

 What time was it 8 * 21 hours ago?  What time will it be 13 * 23 hours

from now?

slide-13
SLIDE 13

CPSC 121 – 2011W T2

Unit 3: Representing Values

Exercice:

 What is 10110110 in decimal, assuming it's a

signed 8-bit binary integer?

How do we convert a positive decimal integer n to binary?

 The last bit is 0 if n is even, and 1 if n is odd.  To find the remaining bits, we divide n by 2, ignore

the remainder, and repeat.

What do we do if n is negative?

slide-14
SLIDE 14

CPSC 121 – 2011W T2

Unit 3: Representing Values

Theorem: for signed integers: the binary value represents the integer

  • r written differently

Proof:

−bn−12

n−1∑i=0 n−2

bi2

i

−bn−12

n−1bn−22 n−2...b22 2b12 1b0

bn−1bn−2...b2b1b0

slide-15
SLIDE 15

CPSC 121 – 2011W T2

Unit 3: Representing Values

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?

slide-16
SLIDE 16

CPSC 121 – 2011W T2

Unit 3: Representing Values

More questions to ponder:

 Why are there more negative n-bit signed integers

than positive ones?

 How do we tell quickly if a signed binary integer is

negative, positive, or zero?

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

slide-17
SLIDE 17

CPSC 121 – 2011W T2

Summary

 Unsigned and signed binary integers.  Characters.  Real numbers.

slide-18
SLIDE 18

CPSC 121 – 2011W T2

Unit 3: Representing Values

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 Interchange

 7-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-19
SLIDE 19

CPSC 121 – 2011W T2

Unit 3: Representing Values

What does the binary value 11111000 represent?

a) -8 b) The character c) 248 d) More than one of the above e) None of the above. ø

slide-20
SLIDE 20

CPSC 121 – 2011W T2

Summary

 Unsigned and signed binary integers.  Characters.  Real numbers.

slide-21
SLIDE 21

CPSC 121 – 2011W T2

Unit 3: Representing Values

Can someone be 1/3

rd Belgian?

Here is a very detailed answer from last year:

 "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
  • f 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 ethnic

  • identity. ... I suggest changing this question in the future as it

might be offensive to some minorities.”

slide-22
SLIDE 22

CPSC 121 – 2011W T2

Unit 3: Representing Values

Here is an interesting answer from this year:

 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-23
SLIDE 23

CPSC 121 – 2011W T2

Unit 3: Representing Values

Can someone be 1/3

rd Belgian?

a) Suppose we start with people who are either 0% or 100% Belgian. b) After 1 generation, how Belgian can a child be? c) After 2 generations, how Belgian can a grand-child be? d) What about 3 generations? e) What about n generations?

(c) ITV/Rex Features (c) 1979, Dargaud ed. et Albert Uderzo

slide-24
SLIDE 24

CPSC 121 – 2011W T2

Unit 3: Representing Values

Numbers with fractional components in binary:

 Example: 5/32 = 0.00101

Which of the following values have a finite binary expansion?

a) 1/10 b) 1/3 c) 1/4 d) More than one of the above. e) None of the above.

slide-25
SLIDE 25

CPSC 121 – 2011W T2

Unit 3: Representing Values

Numbers with fractional components (cont):

 In decimal:

 1/3 = 0.333333333333333333333333333333333333...  1/8 = 0.125  1/10 = 0.1

 In binary:

 1/3 =  1/8 =  1/10 =

Which fractions have a finite decimal expansion?

slide-26
SLIDE 26

CPSC 121 – 2011W T2

Unit 3: Representing Values

How does Java represent values of the form xxx.yyyy?

 They use scientific notation

 1724 = 0.1724 x 10 4

 But in binary, instead of decimal.

 1724 = 0.11010111100 x 2 1011

 Only the mantissa and exponent need to be stored.  The mantissa has a fixed number of bits (24 for float,

53 for double).

mantissa exponent

slide-27
SLIDE 27

CPSC 121 – 2011W T2

Unit 3: Representing Values

Scheme/Racket uses this for inexact numbers. Consequences:

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

 Example: predict the output of:

(* #i0.01 0.01 0.01 100 100 100)

slide-28
SLIDE 28

CPSC 121 – 2011W T2

Unit 3: Representing Values

Consider the following: What output will (addfractions 0) return?

a) 10 d) No value will be printed b) 11 e) None of the above c) More than 11

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

slide-29
SLIDE 29

CPSC 121 – 2011W T2

Unit 3: Representing Values

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-30
SLIDE 30

CPSC 121 – 2011W T2

Unit 3: Representing Values

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-31
SLIDE 31

CPSC 121 – 2011W T2

Unit 3: Representing Values

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: 24577.  Problem:

Solution: use hexadecimal 6031

Arithmetic operation Addition %ebx %ecx

We can not tell what operation this is.