Internal Data Representation

Similar documents
Elements of Computing Systems, Nisan & Schocken, MIT Press. Boolean Arithmetic

Boolean Arithmetic. Building a Modern Computer From First Principles.

UNIT 7A Data Representation: Numbers and Text. Digital Data

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

COMP2611: Computer Organization. Data Representation

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

Zheng-Liang Lu Java Programming 45 / 79

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

MACHINE LEVEL REPRESENTATION OF DATA

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

COMP Overview of Tutorial #2

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

CS61c Midterm Review (fa06) Number representation and Floating points From your friendly reader

IEEE Standard for Floating-Point Arithmetic: 754

Digital Computers and Machine Representation of Data

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

CHAPTER 2 Data Representation in Computer Systems

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

CHAPTER 2 Data Representation in Computer Systems

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

Chapter 1. Data Storage Pearson Addison-Wesley. All rights reserved

Binary Representations and Arithmetic

The type of all data used in a C++ program must be specified

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

The type of all data used in a C (or C++) program must be specified

Introduction to Computers and Programming. Numeric Values

COMP Primitive and Class Types. Yi Hong May 14, 2015

Chapter 2 Bits, Data Types, and Operations

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

COMP2121: Microprocessors and Interfacing. Number Systems

Signed Multiplication Multiply the positives Negate result if signs of operand are different

9/23/15. Agenda. Goals of this Lecture. For Your Amusement. Number Systems and Number Representation. The Binary Number System

Binary Values. CSE 410 Lecture 02

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

l l l l l l l Base 2; each digit is 0 or 1 l Each bit in place i has value 2 i l Binary representation is used in computers

Number Systems & Encoding

Inf2C - Computer Systems Lecture 2 Data Representation

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

CSE 220: Systems Programming

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

LING 388: Computers and Language. Lecture 5

Module 2: Computer Arithmetic

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Few reminders and demos

Basic Operations jgrasp debugger Writing Programs & Checkstyle

MC1601 Computer Organization

1.00 Lecture 4. Promotion

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Chapter 2 Bits, Data Types, and Operations

M1 Computers and Data

Arithmetic Logic Unit (ALU)

Number System. Introduction. Decimal Numbers

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

CSCI 402: Computer Architectures. Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI.

Floating Point Numbers

Data Representation 1

Chapter 2 Bits, Data Types, and Operations

CS & IT Conversions. Magnitude 10,000 1,

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

CS61B Lecture #14: Integers. Last modified: Wed Sep 27 15:44: CS61B: Lecture #14 1

More Programming Constructs -- Introduction

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Variables and Data Representation

Floating Point Arithmetic

CSCI 2212: Intermediate Programming / C Chapter 15

Princeton University Computer Science 217: Introduction to Programming Systems. Goals of this Lecture. Number Systems and Number Representation

CHW 261: Logic Design

Chapter 2. Data Representation in Computer Systems

BSM540 Basics of C Language

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

JAVA OPERATORS GENERAL

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Work relative to other classes

Number Systems and Number Representation

Chapter 3: Number Systems and Codes. Textbook: Petruzella, Frank D., Programmable Logic Controllers. McGraw Hill Companies Inc.

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

Announcement. (CSC-3501) Lecture 3 (22 Jan 2008) Today, 1 st homework will be uploaded at our class website. Seung-Jong Park (Jay)

Hani Mehrpouyan, California State University, Bakersfield. Signals and Systems

Information Science 1

Lecture 03 Bits, Bytes and Data Types

Recap from Last Time. CSE 2021: Computer Organization. It s All about Numbers! 5/12/2011. Text Pictures Video clips Audio

COMP6700/2140 Data and Types

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Digital Fundamentals

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Final Labs and Tutors

Bits, Words, and Integers

Chapter 2. Data Representation in Computer Systems

HALF-YEARLY EXAMINATIONS FEBRUARY Subject: Computing Form: 4 Time: 1 ½ hours MARKING SCHEME

Question 4: a. We want to store a binary encoding of the 150 original Pokemon. How many bits do we need to use?

T02 Tutorial Slides for Week 2

CS101 Lecture 04: Binary Arithmetic

Transcription:

Introduction to Computer Science, Shimon Schocken, IDC Herzliya Lecture 7. Internal Data Representation Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 1

