Inf2C - Computer Systems Lecture 2 Data Representation

Similar documents
COMP2611: Computer Organization. Data Representation

Floating Point Arithmetic

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

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

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

ECE232: Hardware Organization and Design

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

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

COMP Overview of Tutorial #2

CS 101: Computer Programming and Utilization

Chapter 4: Data Representations

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

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

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

Introduction to Computers and Programming. Numeric Values

3.5 Floating Point: Overview

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

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

M1 Computers and Data

COMP2121: Microprocessors and Interfacing. Number Systems

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

Data Representation 1

Numeric Encodings Prof. James L. Frankel Harvard University

Chapter 2. Data Representation in Computer Systems

CO212 Lecture 10: Arithmetic & Logical Unit

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

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. Both numbers are positive

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

CHW 261: Logic Design

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

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

Chapter Three. Arithmetic

Module 2: Computer Arithmetic

MACHINE LEVEL REPRESENTATION OF DATA

FLOATING POINT NUMBERS

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

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

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

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

DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

Data Representation. DRAM uses a single capacitor to store and a transistor to select. SRAM typically uses 6 transistors.

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

Number System. Introduction. Decimal Numbers

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

Chapter 3: Arithmetic for Computers

Topic Notes: Bits and Bytes and Numbers

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

unused unused unused unused unused unused

Divide: Paper & Pencil

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

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

Final Labs and Tutors

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Floating Point Numbers. Lecture 9 CAP

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Floating Point Numbers

Representing Information. Bit Juggling. - Representing information using bits - Number representations. - Some other bits - Chapters 1 and 2.3,2.

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

CS101 Lecture 04: Binary Arithmetic

Chapter 2 Data Representations

T02 Tutorial Slides for Week 2

Topic Notes: Bits and Bytes and Numbers

Computer Arithmetic Ch 8

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Computer Arithmetic Ch 8

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Chapter 4. Operations on Data

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

Number Systems CHAPTER Positional Number Systems

Representing and Manipulating Floating Points. Jo, Heeseung

15213 Recitation 2: Floating Point

Integers and Floating Point

Data Representations & Arithmetic Operations

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Representing and Manipulating Floating Points

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

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

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

Computer Organisation CS303

CPS 104 Computer Organization and Programming Lecture-2 : Data representations,


Foundations of Computer Systems

Organisasi Sistem Komputer

Computer Organization

Number Systems and Computer Arithmetic

Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Signed umbers. Sign/Magnitude otation

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

More about Binary 9/6/2016

Data Representation Type of Data Representation Integers Bits Unsigned 2 s Comp Excess 7 Excess 8

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Chapter 2 Bits, Data Types, and Operations

Transcription:

Inf2C - Computer Systems Lecture 2 Data Representation Boris Grot School of Informatics University of Edinburgh

Last lecture Moore s law Types of computer systems Computer components Computer system stack Inf2C Computer Systems - 2017-2018. Boris Grot 2

Announcements Tutorials start next week If you need to change your tutorial group, contact the ITO Piazza (online discussion forum) is up and active USE IT! Inf2C Computer Systems - 2017-2018. Boris Grot 3

Lecture 2: Data Representation The way in which data is represented in computer hardware affects complexity of circuits cost speed reliability Must consider how to design hardware for Storing data: memories Manipulating data: processing (e.g., adders, multipliers) Inf2C Computer Systems - 2017-2018. Boris Grot 6

Lecture outline The bit atomic unit of data Representing numbers Integers Floating point Representing text Inf2C Computer Systems - 2017-2018. Boris Grot 7

The bit Information represented as sequences of symbols Humans use letters, numerals, punctuation, whitespace Computers use just 0s and 1s, bits Bit an acronym for Binary digit Advantages: easy to do computation, very reliable, simple & reusable circuits Disadvantages: little information per bit à must use many bits. 512 1 0000 0000, A 0100 0001 Inf2C Computer Systems - 2017-2018. Boris Grot 8

Natural numbers representation Non-negative (unsigned) integers are very simple to represent in binary Most significant bit Least significant bit MSB LSB Bit position n-1 n-2 1 0 Binary: Decimal: *2 n-1 + *2 n-2 + *2 1 + *2 0 Inf2C Computer Systems - 2017-2018. Boris Grot 9

