Data Structures Mohammed Thajeel To the second year students

Slides:



Advertisements
Similar presentations
Lecture - 2 Number systems and computer data formats
Advertisements

©Brooks/Cole, 2003 Chapter 3 Number Representation.
Floating Point Numbers
Signed Numbers.
Floating Point Numbers
Binary Number Systems.
Binary Representation and Computer Arithmetic
The Binary Number System
Data Representation Number Systems.
Simple Data Type Representation and conversion of numbers
Data Representation – Binary Numbers
Computer Arithmetic Nizamettin AYDIN
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Lecture Overview Introduction Positional Numbering System
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 3 Number Representation. Convert a number from decimal to binary notation and vice versa. Understand the different representations of an integer.
Number Systems & Operations
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Introduction To Number Systems Binary System M. AL-Towaileb1.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
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.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Floating Point Numbers Dr. Mohsen NASRI College of Computer and Information Sciences, Majmaah University, Al Majmaah
Chapter 9 Computer Arithmetic
William Stallings Computer Organization and Architecture 8th Edition
MATH Lesson 2 Binary arithmetic.
Floating Point Representations
CS2100 Computer Organisation
Department of Computer Science Georgia State University
Data Representation COE 308 Computer Architecture
Dr.Faisal Alzyoud 2/20/2018 Binary Arithmetic.
Programming and Data Structure
Data Representation ICS 233
Lec 3: Data Representation
Data Representation.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Number Representation
Introduction To Computer Science
Number Systems and Binary Arithmetic
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
Data Representation Binary Numbers Binary Addition
Numbers in a Computer Unsigned integers Signed magnitude
William Stallings Computer Organization and Architecture 7th Edition
IT 0213: INTRODUCTION TO COMPUTER ARCHITECTURE
Topic 3d Representation of Real Numbers
Data Representation COE 301 Computer Organization
Chapter 2 Bits, Data Types & Operations Integer Representation
Data Representation Data Types Complements Fixed Point Representation
Digital Logic & Design Lecture 02.
ECEG-3202 Computer Architecture and Organization
Number Representation
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Chapter 8 Computer Arithmetic
Data Representation ICS 233
Binary to Decimal Conversion
Prof. Giancarlo Succi, Ph.D., P.Eng.
Topic 3d Representation of Real Numbers
Chapter3 Fixed Point Representation
OBJECTIVES After reading this chapter, the reader should be able to :
Introduction To Number Systems
1.6) Storing Integer: 1.7) storing fraction:
Data Representation COE 308 Computer Architecture
Presentation transcript:

Data Structures Mohammed Thajeel 2016-2017 To the second year students AL- Furat AL - Awsat Technical University Karbala Technical Institute Department - Computer Systems Techniques Data Structures To the second year students Prepared by the lecturer: Mohammed Thajeel 2016-2017

Lecture (2+3) Outline Binary and Decimal Numbers Introduction to Primitive data structures Methods for representing integers Signed-Magnitude Representation Two’s-Complement Representation Overflow Methods for representing Real Numbers IEEE Floating Point Format Decimal FP to IEEE standard Conversion

Binary and Decimal Numbers A binary number is a number that includes only ones and zeroes. The number could be of any length The following are all examples of binary numbers: 0, 10101, 1, 0101010, 10 Another name for binary is base-2 and written (1010)2. A decimal numbers are the numbers that we are used to seeing are called decimal numbers. decimal numbers consist of the digits from 0 (zero) through 9. The following are examples of decimal numbers: 3, 76, 0, 329, 1 Another name for decimal numbers are base-10 and written (1045)10. Data Structures : Lecture (2&3) page (1) Prepared by : Mohammed Thajeel

Decimal to Binary Conversion The Process : Successive Division Divide the Decimal Number by 2; the remainder is the Least significant bit of Binary Number. If the quotient zero, the conversion is complete; else repeat step (a) using the quotient as the Decimal Number. The new remainder is the next most significant bit of the Binary Number. Example 1: Convert the decimal number 610 into its binary equivalent? Answer:    610 = 1102 Data Structures : Lecture (2&3) page (2) Prepared by : Mohammed Thajeel

Subtraction Rules (1-bit) Binary Addition and Subtraction Rules Addition Rules (1-bit) 1) Carry-out 1 + 2) 3) 4) Subtraction Rules (1-bit) 1) 2) 3) 4) 1 - Note: What we see in subtraction rule ( 4 ) is the end result of a borrow process Data Structures : Lecture (2&3) page (3) Prepared by : Mohammed Thajeel

