Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 5 Data representation Data Rep. 2 Learning outcomes By the end of this Chapter you will be able to: Explain how integers are represented in.

Similar presentations


Presentation on theme: "1 Chapter 5 Data representation Data Rep. 2 Learning outcomes By the end of this Chapter you will be able to: Explain how integers are represented in."— Presentation transcript:

1 1 Chapter 5 Data representation Data Rep

2 2 Learning outcomes By the end of this Chapter you will be able to: Explain how integers are represented in computers using: Unsigned, signed magnitude, excess, and twos complement notations Explain how fractional numbers are represented in computers Floating point notation (IEEE 754 single format) Calculate the decimal value represented by a binary sequence in: Unsigned, signed notation, excess, twos complement, and the IEEE 754 notations. Explain how characters are represented in computers E.g. using ASCII and Unicode Explain how colours, images, sound and movies are represented Data Rep

3 3 Additional Reading Essential Reading Stalling (2003): Chapter 9 Further Reading Brookshear (2003): Chapter Burrell (2004): Chapter 2 Schneider and Gersting (2004): Chapter 4.2 Data Rep

4 4 Number representation Representing whole numbers Representing fractional numbers Data Rep

5 5 Integer Representations Unsigned notation Signed magnitude notion Excess notation Twos complement notation. Data Rep

6 6 Unsigned Representation Represents positive integers. Unsigned representation of 157: Addition is simple: = position Bit pattern contribution Data Rep

7 7 Advantages and disadvantages of unsigned notation Advantages: One representation of zero Simple addition Disadvantages Negative numbers can not be represented. The need of different notation to represent negative numbers. Data Rep

8 8 Representation of negative numbers Is a representation of negative numbers possible? Unfortunately: you can not just stick a negative sign in front of a binary number. (it does not work like that) There are three methods used to represent negative numbers. Signed magnitude notation Excess notation notation Twos complement notation Data Rep

9 9 Signed Magnitude Representation Unsigned: - and + are the same. In signed magnitude the left-most bit represents the sign of the integer. 0 for positive numbers. 1 for negative numbers. The remaining bits represent to magnitude of the numbers. Data Rep

10 10 Example Suppose is a signed magnitude representation. The sign bit is 1, then the number represented is negative The magnitude is with a value = 29 Then the number represented by is –29. position Bit pattern contribution Data Rep

11 11 Exercise has in signed magnitude notation. Find the signed magnitude of –37 10 ? 2. Using the signed magnitude notation find the 8-bit binary representation of the decimal value and Find the signed magnitude of –63 using 8-bit binary sequence? Data Rep

12 12 Disadvantage of Signed Magnitude Addition and subtractions are difficult. Signs and magnitude, both have to carry out the required operation. They are two representations of = = To test if a number is 0 or not, the CPU will need to see whether it is or is always performed in programs. Therefore, having two representations of 0 is inconvenient. Data Rep

13 13 Signed-Summary In signed magnitude notation, The most significant bit is used to represent the sign. 1 represents negative numbers 0 represents positive numbers. The unsigned value of the remaining bits represent The magnitude. Advantages: Represents positive and negative numbers Disadvantages: two representations of zero, Arithmetic operations are difficult. Data Rep

14 14 Excess Notation In excess notation: The value represented is the unsigned value with a fixed value subtracted from it. For n-bit binary sequences the value subtracted fixed value is 2 (n-1). Most significant bit: 0 for negative numbers 1 for positive numbers Data Rep

15 15 Excess Notation with n bits 1000…0 represent 2 n-1 is the decimal value in unsigned notation. Therefore, in excess notation: 1000…0 will represent 0. Decimal value In unsigned notation Decimal value In excess notation - 2 n-1 = Data Rep

16 16 Example (1) - excess to decimal Find the decimal number represented by in excess notation. Unsigned value = = = Excess value: excess value = 153 – 2 7 = 152 – 128 = 25. Data Rep

17 17 Example (2) - decimal to excess Represent the decimal value 24 in 8-bit excess notation. We first add, 2 8-1, the fixed value = = 152 then, find the unsigned value of = (unsigned notation) = (excess notation) Data Rep

18 18 example (3) Represent the decimal value -24 in 8-bit excess notation. We first add, 2 8-1, the fixed value = = 104 then, find the unsigned value of = (unsigned notation) = (excess notation) Data Rep

19 19 Example (4) (10101) Unsigned = = The value represented in unsigned notation is 21 Sign Magnitude The sign bit is 1, so the sign is negative The magnitude is the unsigned value = 5 10 So the value represented in signed magnitude is Excess notation As an unsigned binary integer = subtracting = 2 4 = 16, we get = So the value represented in excess notation is Data Rep

