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

Similar documents
Data Representation COE 301. Computer Organization Prof. Muhamed Mudawar

Number Systems. Both numbers are positive

COMP2611: Computer Organization. Data Representation

Chapter 2. Data Representation in Computer Systems

CHW 261: Logic Design

Digital Systems COE 202. Digital Logic Design. Dr. Muhamed Mudawar King Fahd University of Petroleum and Minerals

Number representations

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

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

Signed umbers. Sign/Magnitude otation

Number System. Introduction. Decimal Numbers

Digital Fundamentals

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

Data Representation 1

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

Module 2: Computer Arithmetic

MACHINE LEVEL REPRESENTATION OF DATA

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

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

COMP2121: Microprocessors and Interfacing. Number Systems

Number Systems CHAPTER Positional Number Systems

Chapter 2 Bits, Data Types, and Operations

Final Labs and Tutors

Computer Architecture and Organization. Chapter 2 Data Representation!

Chapter 2 Bits, Data Types, and Operations

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

COMP Overview of Tutorial #2

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Data Representation and Binary Arithmetic. Lecture 2

Objectives. Connecting with Computer Science 2

Divide: Paper & Pencil

Number Systems & Encoding

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

Internal Data Representation

BINARY SYSTEM. Binary system is used in digital systems because it is:

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. Data Representation in Computer Systems

1.1. INTRODUCTION 1.2. NUMBER SYSTEMS

Level ISA3: Information Representation

Chapter 2 Data Representations

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

DIGITAL SYSTEM FUNDAMENTALS (ECE 421) DIGITAL ELECTRONICS FUNDAMENTAL (ECE 422) COURSE / CODE NUMBER SYSTEM

Data Representations & Arithmetic Operations

CMSC 313 Lecture 03 Multiple-byte data big-endian vs little-endian sign extension Multiplication and division Floating point formats Character Codes

Chapter 2: Number Systems

Computer Organisation CS303

Chapter 2 Bits, Data Types, and Operations

CO212 Lecture 10: Arithmetic & Logical Unit

Inf2C - Computer Systems Lecture 2 Data Representation

Introduction to Computers and Programming. Numeric Values

Electronic Data and Instructions

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Fundamentals of Programming (C)

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

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

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

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

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

Number Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010

CHAPTER 2 Data Representation in Computer Systems

ECE331: Hardware Organization and Design

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

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

CS 101: Computer Programming and Utilization

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

Chapter 3. Information Representation

ECE260: Fundamentals of Computer Engineering

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

Beyond Base 10: Non-decimal Based Number Systems

CHAPTER 2 Data Representation in Computer Systems

Chapter 2. Data Representation in Computer Systems. Objectives (1 of 2) Objectives (2 of 2)

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

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

Numeral Systems. -Numeral System -Positional systems -Decimal -Binary -Octal. Subjects:

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

Characters, Strings, and Floats

CS101 Lecture 04: Binary Arithmetic

Floating Point Arithmetic

QUIZ ch.1. 1 st generation 2 nd generation 3 rd generation 4 th generation 5 th generation Rock s Law Moore s Law

Introduction to Computer Science-103. Midterm

Lecture 2: Number Systems

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

UNIT-III COMPUTER ARTHIMETIC

Chapter 3: Arithmetic for Computers

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

±M R ±E, S M CHARACTERISTIC MANTISSA 1 k j

Lecture (02) Operations on numbering systems

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

Chapter 2 Bits, Data Types, and Operations

Digital Fundamentals

Chapter 3 Arithmetic for Computers (Part 2)

Computer Organization

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

Integers and Floating Point

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

IEEE Standard for Floating-Point Arithmetic: 754

Chapter 2 Bits, Data Types, and Operations

CS & IT Conversions. Magnitude 10,000 1,

Beyond Base 10: Non-decimal Based Number Systems

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

Transcription:

Data Representation ti and Arithmetic for Computers Rui Wang, Assistant professor Dept. of Information and Communication Tongji University it Email: ruiwang@tongji.edu.cn

