1 / 35

Data Structures

This lecture covers binary and decimal numbers, methods for representing integers and real numbers, binary addition and subtraction, floating point numbers, and conversion between binary and decimal. Prepared by Mohammed Thajeel, lecturer at Furat Al-Awsat Technical University Karbala Technical Institute.

sidwell
Download Presentation

Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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, 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. Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (1)

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

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

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

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

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

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

  10. Binary to Decimal Conversion The Process : Weighted Multiplication • Multiply each bit of the Binary Numberby 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: Bit-Weighting Factors  01102= 610 Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (8)

  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 (12.3125)into its binary equivalent. • Find the binary equivalent of (34.625). Solutions: • 1810 • 1 0 0 1 1 1 1 0 2 • 1 1 0 1 2 • 1100.0101 2 • 100010.101 2 Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (9)

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

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

  14. Methods for representing integers Theintegerabstract 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. Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (12)

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

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

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

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

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

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

  21. Advantages and Disadvantages of Signed-Magnitude Representation • Advantages: • 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 Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (19)

  22. Two’s-Complement Representation • Two’s-Complement(2s complement) is a method for encoding signed integers. • To computeinteger values using2s 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 complementare simply their binary representation (just step 2). Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (20)

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

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

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

  26. Summary of Integer Representation Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (24)

  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 onbyte representations falls outside this range, then the value that is represented by the result is different from the correct value. Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (25)

  28. Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (26)

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

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

  31. 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 Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (29)

  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=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 8-bit 1-bit 23-bit K-1 8-1 7 e m S 1-bit 11-bit 52-bit e m S K-1 11-1 10 Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (30)

  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 *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: SignExponentMantissa (from step 3)(from step 2) exponent Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (31)

  34. 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. 12910 = 1000 00012 Step 4: Store the results from steps 1-3: SignExponentMantissa 2 8-bit 1-bit 23-bit Data Structures : Lecture (2&3) Prepared by : Mohammed Thajeel page (32)

  35. 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.112 Step 2: Normalize the number –11000.112 = 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 00112 Step 4: Store the results from steps 1-3: SignExponentMantissa 4 Prepared by : Mohammed Thajeel Data Structures : Lecture (2&3) page (33)

More Related