Binary Addition and Subtraction Examples 1) 110 1010 100 10 112 10102 + 101 2) 3) 4) - - 11012 1001 10 100 Data Structures : Lecture (2&3) page (4) Prepared by : Mohammed Thajeel

Floating Point Numbers In the decimal system, a decimal point (radix point) separates the integer numbers from the fractional part. Examples: 37.25 ( integer =37, fraction = 25). Binary Equivalent: The binary equivalent of a floating point number can be determined by computing the binary representation for each part separately. 1) For the integer part: - Use the division method previously learned. 2) For the fractional part: - Use the multiplication method (to be shown next) Data Structures : Lecture (2&3) page (5) Prepared by : Mohammed Thajeel

Fractional Part – Multiplication Method   0.5 save the integer part = 0 1.0 save the integer part = 1 0.01  0.2510 = 0.012 Data Structures : Lecture (2&3) page (6) Prepared by : Mohammed Thajeel

Fractional Part – Multiplication Method cont’d Example 3: Find the binary equivalent of 0.625 10 ? Answer: step 1: 0.625 x 2 = Step 2: 1.25 save the integer part = 1 0.25 x 2 = 0.50 save the integer part = 0 0.50 x 2 = 1.0 save the integer part = 1 stopped because fractional part becomes zero. 0.101  0.62510 = 0.1012 Data Structures : Lecture (2&3) page (7) Prepared by : Mohammed Thajeel

Binary to Decimal Conversion The Process : Weighted Multiplication Multiply each bit of the Binary Number by it corresponding bit-weighting factor (i.e. Bit-0→20=1; Bit-1→21=2; Bit-2→22=4; etc). Sum up all the products in step (a) to get the Decimal Number. Example 4: Convert the binary number (0110) into its decimal equivalent? Answer: 1 23 22 21 20 8 4 2 + = 610 Bit-Weighting Factors  01102 = 610 Data Structures : Lecture (2&3) page (8) Prepared by : Mohammed Thajeel

Exercises Convert the binary number (10010) into its decimal equivalent. Convert the decimal number (158) into its binary equivalent. Convert the decimal number (13) into its binary equivalent. Convert the decimal number (12.3125) into its binary equivalent. Find the binary equivalent of (34.625) . Solutions: 18 10 1 0 0 1 1 1 1 0 2 1 1 0 1 2 1100.0101 2 100010.101 2 Data Structures : Lecture (2&3) page (9) Prepared by : Mohammed Thajeel

Introduction to Primitive data structures Abstract data types are divided into two categories, primitive data types and user-defined data types: Primitive data types: is defined by the programming language, such as the data types you learned about in the previous Lecture as (Int, Float, … etc), Some programmers call these built-in data types. You determine the amount of memory to reserve by determining the appropriate abstract data type group to use and then deciding which abstract data type within the group is right for the data. Data Structures : Lecture (2&3) page (10) Prepared by : Mohammed Thajeel

Introduction to Primitive data structures cont’d There are four data type groups: Integer Stores whole numbers and signed numbers. Great for storing the number of dollars in your wallet when you don’t need a decimal value. Floating-point Stores real numbers (fractional values). Perfect for storing bank deposits where pennies (fractions of a dollar) can quickly add up to a few dollars. Character Stores a character. Ideal for storing names of things. String Boolean Stores a true or false value. The correct choice for storing a yes or no or true or false response to a question. Pointers Data Structures : Lecture (2&3) page (11) Prepared by : Mohammed Thajeel

Methods for representing integers The integer abstract data type group consists of four abstract data types used to reserve memory to store whole numbers: byte , short , int , and long. Depending on the nature of the data, sometimes an integer must be stored using a positive or negative sign, such as a +10 or –5. An integer that is stored with a sign is called a signed number; an integer that isn’t stored with a sign is called an unsigned number. Declaration in C++: Int x; An integer is a whole number which may be negative. The number must therefore be encoded in such a way as to tell if it is positive or negative, and to follow the rules of addition. Data Structures : Lecture (2&3) page (12) Prepared by : Mohammed Thajeel

Methods for representing integers cont’d Signed-Magnitude Two’s-Complement Data Structures : Lecture (2&3) page (13) Prepared by : Mohammed Thajeel