Questions What do you know about other number system? (e.g. Decimal base 10) What kind of number system is used by the computer? Why? 2

Positional Number Systems Different Representations of Natural Numbers 27 decimal number 11011 2 base-2 or binary number Examples: (11011) 2 = 1 2 4 + 1 2 3 + 0 2 2 + 1 2 + 1 = 27 (2103) 4 = 2 4 3 + 1 4 2 + 0 4 + 3 = 147 3

The binary system is also called the base-2 system. (101100.011) 011) Our decimal system is the base-10 system. It uses powers of 10 for each position in a number. (975.3) Any integer quantity can be represented exactly using any base. (3077 octal or 2BAD hex) ) 4

Weighted Position Code The base of a number system defines the range of possible values that a digit may have: 0 9 for decimal; 0,1 for binary. The general form for determining the decimal value of a number is given by: Example: 541.25 10 = 5 10 2 +4 10 1 +1 10 0 +2 10-1 +5 10-2 10 = (500) 10 + (40) 10 + (1) 10 + (2/10) 10 + (5/100) 10 = (541.25) 10 5

Binary representation Why is binary representation used by computer? 1 1=11 Easy to implement 0 0=0 Simple computation rules 1 0=0 1=0 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 1 6

Binary Numbers Each binary digit (called bit) is either 1 or 0 Bits have no inherent meaning, can represent Unsigned and signed integers Characters Floating-point numbers Images, sound, etc. 7 6 5 4 3 2 1 0 1 0 0 1 1 1 0 1 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 7

A bit is the most basic unit of information in a computer. It is a state of on or off in a digital circuit. Sometimes they represent high or low voltage A byte is a group of eight bits. It is the smallest possible addressable unit of computer storage. 8

A word is a contiguous group of bytes. Words can be any number of bits or bytes. Word sizes of 16, 32, or 64 bits are most common. 9

Octal & Hexadecimal numbers Decimal Binary Octal Hexadecimal 00 0000 00 0 01 0001 01 1 02 0010 02 2 03 0011 03 3 04 0100 04 4 05 0101 05 5 06 0110 06 6 07 0111 07 7 08 1000 10 8 09 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F 10

Converting base-r to decimal 10101(B)=2 4 +2 2 +2 0 =16+4+1=21 101.11(B)=2 2 +2 0 +2-1 +2-2 =5.75 101(O)=8 2 +8 0 =64+1=65 71(O)=7 8 1 +1 8 0 =57 101A(H)=1 16 3 + 1 16 0 +10=4106 11

Converting decimal to binary Example: Convert 23.375375 10 to base 2. Start by converting the integer portion: 12

Now, convert the fraction: Putting them all together, 23.375 375 10 = 10111.011 2 13

Repeatedly divide the decimal integer by 2 Each remainder is a binary digit 37 = (100101) 2 stop when quotient is zero 14

Nonterminating Base 2 Fraction We can t always convert a terminating base 10 fraction into an equivalent terminating base 2 fraction: 15

Decimal to Octal & Hexadecimal 100(D)=144(O)=64(H) 8 Octal number 100 8 12 4 8 1 0 Hexadecimal number 16 100 16 6 4 1 4 0 6 16

Base-k to decimal Decimal to base-k Integer portion: divided by k and take the remainders Fraction: multiplied by k and take the integer parts 17

Converting binary to octal / hexadecimal numbers Each octal (hexadecimal) digit corresponds to 3 (4) binary bits 1 101 101 110.110 101(B)= 1556.65(O) 1 5 5 6 6 5 11 0110 1110.1101 01(B)=36F.D4(H) 3 6 F D 4 18

Some Excercises Write down the computation steps (110111.01)B= ( )D (422)D=( )H (52)O=( )B (01101101)=( )H 19

Answers to Exercises (110111.01)B=32+16+4+2+1+0.25=(55.25)D (422)D=(1A6)H 52 8 = (101 2 )(010 2 ) =101010 2 01101101 2 = (0110 2 )(1101 2 ) = 6D 16 20