Basic operations Addition, subtraction with unsigned binary numbers is easy: 1 1 1 1 01101 +01011 11000 24 11 13 0 0 1 0 01101 01011 00010 2 Inf2C Computer Systems - 2017-2018. Boris Grot 10

Fixed bit-length arithmetic Hardware cannot handle infinitely long bit sequences We end up with a few fixed-size data types Byte: always 8 bits Word: the typical unit of data on which a processor operates (32 or 64 bits most common today) Overflow happens when a result does not fit Numbers wrap-around when they become too large Arithmetic is modulo 2 N, where N=number of bits Inf2C Computer Systems - 2017-2018. Boris Grot 11

What about negative numbers? Sign-magnitude representation: Use 1 st bit (MSB) as the sign 1 à negative, 0 à positive 0010 2 1010-2 Complicates addition and subtraction The actual operation depends on the sign Has positive and negative zero 0000 0 1000-0 Better way: 2 s complement representation Inf2C Computer Systems - 2017-2018. Boris Grot 12

Two s complement: the intuition Want: X + (-X) = 0 Insight: don t need the full sum to be 0 Only need the bits that can be represented within a computer s fixed width to be 0 Approach: Represent the negation of X as 2 N -X Recall: largest number represented with N bits: 2 N -1 Then: X + (-X) = X + (2 N -X) = 2 N Note that N lowest bits are all 0 Inf2C Computer Systems - 2017-2018. Boris Grot 13

Two s complement: example Given: 3-bit fixed width (N=3) X = 2 (decimal) à 0 1 0 (binary) 2 N = 8 (dec) à 1 0 0 0 (bin) -X = 2 N X = 8 2 = 6 (dec) à 1 1 0 (bin) Check: X + (-X) = 0 1 0 + 1 1 0 = 1 0 0 0 Inf2C Computer Systems - 2017-2018. Boris Grot 14

Efficiently computing 2 s complement EASY! Flip the bits and add 1 Example: X = 0 1 0 (bin) à 2 (dec) Flip the bits: 1 0 1 Add 1: 1 1 0 (bin) à -X Inf2C Computer Systems - 2017-2018. Boris Grot 15

2 s complement details The MSB is the sign A B = A + 2 s complement of B Arithmetic operations do not depend on the operands signs Range is asymmetric: 2 n-1 to 2 n-1-1 There are two kinds of overflows: Positive overflow produces a negative number Negative underflow produces a positive number Inf2C Computer Systems - 2017-2018. Boris Grot 16

Converting between data types Converting a 2 s complement number from a smaller to a larger representation is done by sign extension Example: from byte to short (16 bits): 2 = 0 0 0 0 0 0 1 0???????? 0 0 0 0 0 0 1 0-2 = 1 1 1 1 1 1 1 0???????? 1 1 1 1 1 1 1 0 2 = 00000010 0000000000000010-2 = 11111110 1111111111111110 (byte) (short) (byte) (short) Inf2C Computer Systems - 2017-2018. Boris Grot 17

Shifting Shifting: move the bits of a data type left or right Data bits falling off the edge are lost For left shifts, 0s fill in the empty bit places For right shifts, two options: Fill with 0 (logical shift): for non-numerical data Fill with MSB (arithmetic shift): for 2 s complement numbers Shift left by n is equivalent to multiplying by 2 n Shift right by n is equivalent to dividing by 2 n and rounding towards - Example 6 = 0 0 0 0 0 1 1 0 >> 2 à 0 0 0 0 0 0 0 1 = 1-6 = 1 1 1 1 1 0 1 0 >> 2 à 1 1 1 1 1 1 1 0 = -2 Inf2C Computer Systems - 2017-2018. Boris Grot 18

Hexadecimal notation Binary numbers (and other binary-encoded information) are too long and tedious for us to use Solution: use a more compact encoding Hexadecimal (base 16) is most common Hex digits: 0-9 and A-F A=10 dec, B=11,, F=15 Conversion to/from binary is very easy: Every 4 bits correspond to 1 hex digit: 1 1 1 1 1 0 0 0 F(15) 8 = 0xF8 Hex is just a convenience for humans Computers use the binary form Inf2C Computer Systems - 2017-2018. Boris Grot 19

Real numbers - floating point Java s float (32 bits) double (64 bits) Binary representation: example 0.75 in base 10 0.11 in base 2 (2-1 + 2-2 = 0.5 + 0.25 = 0.75) Inf2C Computer Systems - 2017-2018. Boris Grot 20