Signed-Magnitude Representation Signed-Magnitude (S-M) is a method for encoding signed integers. The Highest-weighted bit/Most Significant Bit ‘S’ is used to represent the sign. ‘1’ is used for a ‘-’ (negative sign), a ‘0’ for a ‘+’ (positive sign). The format of a S-M number in 8-bits is: where ‘S’ is the sign bit and the other 7 bits ‘M’ represent the magnitude. NOTE: positive numbers in S-M are simply their binary representation. 8 bit 1 2 3 4 5 6 7 position M S S-M Data Structures : Lecture (2&3) page (14) Prepared by : Mohammed Thajeel

Signed-Magnitude Representation cont’d  To compute integer values using Sign-Magnitude (S-M) representation: Take its absolute value (its positive equivalent). It is represented in base-2 using n-1 bits (convert it to binary number). If the number negative then flip the leftmost zero bit. Note: positive numbers in S-M are simply their binary representation (just step 2). Data Structures : Lecture (2&3) page (15) Prepared by : Mohammed Thajeel

Signed-Magnitude Representation cont’d Example 5: Express the integer number (-7) 10in sign-magnitude representation using 4-bits? Answer: Step 1: Step 2: Step 3: Take its absolute value ( |-7|=7 ). It is represented in base-2     Least significant bit         Most significant bit 710 = 0111 2 . If the number negative then flip the leftmost zero bit ( 1111 2 ).  -710 = 1111 2 Data Structures : Lecture (2&3) page (16) Prepared by : Mohammed Thajeel

Signed-Magnitude Representation cont’d Example 6:Represent the integer number (-9) 10in sign-magnitude representation using 8-bits? Answer:    -910 = 10001001 2 Data Structures : Lecture (2&3) page (17) Prepared by : Mohammed Thajeel

Signed-Magnitude Representation cont’d   Data Structures : Lecture (2&3) page (18) Prepared by : Mohammed Thajeel

Advantages and Disadvantages of Signed-Magnitude Representation Signed magnitude easy to understand and encode. Disadvantages: Need additional bit for sign. Not convenient for arithmetic. They are two representations of zero 00000000 = + 010 10000000 = - 010 Data Structures : Lecture (2&3) page (19) Prepared by : Mohammed Thajeel

Two’s-Complement Representation Two’s-Complement (2s complement) is a method for encoding signed integers. To compute integer values using 2s complement: Take its absolute value (its positive equivalent). It is represented in base-2 (convert it to binary number). If it is negative, switch each bit with its complement (i.e. the zeroes are all replaced by ones and vice versa). Add one to complemented. NOTE: positive numbers in 2s complement are simply their binary representation (just step 2). Data Structures : Lecture (2&3) page (20) Prepared by : Mohammed Thajeel

Two’s-Complement Representation cont’d    -410 = 1100 2 Data Structures : Lecture (2&3) page (21) Prepared by : Mohammed Thajeel

Two’s-Complement Representation cont’d   Data Structures : Lecture (2&3) page (22) Prepared by : Mohammed Thajeel

Advantages of 2s Complement Representation - Only has one value for zero. - Convenient for arithmetic. Data Structures : Lecture (2&3) page (23) Prepared by : Mohammed Thajeel

Summary of Integer Representation 2s Complement Sign-Magnitude Unsigned Contents of Memory +0 0000 +1 1 0001 +2 2 0010 +3 3 0011 +4 4 0100 +5 5 0101 +6 6 0110 +7 7 0111 -8 -0 8 1000 -7 -1 9 1001 -6 -2 10 1010 -5 -3 11 1011 -4 12 1100 13 1101 14 1110 15 1111 Data Structures : Lecture (2&3) page (24) Prepared by : Mohammed Thajeel

Overflow What is "overflow“: An error that occurs when the computer attempts to handle a number that is too large for it. When the result of some operation on byte representations falls outside this range, then the value that is represented by the result is different from the correct value. Data Structures : Lecture (2&3) page (25) Prepared by : Mohammed Thajeel

overflow Long (64-bits) Int (32-bits) Short (16-bits) Byte (8-bits) Decimal 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0111 0000 0000 0000 0000 0000 0000 0000 0111 0000 0000 0000 0111 0000 0111 +7 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1001 1111 1111 1111 1111 1111 1111 1111 1001 1111 1111 1111 1001 1111 1001 -7 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0110 0000 1011 1000 0000 0000 0000 0000 0110 0000 1011 1000 0110 0000 1011 1000 overflow +24,760 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 0111 1111 0001 1000 1111 1111 1111 1111 0111 1111 0001 1000 -33,000 0000 0000 0000 0000 0000 0000 0000 0000 1011 0010 1101 0000 0101 1110 0000 0000 3000,000,000 Data Structures : Lecture (2&3) page (26) Prepared by : Mohammed Thajeel