Classification of numbers Numbers Fixed Point Floating Point Signed Unsigned Signed Unsigned 21

Fixed Point Numbers Each number has exactly the same number of digits, and the point is always in the same place For example: 0.23, 5.12, 9.11(decimal); each number has 3 digits, and the decimal point is located two places from the right 11.10, 01.10, 00.11(binary) 4 binary digits and the binary point is in the middle 22

Unsigned Integer Storage Sizes Half Word Byte 8 16 Storage Sizes Word 32 Double Word 64 Storage Type Unsigned Range Powers of 2 Byte 0 to 255 0 to (2 8 1) Half Word 0 to 65,535 0 to (2 16 1) Word 0 to 4,294,967,295 967 295 0to(2 32 1) Double Word 0 to 18,446,744,073,709,551,615 0 to (2 64 1) What is the largest 20-bit unsigned integer? Answer: 2 20 1 = 1,048,575 23

Binary Addition Start with the rightmost bit Add each pair of bits Include the carry in the addition, if present carry 1 1 1 1 0 0 1 1 0 1 1 0 (54) + 0 0 0 1 1 1 0 1 (29) 0 1 0 1 0 0 1 1 (83) bit position: 7 6 5 4 3 2 1 0 24

Hexadecimal Addition Start with the rightmost hexadecimal digits Let Sum = summation of two hex digits If Sum is greater than or equal to 16 Sum = Sum 16 and Carry = 1 Example: carry: 1 1 1 Subtraction? 1C37286A 9 3 9 5 E 8 4 B Since 21 16 + A + B = 10 + 11 = 21 AFC D 1 0 B 5 Sum = 21 16 = 5 Carry = 1 25

Signed Integers 8-bit Binary Unsigned Signed How to represent a signed value value value number? Signed magnitude Sign bit: 0-positive 2's complement (computation) Divide the range of values into 2 equal parts First part corresponds to the positive numbers ( 0) Second part correspond to the negative numbers (< 0) Sign bit: 1-negative 00000000 0 0 00000001 1 +1 00000010 2 +2......... 01111110 126 +126 01111111 127 +127 10000000 128-128 10000001 129-127......... 11111110 254-2 11111111 255-1 26

Sign Bit Highest bit indicates the sign 1 = negative 0 = positive Sign bit 1 1 1 1 0 1 1 0 Negative 0 0 0 0 1 0 1 0 Positive For Hexadecimal Numbers, check leftmost digit If leftmost digit is > 7, then value is negative Examples: 8A and C5 are negative bytes B1C42A00 is a negative word (32-bit signed integer) 27

Ranges of Signed Integers For n-bit signed integers: Range is -2 n 1 1 to (2 n 1 1 1) Positive range: 0 to 2 n 1 1 Negative range: -2 n 1 to -1 Storage Type Unsigned Range Powers of 2 Byte 128 to +127 2 7 to (2 7 1) Half Word 32,768 to +32,767 2 15 to (2 15 1) Word 2,147,483,648 to +2,147,483,647 2 31 to (2 31 1) Double Word 9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 2 63 to (2 63 1) Practice: What is the range of signed values that may be stored in 20 bits? 28

Signed Magnitude Also know as sign and magnitude, the leftmost t bit is the sign (0 = positive, 1 = negative) and the remaining bits are the magnitude. Example: +25 10 = 00011001 2-25 10 = 10011001 2 Two representations for zero: +0 = 00000000 2, -0 = 10000000 2. Example: (-5)+4 10000101+00000100=10001001 (-9)D? Solution: 2 s complement representation 29

Two s Complement One representation for zero: +0 = 00000000 2, -0 = 00000000 2. Positive number: 2 s complement = original number Negative number starting value (negative numbers) 10100100 = -36 step1: keep the sign bit and reverse other bits 11011011 step 2: add 1 to the value from step 1 + 1 sum = 2's complement representation 11011100 = -92 30