20 20 Advantages of Excess Notation It can represent positive and negative integers. There is only one representation for 0. It is easy to compare two numbers. When comparing the bits can be treated as unsigned integers. Excess notation is not normally used to represent integers. It is mainly used in floating point representation for representing fractions (later floating point rep.). Data Rep

21 21 Exercise 2 1. Find is an 8-bit binary sequence. Find the decimal value it represents if it was in unsigned and signed magnitude. Suppose this representation is excess notation, find the decimal value it represents? 2. Using 8-bit binary sequence notation, find the unsigned, signed magnitude and excess notation of the decimal value ? Data Rep

22 22 Excess notation - Summary In excess notation, the value represented is the unsigned value with a fixed value subtracted from it. i.e. for n-bit binary sequences the value subtracted is 2 (n-1). Most significant bit: 0 for negative numbers. 1 positive numbers. Advantages: Only one representation of zero. Easy for comparison. Data Rep

23 23 Twos Complement Notation The most used representation for integers. All positive numbers begin with 0. All negative numbers begin with 1. One representation of zero i.e. 0 is represented as 0000 using 4-bit binary sequence. Data Rep

24 24 Twos Complement Notation with 4-bits Binary pattern Value in 2s complement Data Rep

25 25 Properties of Twos Complement Notation Positive numbers begin with 0 Negative numbers begin with 1 Only one representation of 0, i.e Relationship between +n and –n Data Rep

26 26 Advantages of Twos Complement Notation It is easy to add two numbers Subtraction can be easily performed. Multiplication is just a repeated addition. Division is just a repeated subtraction Twos complement is widely used in ALU Data Rep

27 27 Evaluating numbers in twos complement notation Sign bit = 0, the number is positive. The value is determined in the usual way. Sign bit = 1, the number is negative. three methods can be used: Method 1decimal value of (n-1) bits, then subtract 2 n-1 Method 2- 2 n-1 is the contribution of the sign bit. Method 3 Binary rep. of the corresponding positive number. Let V be its decimal value. - V is the required value. Data Rep

28 28 Example in Twos Complement The most significant bit is 1, hence it is a negative number. Method = +5 (+5 – = 5 – 2 4 = 5-16 = -11) Method = -11 Method 3 Corresponding + number is = = 11 the result is then –11. Data Rep

29 29 Twos complement-summary In twos complement the most significant for an n-bit number has a contribution of –2 (n-1). One representation of zero All arithmetic operations can be performed by using addition and inversion. The most significant bit: 0 for positive and 1 for negative. Three methods can the decimal value of a negative number: Method 1decimal value of (n-1) bits, then subtract 2 n-1 Method 2- 2 n-1 is the contribution of the sign bit. Method 3 Binary rep. of the corresponding positive number. Let V be its decimal value. - V is the required value. Data Rep

30 30 Exercise Determine the decimal value represented by in each of the following four systems. 1. Unsigned notation? 2. Signed magnitude notation? 3. Excess notation? 4. Tows complements? Data Rep

31 31 Fraction Representation To represent fraction we need other representations: Fixed point representation Floating point representation. Data Rep

32 32 Fixed-Point Representation old position New position Bit pattern Contribution = Radix-point Data Rep

33 33 Limitation of Fixed-Point Representation To represent large numbers or very small numbers we need a very long sequences of bits. This is because we have to give bits to both the integer part and the fraction part. Data Rep

34 34 Floating Point Representation In decimal notation we can get around this problem using scientific notation or floating point notation. NumberScientific notationFloating-point notation 1,245,000,000, Data Rep

35 35 Floating Point could be represented as * * A calculator might display 159 E * SignExponent Base Mantissa Data Rep

36 36 M B E Sign mantissa or base exponent significand Floating point format SignExponentMantissa Data Rep

37 37 Floating Point Representation format The exponent is biased by a fixed value b, called the bias. The mantissa should be normalised, e.g. if the real mantissa if of the form 1.f then the normalised mantissa should be f, where f is a binary sequence. SignExponentMantissa Data Rep

38 38 IEEE 745 Single Precision The number will occupy 32 bits The first bit represents the sign of the number; 1= negative 0= positive. The next 8 bits will specify the exponent stored in biased 127 form. The remaining 23 bits will carry the mantissa normalised to be between 1 and 2. i.e. 1<= mantissa < 2 Data Rep

39 39 Representation in IEEE 754 single precision sign bit: 0 for positive and, 1 for negative numbers 8 biased exponent by bit normalised mantissa SignExponentMantissa Data Rep

40 40 Basic Conversion Converting a decimal number to a floating point number. 1.Take the integer part of the number and generate the binary equivalent. 2.Take the fractional part and generate a binary fraction 3.Then place the two parts together and normalise. Data Rep