Methods for representing Real Numbers Computers which work with real arithmetic use a system called floating point. In computing, floating point (FP) describes a system for representing real numbers which supports a wide range of values. Declaration in C++: Float x; The term (FP) refers to the fact that the radix point can "float"; that is, it can be placed anywhere relative to the significant digits of the number. The most commonly encountered representation is that defined by the IEEE 754 Standard (Institute for Electrical and Electronics Engineers). Data Structures : Lecture (2&3) page (27) Prepared by : Mohammed Thajeel

Methods for representing Real Numbers cont’d Problem storing binary equivalent of FP number: - We have no way to store the radix point! - All we have are zeros and ones… Solution is Normalization (normal scientific form): - Every binary number, except the one corresponding to the number zero, can be normalized by choosing the exponent so that the radix point falls to the right of the leftmost 1 bit. (±mantissa *2 ) Ex: 37.2510 = 100101.012 = 1.0010101 x 25 7.62510 = 111.1012 = 1.11101 x 22 0.312510 = 0.01012 = 1.01 x 2-2 exponent Data Structures : Lecture (2&3) page (28) Prepared by : Mohammed Thajeel

IEEE Floating Point Format IEEE numbers are stored using a kind of scientific notation. (±mantissa *2 ) We can represent floating-point numbers with three binary fields: a sign bit s, an exponent field e, and a mantissa field m. exponent The left-most bit stores the sign of the number (0 = positive, 1 = negative). The exponent is stored using excess or biased notation – a typical value (usually 2 – 1), known as the bias, is added to the true exponent value (where k is the number of bits in the exponent). The significant/mantissa is stored in the remaining bits. K-1 Data Structures : Lecture (2&3) page (29) Prepared by : Mohammed Thajeel

IEEE Floating Point Format cont’d The IEEE 754 standard defines several different precisions: - Single precision numbers include an 8-bit exponent field and a 23-bit fraction, for a total of 32-bits. bias=2 -1=2 -1=2 -1= 128-1= 127 - Double precision numbers have an 11-bit exponent field and a 52-bit fraction, for a total of 64-bits. bias=2 -1=2 -1=2 -1= 1024-1= 1023 1-bit 8-bit 23-bit S e m K-1 8-1 7 1-bit 11-bit 52-bit e m S K-1 11-1 10 Data Structures : Lecture (2&3) page (30) Prepared by : Mohammed Thajeel

Decimal FP to IEEE standard Conversion Step 1: Compute the binary equivalent of the integer part and the fractional part. Step 2: Normalize the number by moving the binary point to the right of the leftmost one. (±mantissa *2 ) Step 3: a- Convert the exponent to a biased exponent (bias + exponent = biased exponent). b- Convert the biased exponent to binary. Step 4: Store the results from steps 1-3: Sign Exponent Mantissa (from step 3) (from step 2) exponent Data Structures : Lecture (2&3) page (31) Prepared by : Mohammed Thajeel

Decimal FP to IEEE standard Conversion cont’d Example 8: Find the IEEE FP representation of 4.2510 using IEEE 754 Single-precision ? Answer: Step 1: Compute the binary equivalent of the number 4.2510 = 100.1012 Step 2: Normalize the number 100.1012 = 1.00101X 2 Step 3: a- Convert the exponent to a biased exponent. 127 + 2 = 129. This is the biased exponent. b- Convert the biased exponent to binary. 129 10 = 1000 0001 2 Step 4: Store the results from steps 1-3: Sign Exponent Mantissa 2 1 1-bit 8-bit 23-bit Data Structures : Lecture (2&3) page (32) Prepared by : Mohammed Thajeel

Decimal FP to IEEE standard Conversion cont’d Example 9: Find the IEEE FP representation of –24.7510 using IEEE 754 Single-precision ? Answer: Step 1: Compute the binary equivalent of the number –24.75 10 = –11000.11 2 Step 2: Normalize the number –11000.11 2 = 1.100011X 2 Step 3: a- Convert the exponent to a biased exponent. 127 + 4 = 131. This is the biased exponent. b- Convert the biased exponent to binary. 131 10 = 1000 0011 2 Step 4: Store the results from steps 1-3: Sign Exponent Mantissa 4 1 Data Structures : Lecture (2&3) page (33) Prepared by : Mohammed Thajeel