Binary Subtraction When subtracting A B, convert B to its 2's complement Add A to ( B), if the result is negative (leftmost bit=1), compute 2 s complement again borrow: 1 1 1 carry: 1 1 1 1 0 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 + 00111010 0 1 1 0 1 0 11000110 1 0 0 1 1 0 (2's complement) 0 0 0 1 0 0 1 1 0 0 0 1 0 0 1 1 (same result) Final carry is ignored -5+4-5:(10000101)B (11111010+1=11111011)2 s complement 11111011+00000100=11111111 (10000000+1=10000001) -1 31

-5+8-5 (11111010+1=11111011) 1 11111011) 11111011+00001000=00000011 3 Some exercise -9-5 -7+11-15+7 32

33

Overflow When two numbers are added that have large magnitudes and the same sign, an overflow will occur if the result is too large to fit inthe number of bits used in the representation The result should be (+130)10,which h is bigger than +127 (maximum for 8-bit) 34

Overflow Overflow occurs when Adding two positive numbers and the sum is negative Adding two negative numbers and the sum is positive If the numbers being added are of opposite signs, then an overflow will never occur 35

Sign Extension When convert low-bit version number to high-bit version, sign extension is needed Example: convert 16-bit 2 to 32-bit binary number 16-bit 2 0000 0000 0000 0010 32-bit 0000 0000 0000 0000 0000 0000 0000 0010 36

Sign Extension Step 1: Move the number into the lower-significant bits Step 2: Fill all the remaining higher bits with the sign bit This will ensure that both magnitude and sign are correct Examples Sign-Extend 10110011 to 16 bits 10110011 = -77 11111111 10110011 = -77 Sign-Extend 01100010 to 16 bits 01100010 = +98 00000000 01100010 = +98 Infinite it 0s can be added d to the left of a positive number Infinite 1s can be added to the left of a negative number 37

Fixed Point Multiplication and Division Multiplication of unsigned binary integers is handled similar to the way it is carried out by hand for decimal numbers When two unsigned n-bit numbers are multiplied, the result can be as large as 2n bits When two signed n-bit numbers are multiplied, the result can be as large as only 2(n-1)+1 = (2n-1) bits, because this is equivalent to multiplying two (n-1)-bit unsigned numbers and then introducing the sign bit 38

Multiplication & division for unsigned integers 39

Signed Fixed Point Multiplication If we apply the multiplication and division methods described in the previous sections to signed integers, then we will run into some trouble 2 s complement 40

Solution: each partial product is extended to the width of the result, and only the rightmost eight bits of the result are retained. If both operands are negative, then the signs are extended for both operands, again retaining only the rightmost eight bits of the result. The first operand is 11111111(2 s complement)-->(-1)10 41

Multiply unit of MIPS contains two 32-bit registers called hi and lo. These are not general purpose registers When two 32-bit operands are multiplied, hi and lo hold the 64 bits of the result. Bits 32 through 63 are in hi and bits 0 through 31 are in lo. 42

Move the result of a multiplication into a general purpose p register The hi and lo registers cannot be used with any of the other arithmetic or logic instructions mfhi $d # $d < hi. Move From Hi mflo $d # $d < lo. Move From Lo 43

What went wrong is that the sign bit did not get extended to the left of the result This is not a problem for a positive result because the high order bits default to 0, producing the correct sign bit 0 44

Signed Fixed Point division Signed division is more difficult. We will not explore the methods here, but as a general technique, we can convert the operands into their positive forms, perform the division, and then convert the result into its true signed form as a final step 45

Division For N-digit integer division there are two results, an N- digit quotient and an N-digit remainder With 32-bit operands there will be (in general) two 32- bit results. MIPS uses the hi and lo registers for the results 46