41 41 IEEE – Example 1 Convert 6.75 to 32 bit IEEE format. 1. The Mantissa. The Integer first. 6 / 2 = 3 r 0 3 / 2= 1 r 1 1 / 2= 0 r 1 2. Fraction next..75 * 2 = * 2 = put the two parts together… Now normalise * 2 2 = = Data Rep

42 42 = IEEE – Example 1 Convert 6.75 to 32 bit IEEE format. 1. The Mantissa. The Integer first. 6 / 2 = 3 r 0 3 / 2= 1 r 1 1 / 2= 0 r 1 2. Fraction next..75 * 2 = * 2 = put the two parts together… Now normalise * 2 2 = Data Rep

43 43 IEEE – Example 1 Convert 6.75 to 32 bit IEEE format. 1. The Mantissa. The Integer first. 6 / 2 = 3 r 0 3 / 2= 1 r 1 1 / 2= 0 r 1 2. Fraction next..75 * 2 = * 2 = put the two parts together… Now normalise * 2 2 = = Data Rep

44 44 IEEE Biased 127 Exponent To generate a biased 127 exponent Take the value of the signed exponent and add 127. Example then = and my value for the exponent would be 143 = So it is simply now an unsigned value.... Data Rep

45 45 Possible Representations of an Exponent Data Rep

46 46 Why Biased ? The smallest exponent Only one exponent zero The highest exponent is To increase the exponent by one simply add 1 to the present pattern. Data Rep

47 47 Back to the example Our original example revisited… * 2 2 Exponent is =129 or in binary. NOTE: Mantissa always ends up with a value of 1 before the Dot. This is a waste of storage therefore it is implied but not actually stored is stored in 32 bit floating point IEEE representation: sign(1) exponent(8) mantissa(23 ) Data Rep

48 48 Representation in IEEE 754 single precision sign bit: 0 for positive and, 1 for negative numbers 8 biased exponent by bit normalised mantissa SignExponentMantissa Data Rep

49 49 Example (2) which number does the following IEEE single precision notation represent? The sign bit is 1, hence it is a negative number. The exponent is = It is biased by 127, hence the real exponent is 128 –127 = 1. The mantissa: It is normalised, hence the true mantissa is 1.01 = Finally, the number represented is: x 2 1 = Data Rep

