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

Slides:



Advertisements
Similar presentations
Number Representation and Logic Design CS 3220 Fall 2014 Hadi Esmaeilzadeh Georgia Institute of Technology Some slides adopted from.
Advertisements

The Binary Numbering Systems
COE 202: Digital Logic Design Signed Numbers
Snick  snack CPSC 121: Models of Computation 2009 Winter Term 1 Introduction & Motivation Steve Wolfman, based on notes by Patrice Belleville and others.
Snick  snack CPSC 121: Models of Computation 2009 Winter Term 1 Introduction to Induction Steve Wolfman 1.
Snick  snack CPSC 121: Models of Computation 2009 Winter Term 1 Propositional Logic: Conditionals and Logical Equivalence Steve Wolfman, based on notes.
Snick  snack A Working Computer Slides based on work by Bob Woodham 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.
1 CSE1301 Computer Programming Lecture 29: Number Representation (Part 1)
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, Java Version, Third Edition.
Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Propositional Logic, Continued Steve Wolfman, based on notes by Patrice Belleville and.
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Third Edition.
Factors, Fractions, and Exponents
Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Revisiting Induction Steve Wolfman, based on work by Patrice Belleville and others 1.
Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Introduction & Motivation Steve Wolfman, based on notes by Patrice Belleville and others.
Wakerly Section 2.4 and further Addition and Subtraction of Nondecimal Numbers.
Assembly Language and Computer Architecture Using C++ and Java
Level ISA3: Information Representation
Introduction to Programming with Java, for Beginners
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Assembly Language and Computer Architecture Using C++ and Java
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
Snick  snack CPSC 121: Models of Computation 2010/11 Winter Term 2 Propositional Logic, Continued Steve Wolfman, based on notes by Patrice Belleville.
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
Snick  snack CPSC 121: Models of Computation 2010/11 Winter Term 2 Introduction & Motivation Steve Wolfman, based on notes by Patrice Belleville and others.
Exponents Scientific Notation
Binary Arithmetic Math For Computers.
Number Systems Lecture 02.
COMP201 Computer Systems Number Representation. Number Representation Introduction Number Systems Integer Representations Examples  Englander Chapter.
Connecting with Computer Science 2 Objectives Learn why numbering systems are important to understand Refresh your knowledge of powers of numbers Learn.
Binary Representation and Computer Arithmetic
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
CENG 311 Machine Representation/Numbers
(2.1) Fundamentals  Terms for magnitudes – logarithms and logarithmic graphs  Digital representations – Binary numbers – Text – Analog information 
Numbering systems.
MATH 224 – Discrete Mathematics
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
IT253: Computer Organization
Foundations of Computer Science Computing …it is all about Data Representation, Storage, Processing, and Communication of Data 10/4/20151CS 112 – Foundations.
Digital Logic Design Lecture 3 Complements, Number Codes and Registers.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction.
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
Topic 1 – Number Systems. What is a Number System? A number system consists of an ordered set of symbols (digits) with relations defined for addition,
Snick  snack CPSC 121: Models of Computation 2012 Summer Term 2 Introduction & Motivation Steve Wolfman, based on notes by Patrice Belleville and others.
1 COMS 161 Introduction to Computing Title: The Digital Domain Date: September 6, 2004 Lecture Number: 6.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Snick  snack 1 CPSC 121: Models of Computation 2013W2 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others This work is.
CPSC 121: Models of Computation Unit 3 Representing Numbers and Other Values in a Computer Based on slides by Patrice Belleville and Steve Wolfman.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
CSE 351 Number Representation & Operators Section 2 October 8, 2015.
1 Fundamentals of Computer Science Combinational Circuits.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
Snick  snack CPSC 121: Models of Computation 2008/9 Winter Term 2 Introduction & Motivation Steve Wolfman, based on notes by Patrice Belleville and others.
CSE 351 Number Representation. Number Bases Any numerical value can be represented as a linear combination of powers of n, where n is an integer greater.
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Snick  snack 1 CPSC 121: Models of Computation 2008/9 Winter Term 2 Number Representation Steve Wolfman, based on notes by Patrice Belleville and others.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Invitation to Computer Science, C++ Version, Fourth Edition
Bits, Data Types, and Operations
3.1 Denary, Binary and Hexadecimal Number Systems
Fundamentals & Ethics of Information Systems IS 201
Wakerly Section 2.4 and further
Data Structures Mohammed Thajeel To the second year students
Invitation to Computer Science, Java Version, Third Edition
CPSC 121: Models of Computation 2016W2
Fundamentals of Data Representation
Presentation transcript:

snick  snack 1 CPSC 121: Models of Computation 2011 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

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

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

Where We Are in The 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!) 5

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 6

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... 7 #pqr 0FFF 1FFT 2FTF 3FTT 4TFF 5TFT 6TTF 7TTT

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. 8 #pqr But... What does it mean for basic operations to be “easy”?

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