Character Storage Character sets Standard ASCII: 7-bit character codes (0 127) Extended ASCII: 8-bit character codes (0 255) Unicode: 16-bit character codes (0 65,535) Unicode standard represents a universal character set Defines codes for characters used in all major languages Used in Windows-XP: each character is encoded as 16 bits UTF-8: variable-length length encoding used in HTML Encodes all Unicode characters Uses 1 byte for ASCII, but multiple bytes for other characters Null-terminated String Array of characters followed by a NULL character 47

Printable ASCII Codes 0 1 2 3 4 5 6 7 8 9 A B C D E F 2 space! " # $ % & ' ( ) * +, -. / 3 0 1 2 3 4 5 6 7 8 9 : ; < = >? 4 @ A B C D E F G H I J K L M N O 5 P Q R S T U V W X Y Z [ \ ] ^ _ 6 ` a b c d e f g h i j k l m n o 7 p q r s t u v w x y z { } ~ DEL Examples: ASCII code for space character = 20 (hex) = 32 (decimal) ASCII code for 'L' =4C (hex) = 76 (decimal) ASCII code for 'a' = 61 (hex) = 97 (decimal) 48

Control Characters The first 32 characters of ASCII table are used for control Control character codes = 00 to 1F (hexadecimal) Examples of Control Characters Character 0 is the NULL character used to terminate a string Character 9 is the Horizontal Tab (HT) character Character 0A (hex) = 10 (decimal) is the Line Feed (LF) Character 0D (hex) = 13 (decimal) is the Carriage Return (CR) The LF and CR characters are used together They advance the cursor to the beginning of next line One control character appears at end of ASCII table Character 7F (hex) is the Delete (DEL) character 49

Question: how to represent large numbers (e.g. 2 80 )? Solution: Floating point numbers 50

Floating-Point Representation Computers use a form of scientific notation for floating-point representation Numbers written in scientific notation have three components: 51

Computer representation of a floating-point number consists of three fixed-size fields: This is the standard arrangement of these fields. 52

The one-bit sign field is the sign of the stored value. The size of the exponent field, determines the range of values that can be represented. The size of the significand determines the precision of the representation. 53

The IEEE-754 single precision floating point standard uses an 8-bit exponent and a 23-bit significand. ifi The IEEE-754 double precision standard uses an 11-bit exponent and a 52-bit significand. ifi 54

The significand of a floating-point number is always preceded by an implied binary point. Thus, the significand always contains a fractional binary value. The exponent indicates the power of 2 to which the significand is raised. 55

Base 10 Floating Point Numbers Floating gpoint numbers allow very large and very small numbers to be represented using only a few digits, at the expense of precision. The precision is primarily determined by the number of digits in the fraction (or significand, which has integer and fractional parts), and the range is primarily determined by the number of digits in the exponent. Example (+6.023 10 23 ): 56

Normalization The base 10 number 254 can be represented in floating point form as 254 10 0, or equivalently as: 25.4 10 1, or 254 2.54 10 2, or.254 10 3, or.0254 10 4, or infinitely many other ways, which creates problems when making comparisons, with so many representations of the same number. Floating point numbers are usually normalized, in which the radix point is located in only one possible position for a given number. Usually, but not always, the normalized representation places the radix point immediately to the left of the leftmost, nonzero digit in the fraction, as in:.254 10 3. 57

IEEE-754 Floating Point Formats 58

IEEE 754 uses a bias of 127 for single precision (1023 for double precision) Biased exponent means that the value represented by a floating-point number is (-1) S (1+d) 2 (p+b) S: value of sign bit d: value of fraction field p: value of exponent field B: bias value (127 / 1023) 59

Number=±1.xx xxx 2 xxx 2 ±p d 1 bit 8/11 bits 23/52 bits Sign E F p+127 P+1023 p+bias d Fraction E: data range F: data precision i

Examples:single precision 26.0D=11010.0B=+1.10100*2 4 +127=131=10000011B 0 10000011 10100000000000000000000-2.5D=-10.1B=-1.01*2 1 +127=128=10000000B 1 10000000 01000000000000000000000

Thank you very much! 62