Why binary representation makes sense? Inside computers, all data is represented as 0 s and 1 s A RAM segment (arbitrary snapshot) Why binary representation makes sense? 1010501 00110110001001100011011000110110 101050 10101010000101010100101010110110 Because The RAM is electric, The disk is magnetic, The communications is optical, The CPU logic is true/false All bi-state artifacts! The binary system is a perfect match to the way computers work. 1010503 1010504 1010505 1010506 1010507 1010508 1010509 1010510 1010511 101051 1010513 1010514 1010515 1010516 10110110001101101011011010100110 10110010001101101010011010100110 00110010000000011011011010110110 10000000000011001011011010110110 00110110001001100011011000110110 10101010000101010100101010110110 10110110001101101011011010100110 10110010001101101010011010100110 00110010000000011011011010110110 10000000000011001011011010110110 00110110001001100011011000110110 10101010000101010100101010110110 10110110001101101011011010100110 10110010001101101010011010100110 Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide

Basic Program Elements: lecture outline Number systems Binary Octal Hexadecimal Decimal Internal representations of Positive integers Negative integers Fractional numbers Internal representations of Characters and strings Images Music Other media types. Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 3

Number systems (well, some of them) unary decimal binary 3-bit register 0 0 000 1 1 001 10 010 3 11 011 4 100 100 5 101 101 6 110 110 7 111 111 8 1000 overflow 9 1001 overflow 10 1010 overflow Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 4

How many things can you represent with two signs? 1 bit bit 3 bit 4 bit 0 1 00 01 10 11 000 001 010 011 100 101 0000 0001 0010 0011 0100 0101 A binary system based on n bits can represent n different things Each time we add a bit to the representation, we can represent twice as many things 110 111 0110 0111 1000 We can use these bit combinations to create agreed-upon codes of 1001 1010 Numbers 1011 Characters 1100 1101 Images 1110 1111 Etc. Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 5

The binary system Let s use cards to represent numbers: What is the smallest number that we can represent? The largest? Can we represent any number within this range? Using the cards to represent numbers Is the representation unique? How can you tell that a number is odd / even? Binary representation of 9 How to convert binary to decimal? How to convert a decimal to binary? How to add 1 to a given number? Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 6

The binary system Computing the decimal value of a binary number: (10011) two = 1 4 + 0 3 + 0 + 1 1 + 1 0 = 19 The general scheme: ( x n x n 1... x0 ) b n = i= 0 x i b i This scheme works with any given number system with base b: (9038) ten = 9 10 3 + 0 10 + 3 10 1 + 8 10 0 = 9038 Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 7

Basic Program Elements: lecture outline Number systems Binary Octal Hexadecimal Decimal Internal representations of Positive integers Negative integers Fractional numbers Internal representations of Characters and strings Images Music Other media types. Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 8

Binary, octal, hexadecimal Binary 0 Octal 0 Hexa 0 4 7 6 5 Octal 1 1 1 0 1 0 0 1 1 1 0 1 0 1 1 0 1 0 1 Binary 3 3 4 5 4 5 4 E B 5 Hexa 6 6 7 8 7 8 9 A B Binary-octal-hexa conversions are easy It is customary to display internal representation of data inside computers using octal or hexa. C D E F Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 9

Examples where octal and hexa are used in computing Unix file permissions (r = read, w = write, x = execute) (Owner, Group, Others) 0 --- no permission 777 -rwxrwxrwx rwx for all 1 --x execute 754 -rwxr-xr-- rwx for owner, rx for group, r for others -w- write 14 ---x-w-r-- x for owner, w for group, read for others 3 -wx write and execute 4 r-- read 5 6 r-x read and execute rw- read and write Unix internal representation of file permissions (octal) 7 rwx read, write and execute URLs http://www.example.com/name%0with%0spaces (Two unrelated examples) The hexa value %0 is 3 in decimal, representing space in ASCII Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 10

Examples where octal and hexa are used in computing public public class class Bla Bla { { public public static static void void main(string[] main(string[] args) args) { { Foo Foo f f = = new new Foo(); Foo(); System.out.println("Value System.out.println("Value of of f f variable: variable: " " + + f); f); } } } } class class Foo Foo { { } } Memory dump = a snapshot of a memory segment Sometimes used for hard core debugging, or hacking Memory addresses and contents are usually shown in hexa. (The Java program and the memory dump are unrelated) Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 11

Basic Program Elements: lecture outline Number systems Binary Octal Hexadecimal Decimal Internal representations of Positive integers (done) Negative integers Fractional numbers Internal representations of Characters and strings Images Music Other media types. Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 1

Representing negative numbers Assuming 4-bit codes: 0 0000 1 0001 1111-1 0010 1110-3 0011 1101-3 4 0100 1100-4 5 0101 1011-5 6 0110 1010-6 7 0111 1001-7 1000-8 - 5 = + (-5) = 0 0 1 0 + 1 0 1 1 1 1 0 1 = -3 The s complement system: Divide all possible codes to two subsets Assign the codes that begin with 0 to positive numbers Assign the codes that begin with 1 to negative numbers To negate a number: leave all trailing 0 s and first 1 intact; flip all the remaining bits Implications: We know how to represent byte, short, int, long, and char Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 13