Side Note (Skipping in Lecture) Are base 2 numbers the only useful numbering system? No. Say you want to build a “rotary encoder”. Turn a dial’s physical angle into a number representing that angle. You build a series of light senser going out along a radius and then paint a disk at angles into binary numbers (with black meaning 0 and shiny meaning 1, or vice versa). Should you encode with base 2? 10

Side Note (Skipping in Lecture) Rotary Encoder: Should you encode with base 2? No! Why not? Consider a dial with 4 sensors. As you go from 15 around to 0, every single “bit” changes. Can you absolutely guarantee that your sensors will all find the “edge” between those angles at the same time? If not (and you can’t), you get brief incorrect readings. The dial seems to jump all over the place! 11

Side Note (Skipping in Lecture) Instead, you want an encoding where only one bit changes at the boundary between each successive pair of numbers. That’s a “Gray code” like 000, 001, 011, 010, 010, 111, 101, 100. Now, only ONE sensor changes at a boundary anyway! (Double side note: For the engineers/ interface designers out there, you still want to build in what’s called “hysteresis”.) 12

Side Note (Skipping in Lecture) Is this really used? Here’s Wikipedia’s abstract and concrete examples: 13 P.S. Gray codes are also central to a technique for minimizing circuits generated from truth tables called Karnaugh maps!

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 14

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. 15 A level is a carpentry tool, essentially a straightedge that indicates when it is either horizontal or vertical.

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. 16 That’s even true for 12. Its additive inverse is itself!

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 these 17 (Clock arithmetic is also known as modular arithmetic in mathematics.)

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

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

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)

Reminder: Two’s Complement Taking two’s complement of B = b 1 b 2 b 3...b n : b 1 b 2 b 3...b n x 1 x 2 x 3...x n B 21 Flip the bits Add one

A Different View of Two’s Complement Taking two’s complement of B = b 1 b 2 b 3...b n : b 1 b 2 b 3...b n x 1 x 2 x 3...x n B 22 Flip the bits Add one b 1 b 2 b 3...b n B Or... Just subtract from

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. 23 Two’s complement turns numbers into their “normal”, “cross-the-clock” additive inverses b 1 b 2 b 3...b n B

Problem: Why Two’s complement? Why make the negation of 010 be 110? (In other words: why use the two’s complement scheme?) a already equals 0. b equals 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

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

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? What one value does that negation fail (or at least do something weird) on for signed binary integers? What does this really mean? How do we calculate the decimal value of a positive signed binary integer? How do we calculate the decimal value of a negative signed binary integer? 26

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 27

Problem: 1/3 Scottish Problem: Can you be 1/3 Scottish? 28

Problem: 1/3 Scottish Problem: Can you be 1/3 Scottish? 29 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 are 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!

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.

Can you be one-third Scottish? Mom: 2/3 Dad: Not Mom: ?? Dad: ??

Can you be one-third Scottish? Mom: 2/3 Dad: Not Mom: 1/3 Dad: Scot Mom: ?? Dad: ??

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

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”:

Mom: 0.75 Dad: Not Mom: 0.5 Dad: Scot Mom: 0.0 Dad: Scot Here’s in binary

Problem: Base 10 vs. Base 2 Can we represent 1/9 with a finite number of digits in base 10 or base 2? a.Yes (in both). b.Yes in base 10, but not in base 2. c.Yes in base 2, but not in base 10. d.No. e.I don’t know. 36

Problem: Base 10 vs. Base 2 Can we represent 1/8 with a finite number of digits in base 10 or base 2? a.Yes (in both). b.Yes in base 10, but not in base 2. c.Yes in base 2, but not in base 10. d.No. e.I don’t know. 37

Problem: Base 10 vs. Base 2 Can we represent 1/5 with a finite number of digits in base 10 or base 2? a.Yes (in both). b.Yes in base 10, but not in base 2. c.Yes in base 2, but not in base 10. d.No. e.I don’t know. 38

So... Computers Can’t Represent 1/3? No! Using a different 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! 39

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

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)); } 41

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)); } 42

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); 43 What will this print? a.First 1.0 and then nothing because it runs forever. b.First 1.0 and then some other value, because it won’t run forever. c.First something OTHER than 1.0 and then nothing because it runs forever. d.First something OTHER than 1.0 and then some other value, because it won’t forever. e.None of these

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 44

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.

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/5 th of the previous program’s byte code. Where is i = 100 ? 46 a d b c e: None of these. b

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 (100 in base 2) might not show up as the sequence of numbers e.It wouldn’t be much more difficult. 47

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 e.It wouldn’t be much more difficult. 48

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 49

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

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

Next Lecture Prerequisites Read Section 1.3 (Epp 3 rd ed) or 2.3 (Epp 4 th ed). Complete the open-book, untimed quiz on Vista that is due before next class. 52

snick  snack Some Things to Try... (on your own if you have time, not required) 53

54 Representing Characters

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 ? 55 ?g

56 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

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

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.A positive number, greater than 70. b.70. c.A positive number, less than 70. d.0. e.A negative number. 58

Problem: Number Rep Breakdown Problem: Explain what’s happening in each of these… 59

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

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