50 50 Single Precision Format The exponent is formatted using excess-127 notation, with an implied base of 2 Example: Exponent: Representation: 135 – 127 = 8 The stored values 0 and 255 of the exponent are used to indicate special values, the exponential range is restricted to to The number 0.0 is defined by a mantissa of 0 together with the special exponential value 0 The standard allows also values +/- (represented as mantissa +/-0 and exponent 255 Allows various other special conditions Data Rep

51 51 In comparison The smallest and largest possible 32-bit integers in twos complement are only and PITT CS Data Rep

52 52 Negative Overflow Positive Overflow Expressible numbers Numbers in 32-bit Formats Twos complement integers Floating point numbers Ref: W. Stallings, Computer Organization and Architecture, Sixth Edition, Upper Saddle River, NJ: Prentice-Hall Expressible negative numbers Expressible positive numbers Positive underflow Negative underflow (2 – )× (2 – )×2 127

53 Data Rep 53 Positive Zero in IEEE × Smallest positive number in single-precision IEEE 754 standard. Interpreted as positive zero. True exponent less than -127 is positive underflow; can be regarded as zero Biased exponent Fraction

54 Data Rep 54 Negative Zero in IEEE × Smallest negative number in single-precision IEEE 754 standard. Interpreted as negative zero. True exponent less than -127 is negative underflow; may be regarded as Biased exponent Fraction

55 Data Rep 55 Positive Infinity in IEEE × Largest positive number in single-precision IEEE 754 standard. Interpreted as + If true exponent = 128 and fraction 0, then the number is greater than. It is called not a number or NaN and may be interpreted as Biased exponent Fraction

56 Data Rep 56 Negative Infinity in IEEE × Smallest negative number in single-precision IEEE 754 standard. Interpreted as - If true exponent = 128 and fraction 0, then the number is less than -. It is called not a number or NaN and may be interpreted as Biased exponent Fraction

57 57 Range of numbers Normalized (positive range; negative is symmetric) × (1+0) = × ( ) smallest largest ( ) Positive underflow Positive overflow PITT CS Data Rep

58 58 Representation in IEEE 754 double precision format It uses 64 bits 1 bit sign 11 bit biased exponent 52 bit mantissa SignExponentMantissa Data Rep

59 59 IEEE 754 double precision Biased = bit exponent with an excess of For example: If the exponent is -1 we then add 1023 to it = 1022 We then find the binary representation of 1022 Which is The exponent field will now hold This means that we just represent -1 with an excess of Data Rep

60 60 IEEE 754 Encoding Single PrecisionDouble PrecisionRepresented Object ExponentFractionExponentFraction non-zero0 +/- denormalized number 1~254anything1~2046anything +/- floating-point numbers /- infinity 255non-zero2047non-zeroNaN (Not a Number) Data Rep

61 61 Floating Point Representation format (summary) the sign bit represents the sign 0 for positive numbers 1 for negative numbers The exponent is biased by a fixed value b, called the bias. The mantissa should be normalised, e.g. if the real mantissa if of the form 1.f then the normalised mantissa should be f, where f is a binary sequence. SignExponentMantissa Data Rep

62 62 Character representation- ASCII ASCII (American Standard Code for Information Interchange) It is the scheme used to represent characters. Each character is represented using 7-bit binary code. If 8-bits are used, the first bit is always set to 0 See (table 5.1 p56, study guide) for character representation in ASCII. Data Rep

63 63 ASCII – example Symbol decimal Binary : ; < = > ? A B C Data Rep

64 64 Character strings How to represent character strings? A collection of adjacent words (bit-string units) can store a sequence of letters Notation: enclose strings in double quotes "Hello world" Representation convention: null character defines end of string Null is sometimes written as '\0' Its binary representation is the number 0 'H' 'e' 'l' 'l' o' ' ' 'W' 'o' 'r' 'l' 'd' '\0' Data Rep

65 65 Layered View of Representation Text string Sequence of characters Character Bit string Information Data Information Data Information Data Information Data Data Rep

66 66 Working With A Layered View of Representation Represent SI at the two layers shown on the previous slide. Representation schemes: Top layer - Character string to character sequence: Write each letter separately, enclosed in quotes. End string with \0. Bottom layer - Character to bit-string: Represent a character using the binary equivalent according to the ASCII table provided. Data Rep

67 67 Solution SI S I \ The colors are intended to help you read it; computers dont care that all the bits run together. Data Rep

68 68 exercise Use the ASCII table to write the ASCII code for the following: CIS110 6=2*3 Data Rep

69 69 Unicode - representation ASCII code can represent only 128 = 2 7 characters. It only represents the English Alphabet plus some control characters. Unicode is designed to represent the worldwide interchange. It uses 16 bits and can represents 32,768 characters. For compatibility, the first 128 Unicode are the same as the one of the ASCII. Data Rep

70 70 Colour representation Colours can represented using a sequence of bits. 256 colours – how many bits? Hint for calculating To figure out how many bits are needed to represent a range of values, figure out the smallest power of 2 that is equal to or bigger than the size of the range. That is, find x for 2 x => bit colour – how many possible colors can be represented? Hints 16 million possible colours (why 16 millions?) Data Rep

71 71 24-bits -- the True colour 24-bit color is often referred to as the true colour. Any real-life shade, detected by the naked eye, will be among the 16 million possible colours. Data Rep

72 72 Example: 2-bit per pixel 4=2 2 choices 00 (off, off)=white 01 (off, on)=light grey 10 (on, off)=dark grey 11 (on, on)=black 0 0 =(white) 1 0 =(dark grey) 1 0 =(light grey) 1 1 =(black) Data Rep

73 73 Image representation An image can be divided into many tiny squares, called pixels. Each pixel has a particular colour. The quality of the picture depends on two factors: the density of pixels. The length of the word representing colours. The resolution of an image is the density of pixels. The higher the resolution the more information information the image contains. Data Rep

74 74 Bitmap Images Each individual pixel (pi(x)cture element) in a graphic stored as a binary number Pixel: A small area with associated coordinate location Example: each point below is represented by a 4-bit code corresponding to 1 of 16 shades of gray Data Rep

75 75 Representing Sound Graphically X axis: time Y axis: pressure A: amplitude (volume) : wavelength (inverse of frequency = 1/ ) Data Rep

76 76 Sampling Sampling is a method used to digitise sound waves. A sample is the measurement of the amplitude at a point in time. The quality of the sound depends on: The sampling rate, the faster the better The size of the word used to represent a sample. Data Rep

77 77 Digitizing Sound Zoomed Low Frequency Signal Capture amplitude at these points Lose all variation between data points Data Rep

78 78 Summary Integer representation Unsigned, Signed, Excess notation, and Twos complement. Fraction representation Floating point (IEEE 754 format ) Single and double precision Character representation Colour representation Sound representation Data Rep

79 79 Exercise 1. Represent +0.8 in the following floating-point representation: 1-bit sign 4-bit exponent 6-bit normalised mantissa (significand). 2. Convert the value represented back to decimal. 3. Calculate the relative error of the representation. Data Rep


Download ppt "1 Chapter 5 Data representation Data Rep. 2 Learning outcomes By the end of this Chapter you will be able to: Explain how integers are represented in."

Similar presentations


Ads by Google