Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Mohammed Thajeel To the second year students

Similar presentations


Presentation on theme: "Data Structures Mohammed Thajeel To the second year students"— Presentation transcript:

1 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

2 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

3 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, , 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: , 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

4 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

5 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

6 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

7 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

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

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

10 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  = 610 Data Structures : Lecture (2&3) page (8) Prepared by : Mohammed Thajeel

11 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 ( ) into its binary equivalent. Find the binary equivalent of (34.625) . Solutions: 18 10 Data Structures : Lecture (2&3) page (9) Prepared by : Mohammed Thajeel

12 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

13 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

14 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

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

16 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

17 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

18 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 = If the number negative then flip the leftmost zero bit ( ).  = Data Structures : Lecture (2&3) page (16) Prepared by : Mohammed Thajeel

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

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

21 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 = = Data Structures : Lecture (2&3) page (19) Prepared by : Mohammed Thajeel

22 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

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

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

25 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

26 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

27 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

28 overflow Long (64-bits) Int (32-bits) Short (16-bits) Byte (8-bits)
Decimal +7 -7 overflow +24,760 -33,000 3000,000,000 Data Structures : Lecture (2&3) page (26) Prepared by : Mohammed Thajeel

29 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

30 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 * ) Ex: = = x 25 = = x 22 = = 1.01 x 2-2 exponent Data Structures : Lecture (2&3) page (28) Prepared by : Mohammed Thajeel

31 IEEE Floating Point Format
IEEE numbers are stored using a kind of scientific notation. (±mantissa * ) 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 – 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

32 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= 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= = 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

33 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 * ) 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

34 Decimal FP to IEEE standard Conversion cont’d
Example 8: Find the IEEE FP representation of using IEEE 754 Single-precision ? Answer: Step 1: Compute the binary equivalent of the number = Step 2: Normalize the number = X 2 Step 3: a- Convert the exponent to a biased exponent. = 129. This is the biased exponent. b- Convert the biased exponent to binary. = 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

35 Decimal FP to IEEE standard Conversion cont’d
Example 9: Find the IEEE FP representation of – using IEEE 754 Single-precision ? Answer: Step 1: Compute the binary equivalent of the number – = – Step 2: Normalize the number – = X 2 Step 3: a- Convert the exponent to a biased exponent. = 131. This is the biased exponent. b- Convert the biased exponent to binary. = 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


Download ppt "Data Structures Mohammed Thajeel To the second year students"

Similar presentations


Ads by Google