Real numbers - floating point Java s float (32 bits) double (64 bits) Binary representation: example 0.75 in base 10 0.11 in base 2 (2-1 + 2-2 = 0.5 + 0.25 = 0.75) Normalization: Mantissa (aka significand) 0.11 1.1x2-1 exponent implicit (always 1) Inf2C Computer Systems - 2017-2018. Boris Grot 21

Why normalize? Three reasons: 1. Simplifies machine representation (don t need to represent the fraction separator) 2. Simplifies comparisons Which one is bigger: 0.0000101 or 0.000001? 3. Is more compact for very small/large numbers E.g., 0.0000000000000001 = 1.0 x 2-16 or can be made more compact (by rounding fraction) Inf2C Computer Systems - 2017-2018. Boris Grot 22

Floating point conversion example #1 Convert the number 25 to floating point with normalization 1) 25 in base 10 11001 in base 2 2) 11001 to normalized floating point 1.1001x2 4 Understand that: The number is normalized 1001 is mantissa (aka significand) 4 is exponent sign is + (implicit here) Inf2C Computer Systems - 2017-2018. Boris Grot 23

IEEE 754 Floating Point standard Need a standard to represent and compute with fixed-width floats 32 bit representation: 31 30 23 22 0 sign (s) exponent (exp) (-1) s x (1.m) x 2 exp-127 mantissa (m) Bias e.g., (0.75) 10 (0.11) 2 (1.1x2-1 ) 2 s = 0, m = 1, exp = 126 0 01111110 10000000000000000000000 Note: representation does NOT use 2 s complement Inf2C Computer Systems - 2017-2018. Boris Grot 24

IEEE 754 Floating Point standard Need a standard to represent and compute with fixed-width floats 32 bit representation: 31 30 23 22 0 sign (s) exponent (exp) (-1) s x (1.m) x 2 exp-127 mantissa (m) Bias e.g., (0.75) 10 (0.11) 2 (1.1x2-1 ) 2 s = 0, m = 1, exp = 126 0 01111110 10000000000000000000000 64 bit representation: exponent = 11 bits; mantissa= 52 bits Inf2C Computer Systems - 2017-2018. Boris Grot 25

IEEE 754 Floating Point standard Why bias? Avoids the complexity of +/- exponents Simplifies relative ordering of FP numbers Note: processors usually have specialized floating point units to perform FP arithmetic Inf2C Computer Systems - 2017-2018. Boris Grot 26

IEEE 754 floating point conversion #2 Example: Convert 23.5 (decimal) to IEEE 754 floating point Start: 23 in base 10 10111 in base 2 Inf2C Computer Systems - 2017-2018. Boris Grot 27

IEEE 754 floating point conversion #2 Example: Convert 23.5 (decimal) to IEEE 754 floating point Start: 23 in base 10 10111 in base 2 1) 23.5 in base 10 10111.1 in base 2 2) 10111.1 to normalized floating point 1.01111x2 4 3) S = 0 M = 01111 is mantissa (remember: 1. is implicit) Exp = 4+127 = 131 in base 10 1000 0011 in base 2 31 30 22 0 sign (s) exponent (exp) 23 Pad with 0s mantissa (m) Inf2C Computer Systems - 2017-2018. Boris Grot 28

IEEE 754 Floating Point notation Exponent Mantissa Meaning 0 0 0 1-254 Anything Floating point number 255 0 Infinity (signed) 255 Non-zero Not-a-number (NaN) 32-bit representation Inf2C Computer Systems - 2017-2018. Boris Grot 29

Representing characters Characters need to be encoded in binary too Operations on characters have simpler requirements than on numbers, so the encoding choice is not crucial Most common representation is ASCII Each character is held in a byte E.g. 0 is 0x30, A is 0x41, a is 0x61 Java uses Unicode which can encode characters from many (all?) languages 16 bits per character required Inf2C Computer Systems - 2017-2018. Boris Grot 30

Representing strings Words, sentences, etc. are just strings of characters How is the end of a string identified? No common standard exists. Different programming languages use different encodings In C: a special character, encoded as 0x00 Also called NULL character In Java: string length is kept with the string itself string is an object and length is one of its member variables Inf2C Computer Systems - 2017-2018. Boris Grot 31

Summary Computers use binary representation Signed numbers: sign-magnitude vs 2 s complement Floating point Characters and strings Inf2C Computer Systems - 2017-2018. Boris Grot 32