1 / 65

ICS124 Session 3 Data Representation

ICS124 Session 3 Data Representation. 1. Review QUIZ. 1) The most powerful computers are: a) super PCs c)workstations b) supermainframes d) supercomputers 2) Perform the following conversions: 235 10 -> binary 873 10 -> hexadecimal 87 10 -> octal 1F34 16 -> binary

eden
Télécharger la présentation

ICS124 Session 3 Data Representation

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. ICS124 Session 3 Data Representation 1

  2. Review QUIZ 1) The most powerful computers are: a) super PCs c)workstations b) supermainframes d) supercomputers 2) Perform the following conversions: 23510 -> binary 87310 -> hexadecimal 8710 -> octal 1F3416 -> binary 256278 -> hexadecimal 2

  3. Review QUIZ 1) The most powerful computers are: a) super PCs c)workstations b) supermainframes d) supercomputers 2) Perform the following conversions: 23510 -> binary = 111010112 87310 -> hexadecimal = 36916 8710 -> octal = 1278 1F3416 -> binary = 0001-1111-0011-01002 256278 -> hexadecimal = 2B9716 3

  4. Objectives: • By the end of this session, the student will be able to: • Add two binary numbers together correctly • List two means to represent signed binary numbers • Construct signed binary numbers based on the two representations • Show an alternative way to represent subtraction of two numbers • Perform binary subtraction through the use of twos complement • List the three components of a floating-point number • Define the criteria for Normalized Format when expressing a floating point number • Report on the Normalized Format criteria that has not been met when looking at a floating point number • Categorize the various numeric data types of the C language • Appreciate that the C language has multiple ways to represent numbers • List 3 coding schemes used by computers • Apply the coding scheme tables to convert between characters and bit strings • Define the term 'collating sequence' in relation to the coding schemes • Predict the outcome of sorting data using the coding schemes • Describe the advantage of using Packed Decimal to store numbers • Convert decimal numbers to signed Packed Decimal format 4

  5. Binary Math • Binary math • This section will introduce you to addition and subtraction of binary numbers. • Addition • Remember how we add in base-10: • 11 <--- carry • 297 • +194 • ------- • 491 • In binary we only have four possible results: • 02 + 02 = 02 • 12 + 02 = 12 • 12 + 12 = 102 (this is were we carry the 12) • 12 + 12 + 12 = 112 (if we had carried a one, we will need to again) 5

  6. Binary Math • Addition, continued • 02 + 02 = 02 • 12 + 02 = 12 • 12 + 12 = 102 (this is were we carry the 12) • 12 + 12 + 12 = 112 (if we had carried a one, we will need to again) • On a small scale, add these two binary numbers together: • 1 • 0110 • +0100 • -------- • 1010 • If we convert these to decimal, we see that we are correct: • 01102 is 610 • 01002 is 410 • 10102 is 1010 6

  7. Binary Math • Addition, continued • 02 + 02 = 02 • 12 + 02 = 12 • 12 + 12 = 102 (this is were we carry the 12) • 12 + 12 + 12 = 112 (if we had carried a one, we will need to again) • On a small scale, add these two binary numbers together: • 1 1 • 0110 • +0111 • -------- • 1101 • If we convert these to decimal, we see that we are correct: • 01102 is 610 • 01112 is 710 • 11012 is 1310 7

  8. Binary Math • Example • 02 + 02 = 02 • 12 + 02 = 12 • 12 + 12 = 102 (this is were we carry the 12) • 12 + 12 + 12 = 112 (if we had carried a one, we will need to again) • Perform this calculation: 11012 + 101112 • Prove that the calculation is correct by converting all binary numbers to decimal. 8

  9. Binary Math • Example • 02 + 02 = 02 • 12 + 02 = 12 • 12 + 12 = 102 (this is were we carry the 12) • 12 + 12 + 12 = 112 (if we had carried a one, we will need to again) • Perform this calculation: 11012 + 101112 • 10111 1x24 + 0x23 + 1x22 + 1x21 + 1x20 = 23 • 1101 1x23 + 1x22 + 0x21 + 1x20 = 13 • ----- • 100100 1x25 + 0x24 + 0x23 + 1x22 + 0x21 + 0x20 = 36 9

  10. Binary Math • Subtraction • For subtraction, we are used to seeing this form in base-10: • 297 - 194 = 103 • The problem with subtraction is that is gets messy with borrowing. The following are simple subtractions: • 02 - 02 = 02 • 12 - 12 = 02 • 12 - 02 = 12 • We have to borrow if we see 02 - 12. 10

  11. Binary Math • Subtraction, continued • Imagine this binary subtraction: • 1101 • - 1001 • ------ • 0100 • How about: • 1000 • - 0001 • ------ • 0111 • Is there an easier way? 11

  12. Binary Math, continued • Signed and unsigned numbers • So far the numbers we have been dealing with are all positive integers. However, there exists such things as negative numbers, as in 297 + (-194) = 103 • Unfortunately, computers are aware of only two states, on and off. These states are used to represent the bits in the binary number system. There is no third state to represent negative bits. • A means to differentiate between positive numbers and negative numbers is required. • There are two schemes used to differentiate between positive and negative numbers: • Sign and Magnitude • Twos complement In this class when working with signed binary numbers, work in multiples of eight-bits. You may discard overflow bits. 12

  13. Binary Math, continued • Sign and magnitude • The simplest means to differentiate between positive and negative numbers is to reserve one of the bits of a binary number to indicate sign. The most significant bit has been chosen for this purpose. This leaves one less bit to represent the number- for example 7 bits or 15 bits. • IF the sign bit is '0' THEN the number is positive • IF the sign bit is '1' THEN the number is negative • For example: • 0101 11012 = +9310 • 1101 11012 = -9310 • Since (+93) +(-93) = 0 • then 010111012 + 110111012 should be 000000002 • Try it. 13

  14. Binary Math, continued • Sign and magnitude, continued • Since (+93) +(-93) = 0 • then 010111012 + 11011101 should be 00000000 • 01011101 • +11011101 • --------- • (1)01111010 • Remember to ignore the overflow bit. • Since the sign-bit is in the MSB position and we are using only 7 bits to represent the number, the result is: • 011110102 • = +12210 • Using this technique is not very effective. 14

  15. Binary Math, continued • Twos complement • Twos complement, or 2s complement, is very similar to the scheme that uses just a sign bit. However, as we shall see when we perform subtraction, this scheme is better. To calculate the 2s complement of a number: • flip the bits (1 -> 0, 0 -> 1) this stage is also referred to as one's complement • add 1 For example, 010111012becomes 101000102 <--- bits flipped + 12 --------------- 101000112 So 010111012 is +9310, and 101000112 is -9310 15

  16. Binary Math, continued • Twos complement, continued • To take a negative number in 2s complement and convert into a positive number, perform the 2s complement operation again. • For example, • 101000112 (-9310)becomes • 010111002 • + 12 • ----------- • 010111012 • So 010111012 is +9310, and • 101000112 is -9310 16

  17. Binary Math, continued • Twos complement, continued • If we wish to perform 9310 - 9310, we could instead perform 9310 + (-9310). In binary it would be: • 010111012 • + 101000112 • ------------ • (1)000000002 • The overflow 12 is ignored. This is also what happens in the CPU, and it knows when to ignore the overflow 12 bit as well. 17

  18. Binary Math, continued • Twos complement, continued • What is the result of 4210 - 5310 (perform the calculation in binary): • ? 18

  19. Binary math, continued • Twos complement, continued • What is the result of 4210 - 5310 (perform the calculation in binary): • 4210 = 42 / 2 = 21 R 0 5310 = 53 / 2 = 26 R 1 • 21 / 2 = 10 R 1 26 / 2 = 13 R 0 • 10 / 2 = 5 R 0 13 / 2 = 6 R 1 • 5 / 2 = 2 R 1 6 / 2 = 3 R 0 • 2 / 2 = 1 R 0 3 / 2 = 1 R 1 • 1 / 2 = 0 R 1 1 / 2 = 0 R 1 • = 0010 10102 = 0011 01012 • 5310 in 2s complement: 4210 + (-5310): • 001101012 -> 11001010 00101010 • + 1 + 11001011 • ------------- ----------- • 11001011 11110101 -> 00001010 • + 1 • --------- • 00001011 = -1110 19

  20. Binary Math, continued • Practice • Perform the following calculations. • Convert to binary first, if required • Assume a 16-bit ALU • Assume a 2s complement number if the MSB of the 16-bit ALU is 12 • Convert answers back to their original base 1001110112 + 110110012 9AC16 + 8CA16 7348 + 64538 FEED16 - BAD16 20

  21. Binary Math, continued • Practice • Perform the following calculations. • Convert to binary first, if required • Assume a 16-bit ALU • Assume a 2s complement number if the MSB of the 16-bit ALU is 12 • Convert answers back to their original base 1001110112 + 110110012 0000 0001 0011 1011 +0000 0000 1101 1001 -------------------- 0000 0010 0001 0100 21

  22. Binary Math, continued • Practice • Perform the following calculations. • Convert to binary first, if required • Assume a 16-bit ALU • Assume a 2s complement number if the MSB of the 16-bit ALU is 12 • Convert answers back to their original base 9AC16 + 8CA16 0000 1001 1010 1100 (9AC16) +0000 1000 1100 1010 (8CA16) -------------------- 0001 0010 0111 0110 = 127616 22

  23. Binary Math, continued • Practice • Perform the following calculations. • Convert to binary first, if required • Assume a 16-bit ALU • Assume a 2s complement number if the MSB of the 16-bit ALU is 12 • Convert answers back to their original base 7348 + 64538 0 000 000 111 011 100 (7348) +0 000 110 100 101 011 (64538) ---------------------- 0 000 111 100 000 111 = 74078 23

  24. Binary Math, continued • Practice • Perform the following calculations. • Convert to binary first, if required • Assume a 16-bit ALU • Assume a 2s complement number if the MSB of the 16-bit ALU is 12 • Convert answers back to their original base FEED16 - BAD16 BAD16 in 2s complement: 0000 1011 1010 1101 1111 0100 0101 0010 + 1 -------------------- 1111 0100 0101 0011 1111 1110 1110 1101 (FEED16) +1111 0100 0101 0011 (2s comp of BAD16) -------------------- 1111 0011 0100 0000 = F34016 24

  25. Floating Point Numbers 25

  26. Floating Point Numbers • Floating point numbers • The numbers that we have been dealing with are all integers with limited precision. For example, a 16-bit binary number can store values between -32768 and +32767 using 2s complement. • We have a need to represent numbers that are either very small (such as the diameter of a virus) or very large (such as the diameter of the solar system). • Floating point numbers are how we represent these numbers. • Representation • A way to represent numbers is to use three components to describe the number: • Base • Mantissa • Exponent 26

  27. Floating Point Numbers, continued • Representation, continued • The general form for representing a floating point number is • M x Be • where 'M' is the mantissa, 'B' is the base and 'e' is the exponent. • An example of a floating point number in this form is: 0.523x103 (which is 523) • Base • The base is the numbering system that is being used, such as decimal (10) or binary (2). 27

  28. Floating Point Numbers, continued • Mantissa • Mantissa is the numeric portion of the floating point number being represented. It will be: • B-1<= | M | < 1 • This is referred to as Normalized Format • Looking at the previous example: • 0.523x103 • we see that the mantissa is < 1, and greater than or equal to 10-1 (which is 0.1). • What all this boils down to is no numbers are to the left of the decimal point, and the first number to the right of the decimal point is not a zero. • Both of these are invalid mantissas: • 7.003 • 0.084 28

  29. Floating Point Numbers, continued • Mantissa, continued • Since we are dealing with values to the right of the 'radix point' (commonly called a decimal point in the decimal numbering system), the digit exponents are negative: • In Decimal: • 10-1, 10-2, 10-3, 10-4, 10-5, 10-6... • = 1/10, 1/100, 1/1000, 1/10000, 1/100000 • = 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001.... • So, similar to what we have seen: • 0.10236 • = 1x10-1 + 0x10-2 + 2x10-3 + 3x10-4 + 6x10-5 • Exponent • The exponent is applied to the base, and then the mantissa is multiplied by this value: • 0.10236 x 104 • =1023.6 29

  30. Floating Point Numbers, continued • Storing a binary floating-point number • The same components are used: • Base • Exponent • Mantissa There are specific representations defined by IEEE (Institute of Electrical and Electronics Engineers www.ieee.org) for the storage of floating point numbers. For single precision, 32 bits are used as follows: 30

  31. Floating Point Numbers, continued • Base • Since are dealing with a binary machine, the base is always 2. There is no need to store it in memory. • Mantissa • The mantissa is stored in sign and magnitude format and is normalized, HOWEVER, there is a slight variation regarding normalization. The binary mantissa is in the range of: • 1.00000000000000000000000 to • 1.11111111111111111111111 • 1.0 <= | M | < 2 • Since the mantissa ALWAYS starts with a '1', the leading 1 is never stored, it is assumed to be there. • Therefore, 1.00011111111111111111101 • would be stored as 00011111111111111111101 • This is the normalized format for the storage of floating point binary numbers. 31

  32. Floating Point Numbers, continued • Mantissa, continued • Since we are dealing with values to the right of the 'radix point' (commonly called a decimal point in the decimal numbering system), the digit exponents are negative: • In Decimal: • 10-1, 10-2, 10-3, 10-4, 10-5, 10-6... • = 1/10, 1/100, 1/1000, 1/10000, 1/100000 • = 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001.... • In Binary • 2-1, 2-2, 2-3, 2-4, 2-5, 2-6... • = 1/ 2 1/ 4 1/8 1/16 1/32 1/64.... • = 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625... • therefore 1.12 = 1x20 + 1x2-1 = 110 + 1/ 210 = 3/210 32

  33. Floating Point Numbers, continued • Exponent • The exponent is stored in 'Excess 127' format. To get the exponent, you subtract 127 from the stored value. • Therefore, if the value stored is 120, the exponent would be -7 • There are two 'special' cases for the exponent: • 0 - represents 'Exactly zero' (as opposed to a number that looks like zero because it has lost its precision) • 255 - represents infinity • A portion of the excess-127 table: • 00000000 0 - exactly 0 • ... • 01111101 125 - -2 • 01111110 126 - -1 • 01111111 127 - 0 • 10000000 128 - +1 • 10000001 129 - +2 • ... • 11111111 255 - infinity 33

  34. Floating Point Numbers, continued • Layout of floating point number • The layout is as follows: • Sign bit - 1 bit • Exponent - 8 bits • Mantissa - 23 bits • ====== • 32 bits • Seee-eeee-eMMM-MMMM-MMMM-MMMM-MMMM-MMMM • 0100-0000-0000-0000-0000-0000-0000-0000 = +210 34

  35. Floating Point Numbers, continued • Layout of floating point number, continued • Seee-eeee-eMMM-MMMM-MMMM-MMMM-MMMM-MMMM • 1100-0011-1001-0110-0000-0000-0000-0000 • Sign is negative • Exponent = 10000111 = 135 • 135 - 127 = 8 • represents 28 • Mantissa = 1.001011 • = 1 + 1/8 + 1/32 + 1/64 • = 75/64 • -75/6410 x 28 = -300.0010 35

  36. Numbers in the C Language 36

  37. Numbers in C • Purpose • Although we have not begun to talk about programming languages yet, we can appreciate the limits of a computer, based on the discussions of data representation thus far. • Background • When a programmer wishes to manipulate data in a program, they will refer to a symbolic name when they need to perform some action on that data. • A way of thinking of a symbolic name is to consider an application form. On this application form is a question about your age. It shows up on the form like: • Applicant's Age • The box is where you enter the data (your age), and the words beside the box is a label or symbolic name. • The programmer, may call this variable in their program applicantsAge 37

  38. Numbers in C, continued • Background, continued • When a C programmer needs a symbolic name, they make up the name, but must tell the computer what type of data the symbolic name will represent. • The data types for C are: • char • short int • int • long int • float • double 38

  39. Numbers in C, continued • char data type • A char is one byte, and is used to represent characters. • It can store numbers between 0 and 255 (where each number represents a character on the keyboard, plus other special characters). 39

  40. Numbers in C, continued • short int data type • The short int is an integer. • It can store numbers between -32768 and 32767 40

  41. Numbers in C, continued • int data type • The int is slightly larger • It can store numbers between -2,147,483,648 to 2,147,483,647 41

  42. Numbers in C, continued • long int data type • The long int is larger again • It can store numbers between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 42

  43. Numbers in C, continued • float data type • The float is where the ability to store non-integers (real numbers) begins (single precision- 32 bits). • It can store numbers between 0.11754943508222875x10-39 to 0.34028234663852886x10+39 43

  44. Numbers in C, continued • double data type • The double is larger (double precision- 64 bits) • It can store numbers between 0.22250738585072014x10-309 to 0.17976931348623158x10+309 44

  45. Coding Schemes 45

  46. Coding Schemes • Background • In our discussions of data representation, we have been dealing with pure binary numbers. However, we as humans, work with more than numbers. We need to represent words and symbols and ideas that are more than simply numbers. • For example, the binary stream 100100011001011101100110110011011112 • is also used to represent the word 'Hello'. • Another way to think of this is consider a payroll program. Some of the data is pure numbers such as pay rate and hours worked. Other data is the Social Insurance Number (S.I.N.) and name of the employee. The SIN and name are not used as numbers as there are no meaningful calculations performed on them. • What is the significance of : • (John Doe + 3) / 5 ? • A means to represent more than simply numbers, is possible through a coding scheme where simple numbers are used to represent letters of a word or symbols. Ideas meaningful to people can be constructed from this scheme. 46

  47. Coding Schemes, continued • Early coding schemes • Two early coding schemes will be looked at. One pre-dates computers, the other is an early zoned coding scheme: • Morse Code • 6-bit Binary Coded Decimal Morse Code An early coding scheme use to represent letters and numbers is the Morse Code. This scheme used two symbols, a dot and a dash in various combinations to represent letters. For example, here is a portion of the Morse Code: http://tronweb.super-nova.co.jp/characcodehist.html 47

  48. Coding Schemes, continued • 6-bit Binary Coded Decimal • The problem with Morse Code is that there are a variable number of dots and dashes to represent the letters and numbers. • Binary Coded Decimal (BCD), is used to represent each digit of a decimal number. For input and output operations, it allows the device to treat each character as an independent entity. The following is how decimal numbers are stored in BCD format: • DecimalBinaryDecimal Binary • 0 0000 5 0101 • 1 0001 6 0110 • 2 0010 7 0111 • 3 0011 8 1000 • 4 0100 9 1001 • It should be no surprise that these binary numbers are straight conversions of the decimal number. When entering a value of 12310, instead of entering 011110112, we enter 0001001000112. 48

  49. Coding Schemes, continued • 6-bit Binary Coded Decimal, continued • Decimal Binary Decimal Binary • 0 0000 5 0101 • 1 0001 6 0110 • 2 0010 7 0111 • 3 0011 8 1000 • 4 0100 9 1001 • To represent values other than numbers, two zone bits are added to the beginning of the codes above, give the 6-bit BCD Code. • Zone 00 - Numbers 0 -9 • Zone 01 - Letters S - Z • Zone 10 - Letters J - R • Zone 11 - Letters A - I • The 6-bit BCD code table follows: 49

  50. Coding Schemes, continued • 6-bit Binary Coded Decimal, continued • CharacterCodeCharacterCode • A 11 0001 S 01 0010 • B 11 0010 T 01 0011 • C 11 0011 U 01 0100 • D 11 0100 V 01 0101 • E 11 0101 W 01 0110 • F 11 0110 X 01 0111 • G 11 0111 Y 01 1000 • H 11 1000 Z 01 1001 • I 11 1001 • 0 00 1010 • J 10 0001 1 00 0001 • K 10 0010 2 00 0010 • L 10 0011 3 00 0011 • M 10 0100 4 00 0100 • N 10 0101 5 00 0101 • O 10 0110 6 00 0110 • P 10 0111 7 00 0111 • Q 10 1000 8 00 1000 • R 10 1001 9 00 1001 50

More Related