Data Representation Eric Roberts CS 106A February 10, 2016.

Slides:



Advertisements
Similar presentations
Review of HTML Ch. 1.
Advertisements

1 Chapter 2 The Digital World. 2 Digital Data Representation.
Dynamic Allocation Eric Roberts CS 106B February 4, 2013.
The Assembly Language Level
Introduction to C Programming
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Level ISA3: Information Representation
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Information Processing Session 5B Binary Arithmetic Slide
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2 Data Types, Declarations, and Displays
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Introduction to C Programming
The Binary Number System
2.1 2 Number Systems Foundations of Computer Science  Cengage Learning.
Real Numbers and the Decimal Number System
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
Programmable Logic Controllers
Memory and C++ Eric Roberts CS 106B February 1, 2013.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Copyright © Cengage Learning. All rights reserved. CHAPTER 2 THE LOGIC OF COMPOUND STATEMENTS THE LOGIC OF COMPOUND STATEMENTS.
Numeral Systems Subjects: Numeral System Positional systems Decimal
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Chapter 7 Objects and Memory. Structure of memory The fundamental unit of memory is called a bit, either 0 or 1. In most modern architectures, the smallest.
Chapter 7—Objects and Memory The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Memory C H A P T E R 7 Yea, from.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
IT-101 Section 001 Lecture #3 Introduction to Information Technology.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 7—Objects and Memory The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Memory C H A P T E R 7 Yea, from.
Data Representation in Computer Systems. 2 Objectives Understand the fundamentals of numerical data representation and manipulation in digital computers.
Introduction to Computer Systems and the Java Programming Language.
Chapter 7—Objects and Memory The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Memory C H A P T E R 7 Yea, from.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
THE BINARY SYSTEM.
Copyright Curt Hill Variables What are they? Why do we need them?
June 10, 2002© Howard Huang1 Number systems To get started, we’ll discuss one of the fundamental concepts underlying digital computer design:
Number Systems by Dr. Amin Danial Asham. References  Programmable Controllers- Theory and Implementation, 2nd Edition, L.A. Bryan and E.A. Bryan.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Data Representation. What is data? Data is information that has been translated into a form that is more convenient to process As information take different.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
Expressions Eric Roberts CS 106A January 13, 2016.
Characters and Strings
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Five-Minute Review 1.What are local/instance/class variables? What about constants? 2.What is an array? 3.How do we locally declare an array of 5 integers?
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Object Lifetime and Pointers
Five-Minute Review What are local/instance/class variables? What about constants? What is an array? How do we locally declare an array of 5 integers? How.
The Machine Model Memory
Java Primer 1: Types, Classes and Operators
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Data Representation in Computer Systems
Variables and Primative Types
Please visit
Binary Representation
Objects and Memory Eric Roberts CS 106A February 5, 2010.
Bases and Representations, Memory, Pointers, Arrays, For-Loops
Ch2: Data Representation
Variables ICS2O.
Chapter 7—Objects and Memory
Chapter 7—Objects and Memory
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Binary Representation
Chapter 7—Objects and Memory
Overloading functions
Copyright © Cengage Learning. All rights reserved.
CHAPTER 69 NUMBER SYSTEMS AND CODES
Five-Minute Review What are local/instance/class variables? What about constants? What is an array? How do we locally declare an array of 5 integers? How.
Presentation transcript:

Data Representation Eric Roberts CS 106A February 10, 2016

Once upon a time...

Claude Shannon Claude Shannon was one of the pioneers who shaped computer science in its early years. In his master’s thesis, Shannon showed how it was possible to use Boolean logic and switching circuits to perform arithmetic calculations. That work led directly to the use of binary arithmetic inside computers—an idea that initially generated considerable resistance among more established scientists. Shannon is best known as the inventor of information theory, which provides the mathematical foundation for understanding data communication. To many, however, Shannon’s great contribution was that he never lost his sense of fun throughout his long career. Claude Shannon ( )

Data Representation

