52
snick snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Snick snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

  • View
    217

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

snick

snack

1

CPSC 121: Models of Computation2009 Winter Term 1

Number Representation

Steve Wolfman, based on notes by Patrice Belleville and others

Page 2: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

2

Page 3: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Lecture Prerequisites

Read Section 1.5 and the supplemental handout: http://www.ugrad.cs.ubc.ca/~cs121/2009W1/Handouts/signed-binary-decimal-conversions.html

Solve problems like Exercise Set 1.5, #1-16, 27-36, and 41-46.

NOTE: the binary numbers in problems 27-30 are signed (that is, they use the two’s complement representation scheme).

Complete the open-book, untimed quiz on Vista that’s due before the next class.

3

Page 4: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

Page 5: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

Page 6: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Where We Are inThe Big Stories

Theory

How do we model computational systems?

Now: showing that our logical models can connect smoothly to models of number systems.

Hardware

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

Page 7: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

7

Page 8: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

8

# p q r

0 F F F

1 F F T

2 F T F

3 F T T

4 T F F

5 T F T

6 T T F

7 T T T

Page 9: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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.

9

# p q r

0 0 0 0

1 0 0 1

2 0 1 0

3 0 1 1

4 1 0 0

5 1 0 1

6 1 1 0

7 1 1 1

But... What does it mean for basic operations to be “easy”?

Page 10: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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?

10

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.

Page 11: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

11

Page 12: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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.

12

A level is a carpentry tool, essentially a straightedge that indicates when it is either horizontal or vertical.

Page 13: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Clock Arithmetic

0500 is five hours from midnight.

1900 is five hours to midnight.

5 and 19 are “additive inverses” in clock arithmetic: 5 + 19 = 0.

So are any other numbers that are “across the clock” from each other.

13That’s even true for 12. Its additive inverse is itself!

Page 14: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

a.0 hundred (midnight)

b.4 hundred

c.8 hundred

d.14 hundred

e.None of these14(Clock arithmetic is also known as

modular arithmetic in mathematics.)

Page 15: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Clock Arithmetic: Food for Thought

0-3

-6

-9-12

If we wanted negative numbers on the clock,we’d probably put them “across the clock” from the positives.

After all, if 3 + 21 is already 0, why not put -3 where 21 usually goes?

15

Page 16: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Unsigned Binary Clock

Here’s a 3-bit unsigned binary clock, numbered from 0 (000) to 7 (111).

16

000001

010

011

100

101

110

111

Page 17: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

17

000001

010

011

100

101

110

111

Page 18: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Reminder:Two’s Complement

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

1 1 1 ...1

- b1b2b3...bn

----------

x1x2x3...xn

+ 1 ----------

-B18

Flip the bits

Add one

Page 19: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

A Different View ofTwo’s Complement

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

1 1 1 ...1

- b1b2b3...bn

----------

x1x2x3...xn

+ 1 ----------

-B19

Flip the bits

Add one

1 1 1 ...1

+ 1

----------

1 0 0 0 ...0

- b1b2b3...bn

----------

-BOr... Just subtract from 100...0

Page 20: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Two’s Complement vs. Crossing the Clock

Two’s complement with k bits:

Equivalent to subtracting from 100...000 with k 0s.

“Crossing the clock” with k bits:

Equivalent to subtracting from 100...000 with k 0s.

20Two’s complement turns numbers into their

“normal”, “cross-the-clock” additive inverses.

1 1 1 ...1

+ 1

----------

1 0 0 0 ...0

- b1b2b3...bn

----------

-B

000001

010

011100

101

110

111

Page 21: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Problem: Why Two’s complement?

Why make the negation of 010 be 110?

a.010 + 110 already equals 0.

b.010 + 110 equals 1000.

c.110 is the easiest negation to calculate.

d.110 isn’t being used for any other purpose.

e.If you invert the bits in 110 and add 1, you get 010.

000001

010

011

100

101

110

111

21

Page 22: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

22

Page 23: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Problem: 1/3 Scottish

Problem: Can you be 1/3 Scottish?

23

Page 24: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Can you be one-third Scottish?

Mom:??

Dad:??

Focus on Mom (and Mom’s Mom and so on).We’ll just make Dad “Scot” or “Not” as needed at each step.

Page 25: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Can you be one-third Scottish?

Mom:2/3

Dad:Not

Mom:??

Dad:??

Page 26: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Can you be one-third Scottish?

Mom:2/3

Dad:Not

Mom:1/3

Dad:Scot

Mom:??

Dad:??

Page 27: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Can you be one-third Scottish?

Mom:2/3

Dad:Not

Mom:1/3

Dad:Scot

Dad:Not

Mom:1/3

Dad:Scot

And so on...

What’s happening here?

Mom:2/3

Page 28: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Mom:2/3

Dad:Not

Mom:1/3

Dad:Scot

Mom:2/3

Dad:Not

Mom:1/3

Dad:Scot

Now, focus on Dad...We can represent fractions in binary by making “Scottish family trees”:

Page 29: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Mom:0.75

Dad:Not

Mom:0.5

Dad:Scot

Mom:0.0

Dad:Scot

Here’s 0.375 in binary...

Page 30: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Problem: 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/9b.1/8c.1/7d.1/6e.None of these.

30

Page 31: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

So... Computers Can’t Represent 1/3?

No! Using a different scheme (e.g., in Java, a rational class with numerator and denominator fields), computers can perfectly represent 1/3!

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!

31

Page 32: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

32

Page 33: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

What Doesn’t Work isNot 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)); }}

33

Page 34: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

What Doesn’t Work isNot 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)); }}

34

Page 35: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Number Representation Prediction

// 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);

35

Can 0.1 be represented in base 2? Will i > 0 always be true?a.No and........................................ no.b.No and........................................ yes.c.Yes and....................................... no.d.Yes and....................................... yes.e.None of these (b/c the answers are not so straightforward)

Page 36: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

36

Page 37: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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 1610. The opcode for istore_1 is 6010. Here’s a typical “hex” view of ~1/5th of the previous program’s byte code. Where is i = 100?

37

a

db

c

e: None of these.

b

Page 38: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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?

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.

38

Page 39: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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?

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.

39

Page 40: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

40

Page 41: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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.

41

Page 42: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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.

42

Page 43: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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 Vista that is due before next class.

43

Page 44: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

snick

snack

Some Things to Try...

(on your own if you have time, not required)

44

Page 45: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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?

45

?g ?g?g?g

Page 46: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

46

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

Page 47: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

47

Representing Characters

Page 48: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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?

48

Page 49: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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;b = b + 64;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.

49

Page 50: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

Problem: Number Rep Breakdown

Problem: Explain what’s happening in each of these…

50

Page 51: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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

Page 52: Snick  snack 1 CPSC 121: Models of Computation 2009 Winter Term 1 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others

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