Basic Program Elements: lecture outline Number systems Binary Octal Hexadecimal Decimal Internal representations of Positive integers Negative integers Fractional numbers Internal representations of Characters and strings Images Music Other media types. Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 14

Representing fractional numbers Regular representation: 53.81 0.00791 Floating point representation: 5381 * 10-791 * 10-5 Normalized: 5.381 * 10-4 7.91 * 10-3 Significand Exponent In normalized form, there is only one digit before the decimal point, and this digit must not be zero. The decimal point floats to affect the necessary correction Thus, we can represent fractional numbers as pairs of two numbers: the significand and the exponent Floating point arithmetic (example): 13456.7 + 101.7654 = (1.34567 * 10^5) + (1.017654 * 10^) = (1.34567 * 10^5) + (0.001017654 * 10^5) = (1.34567 + 0.001017654) * 10^5 = 1.35584654 * 10^5 All arithmetic operations are done using floating point representation. The numbers are converted to decimals only when we wish to read or write a value. Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 15

Floating point in binary Sign Exponent Significand value (3 bit sequence) = (-1) sign bit * significand * exponent Each bit in the significand represents a value that halves, as follows: bit = 0.5, bit 1 = 0.5, bit 0 = 0.15, bit 19 = 0.065... This may lead to slight accuracy anomalies. Implications: This is how float and double are represented. Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 16

Basic Program Elements: lecture outline Number systems Binary Octal Hexadecimal Decimal Internal representations of Positive integers Negative integers Fractional numbers Internal representations of Characters and strings Images Music Other media types. Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 17

Representing non-numeric data The data on the right can be part of a Spreadsheet A RAM segment (arbitrary snapshot) 1010501 00110110001001100011011000110110 Word document JPG Image Video stream Medical record Operating system code Java code So what is it? There s is no way to tell The semantics of the data makes sense only in the context in which we use it. 101050 1010503 1010504 1010505 1010506 1010507 1010508 1010509 1010510 1010511 101051 1010513 1010514 1010515 1010516 10101010000101010100101010110110 10110110001101101011011010100110 10110010001101101010011010100110 00110010000000011011011010110110 10000000000011001011011010110110 00110110001001100011011000110110 10101010000101010100101010110110 10110110001101101011011010100110 10110010001101101010011010100110 00110010000000011011011010110110 10000000000011001011011010110110 00110110001001100011011000110110 10101010000101010100101010110110 10110110001101101011011010100110 10110010001101101010011010100110 Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 18

Representing characters and strings Symbol table symb addr type public public class class Charade Charade { { public public static static void void main(string[] main(string[] args){ args){ System.out.println(); System.out.println(); String String s1 s1 = = "It\'s "It\'s just"; just"; String String s s = = "a "a huge, huge, incredible"; incredible"; String String s3 s3 = = "charade."; "charade."; System.out.println(s1+" System.out.println(s1+" "+s+" "+s+" "+s3); "+s3); } } } } 10096 10097 70606 10098 70611 RAM (3-bit) s1 10096 s 10097 s 10098 70603 70603 9 73 116 96 String String String 70604 115 3 106 117 70605 115 116 70606 18 97 3 104 70607 117 103 101 44 There s a big difference between the way the data is rendered to our senses, and the way the data is stored in memory This should not be surprising. 70608 3 105 110 99 70609 114 101 100 105 70610 98 108 101 70611 7 99 104 97 7061 114 97 100 101 Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 19

Representing other media types Text (e.g. Ascii) Sound (e.g. MP3) Images (e.g. JPG) Programs (e.g. Bytecode) From an agreed-upon, media-specific standard representation to bits From bits to an agreed-upon, media-specific standard representation RAM 00110110001001100011011000110110 10101010000101010100101010110110 10110110001101101011011010100110 10110010001101101010011010100110 00110010000000011011011010110110 10000000000011001011011010110110 00110110001001100011011000110110 10101010000101010100101010110110 10110110001101101011011010100110 10110010001101101010011010100110 00110010000000011011011010110110 10000000000011001011011010110110 The conversions are performed only when data is acquired or rendered The conversions are typically performed by programs running on device drivers. Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 0

On the shoulders of giants Leibniz s medallion: Goittfried Leibniz (1646-1716) Leibniz complained that All who are occupied with the reading or writing of scientific literature have assuredly very often felt the want of a common scientific language, and regretted the great loss of time and trouble caused by the multiplicity of languages employed in scientific literature Leibniz dream: Characteristica Universalis : a universal, formal, language of reasoning in which any assertion can be proved or disproved automatically The dream s end: Turing and Goedl in 1930 s. Internal Data Representation, Shimon Schocken, IDC Herzliya, www.introcs.com slide 1