The Structure of Memory The fundamental unit of memory inside a computer is called a bit, which is a contraction of the words binary digit. A bit can be in either of two states, usually denoted as 0 and 1. Numbers are stored in still larger units that consist of multiple bytes. The unit that represents the most common integer size on a particular hardware is called a word. Because machines have different architectures, the number of bytes in a word varies from machine to machine The hardware structure of a computer combines individual bits into larger units. In most modern architectures, the smallest unit on which the hardware operates is a sequence of eight consecutive bits called a byte. The following diagram shows a byte containing a combination of 0s and 1s:

Binary Notation The rightmost digit is the units place. The next digit gives the number of 2s. The next digit gives the number of 4s. And so on... Bytes and words can be used to represent integers of different sizes by interpreting the bits as a number in binary notation. 0 x 0 1 = 1 x 2 2 = 0 x 0 4 = 42 1 x 8 8 = 0 x 0 16 = 1 x 32 = 0 x 0 64 = 0 x = Binary notation is similar to decimal notation but uses a different base. Decimal numbers use 10 as their base, which means that each digit counts for ten times as much as the digit to its right. Binary notation uses base 2, which means that each position counts for twice as much, as follows:

Numbers and Bases The calculation at the end of the preceding slide makes it clear that the binary representation is equivalent to the number 42. When it is important to distinguish the base, the text uses a small subscript, like this: = Although it is useful to be able to convert a number from one base to another, it is important to remember that the number remains the same. What changes is how you write it down. The number 42 is what you get if you count how many stars are in the pattern at the right. The number is the same whether you write it in English as forty-two, in decimal as 42, or in binary as Numbers do not have bases; representations do.

Octal and Hexadecimal Notation Because binary notation tends to get rather long, computer scientists often prefer octal (base 8) or hexadecimal (base 16) notation instead. Octal notation uses eight digits: 0 to 7. Hexadecimal notation uses sixteen digits: 0 to 9, followed by the letters A through F to indicate the values 10 to 15. The following diagrams show how the number forty-two appears in both octal and hexadecimal notation: 2 x x x x A 42 octal hexadecimal = = = = The advantage of using either octal or hexadecimal notation is that doing so makes it easy to translate the number back to individual bits because you can convert each digit separately.

Exercises: Number Bases What is the decimal value for each of the following numbers? AD 16 1 x = x = 0 2 = 0 4 = x = x = 7 1 = x = x = x = AD As part of a code to identify the file type, every Java class file begins with the following sixteen bits: How would you express that number in hexadecimal notation? AFE CAFE

Memory and Addresses Every byte inside the primary memory of a machine is identified by a numeric address. The addresses begin at 0 and extend up to the number of bytes in the machine, as shown in the diagram on the right. In these slides as well as in the diagrams in the text, memory addresses appear as four-digit hexadecimal numbers, which makes addresses easy to recognize. In Java, it is impossible to determine the address of an object. Memory addresses used in the examples are therefore chosen completely arbitrarily. Memory diagrams that show individual bytes are not as useful as those that are organized into words. The revised diagram on the right now includes four bytes in each of the memory cells, which means that the address numbers increase by four each time A 000B FFF4 FFF5 FFF6 FFF7 FFF8 FFF9 FFFA FFFB FFFC FFFD FFFE FFFF C C C FFD0 FFD4 FFD8 FFDC FFE0 FFE4 FFE8 FFEC FFF0 FFF4 FFF8 FFFC

The Allocation of Memory to Variables When you declare a variable in a program, Java allocates space for that variable from one of several memory regions. One region of memory is reserved for variables that are never created or destroyed as the program runs, such as named constants and other class variables. This information is called static data. static data 0000 stack FFFF Each time you call a method, Java allocates a new block of memory called a stack frame to hold its local variables. These stack frames come from a region of memory called the stack. Whenever you create a new object, Java allocates space from a pool of memory called the heap. heap In classical architectures, the stack and heap grow toward each other to maximize the available space.

Heap-Stack Diagrams It is easier to understand how Java works if you have a good mental model of its use of memory. The text illustrates this model using heap-stack diagrams, which show the heap on the left and the stack on the right, separated by a dotted line. Whenever your program creates a new object, you need to add a block of memory to the heap side of the diagram. That block must be large enough to store the instance variables for the object, along with some extra space, called overhead, that is required for any object. Overhead space is indicated in heap-stack diagrams as a crosshatched box. Whenever your program calls a method, you need to create a new stack frame by adding a block of memory to the stack side. For method calls, you need to add enough space to store the local variables for the method, again with some overhead information that tracks what the program is doing. When a method returns, Java reclaims the memory in its frame.

Object References Internally, Java identifies an object by its address in memory. That address is called a reference. stack 1000 r1 FFFC The local variable r1 is allocated in the current stack frame and is assigned the value 1000, which identifies the object. The next slide traces the execution of the TestRational program from Chapter 6 using the heap-stack model. Rational r1 = new Rational(1, 2); heap 2 den 1 num it allocates heap space for the new Rational object. For this example, imagine that the object is created at address As an example, when Java evaluates the declaration

A Complete Heap-Stack Trace skip simulation a b c sum FFFC FFF8 FFF4 FFF0 FFEC C TestRational stack This object is a temporary value used only during the calculation. 1/2 + 1/3 + 1/6 = 1 public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = a.add(b).add(c); println(a + " + " + b + " + " + c + " = " + sum); } 1 den 1 num 6 den 5 num 6 den 1 num 3 den 1 num 2 den 1 num public Rational add(Rational r) { return new Rational( this.num * r.den + r.num * this.den, this.den * r.den ); } public Rational add(Rational r) { return new Rational( this.num * r.den + r.num * this.den, this.den * r.den ); } 36 heap C C C C this r FFE8 FFE4 FFE C This stack frame is created for the run method. This stack frame is created for the add method. All objects are created in the heap. TestRational 1/2 + 1/3 + 1/6 = 1 public void run() { Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = a.add(b).add(c); println(a + " + " + b + " + " + c + " = " + sum); } 1 den 1 num 6 den 5 num 6 den 1 num 3 den 1 num 2 den 1 num heapstack a b c sum FFFC FFF8 FFF4 FFF0 FFEC C C C C

The Pointer Model 1 den 1 num 6 den 5 num 6 den 1 num 3 den 1 num 2 den 1 num heapstack a b c sum FFFC FFF8 FFF4 FFF0 FFEC C C C C The heap-stack diagram at the lower left shows the state of memory at the end of the run method from TestRational. The diagram at the lower right shows exactly the same state using arrows instead of numeric addresses. This style of diagram is said to use the pointer model. 1 den 1 num 6 den 5 num 6 den 1 num 3 den 1 num 2 den 1 num a b c sum heapstack

Garbage Collection One fact that the pointer model makes clear in this diagram is that there are no longer any references to the Rational value 5/6. That value has now become garbage. From time to time, Java runs through the heap and reclaims any garbage. This process is called garbage collection. 1 den 1 num 6 den 5 num 6 den 1 num 3 den 1 num 2 den 1 num a b c sum heapstack This object was used to hold a temporary result and is no longer accessible.

Exercise: Stack-Heap Diagrams public void run() { Point p1 = new Point(0, 0); Point p2 = new Point(200, 200); Line line = new Line(p1, p2); } public class Point { public Point(int x, int y) { cx = x; cy = y; }... other methods appear here... private int cx; private int cy; } Suppose that the classes Point and Line are defined as follows: public class Line { public Line(Point p1, Point p2) { start = p1; finish = p2; }... other methods appear here... private Point start; private Point finish; } Draw a heap-stack diagram showing the state of memory just before the following run method returns.

Solution: Stack-Heap Diagrams 100C finish 1000 start 200 cy 200 cx 0 cy 0 cx heap stack p1 p2 line FFFC FFF8 FFF4 FFF C C C finish start 200 cy 200 cx 0 cy 0 cx p1 p2 line heapstack Address ModelPointer Model

Primitive Types vs. Objects At first glance, Java’s rules for passing objects as arguments seem to differ from the rules Java uses with arguments that are primitive types. When you pass an argument of a primitive type to a method, Java copies the value of the argument into the parameter variable. As a result, changes to the parameter variable have no effect on the argument. When you pass an object as an argument, there seems to be some form of sharing going on. Although changing the parameter variable itself has no effect, any changes that you make to the instance variables inside an object—usually by calling setters—have a permanent effect on the object. Stack-heap diagrams make the reason for this seeming asymmetry clear. When you pass an object to a method, Java copies the reference but not the object itself.

The End