2Learning 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 two’s complement notationsExplain how fractional numbers are represented in computersFloating point notation (IEEE 754 single format)Calculate the decimal value represented by a binary sequence in:Unsigned, signed notation, excess, two’s complement, and the IEEE 754 notations.Explain how characters are represented in computersE.g. using ASCII and UnicodeExplain how colours, images, sound and movies are represented
4Introduction In Chapter 5 In Chapter 3 In Chapter 4: How information is storedin the main memory,magnetic memory,and optical memoryIn Chapter 4:We studied how information is processed in computersHow the CPU executes instructionsIn Chapter 5We will be looking at how data is represented in computersInteger and fractional number representationCharacters, colours and sounds representation
5Binary numbersBinary number is simply a number comprised of only 0's and 1's.Computers use binary numbers because it's easy for them to communicate using electrical current is off, 1 is on.You can express any base 10 (our number system -- where each digit is between 0 and 9) with binary numbers.The need to translate decimal number to binary and binary to decimal.There are many ways in representing decimal number in binary numbers. (later)
6How does the binary system work? It is just like any other systemIn decimal system the base is 10We have 10 possible values 0-9In binary system the base is 2We have only two possible values 0 or 1.The same as in any base, 0 digit has non contribution, where 1’s has contribution which depends at there position.
12Unsigned Representation Represents positive integers.Unsigned representation of 157:Addition is simple:=position7654321Bit patterncontribution2724232220
13Advantages and disadvantages of unsigned notation One representation of zeroSimple additionDisadvantagesNegative numbers can not be represented.The need of different notation to represent negative numbers.
14Representation 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 notationExcess notation notationTwo’s complement notation
15Signed Magnitude Representation Unsigned: - and + are the same.In signed magnitudethe 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.
16Example - Suppose 10011101 is a signed magnitude representation. The sign bit is 1, then the number represented is negativeThe magnitude is with a value = 29Then the number represented by is –29.position7654321Bit patterncontribution-24232220
17Exercise 13710 has in signed magnitude notation. Find the signed magnitude of –3710 ?Using the signed magnitude notation find the 8-bit binary representation of the decimal value 2410 andFind the signed magnitude of –63 using 8-bit binary sequence?
18Disadvantage of Signed Magnitude Addition and subtractions are difficult.Signs and magnitude, both have to carry out the required operation.They are two representations of 0==To test if a number is 0 or not, the CPU will need to see whether it is or0 is always performed in programs.Therefore, having two representations of 0 is inconvenient.
19Signed-Summary In signed magnitude notation, Advantages: The most significant bit is used to represent the sign.1 represents negative numbers0 represents positive numbers.The unsigned value of the remaining bits represent The magnitude.Advantages:Represents positive and negative numbersDisadvantages:two representations of zero,Arithmetic operations are difficult.
20Excess 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 numbers1 for positive numbers
21Excess Notation with n bits 1000…0 represent 2n-1 is the decimal value in unsigned notation.Therefore, in excess notation:1000…0 will represent 0 .Decimal valueIn unsignednotationDecimal valueIn excessnotation- 2n-1 =
22Example (1) - excess to decimal Find the decimal number represented by in excess notation.Unsigned value= = = 15310Excess value:excess value = 153 – 27 = 152 – 128 = 25.
23Example (2) - decimal to excess Represent the decimal value 24 in 8-bit excess notation.We first add, 28-1, the fixed value= = 152then, find the unsigned value of 15215210 = (unsigned notation).= (excess notation)
24example (3) Represent the decimal value -24 in 8-bit excess notation. We first add, 28-1, the fixed value= = 104then, find the unsigned value of 10410410 = (unsigned notation).-2410 = (excess notation)
25Example (4) (10101) Unsigned Sign Magnitude Excess notation = = 2110The value represented in unsigned notation is 21Sign MagnitudeThe sign bit is 1, so the sign is negativeThe magnitude is the unsigned value = 510So the value represented in signed magnitude is -510Excess notationAs an unsigned binary integer = 2110subtracting = = 16, we get = 510.So the value represented in excess notation is 510.
26Advantages 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.).
27Exercise 2 Find 10011001 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?Using 8-bit binary sequence notation, find the unsigned, signed magnitude and excess notation of the decimal value ?
28Excess 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.
29Two’s Complement Notation The most used representation for integers.All positive numbers begin with 0.All negative numbers begin with 1.One representation of zeroi.e. 0 is represented as 0000 using 4-bit binary sequence.
30Two’s Complement Notation with 4-bits Binary pattern Value in 2’s complement.
31Properties of Two’s Complement Notation Positive numbers begin with 0Negative numbers begin with 1Only one representation of 0, i.e. 0000Relationship between +n and –n.
32Advantages of Two’s 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 subtractionTwo’s complement is widely used in ALU
33Evaluating numbers in two’s 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 2n-1Method 2- 2n-1 is the contribution of the sign bit.Method 3Binary rep. of the corresponding positive number.Let V be its decimal value.- V is the required value.
34Example- 10101 in Two’s Complement The most significant bit is 1, hence it is a negative number.Method 10101 = (+5 – 25-1 = 5 – 24 = 5-16 = -11)Method 2=Method 3Corresponding + number is = = 11the result is then –11.
35Two’s complement-summary In two’s complement the most significant for an n-bit number has a contribution of –2(n-1).One representation of zeroAll 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 2n-1Method 2- 2n-1 is the contribution of the sign bit.Method 3Binary rep. of the corresponding positive number.Let V be its decimal value.- V is the required value.
36ExerciseDetermine the decimal value represented by in each of the following four systems.Unsigned notation?Signed magnitude notation?Excess notation?Tow’s complements?
37Fraction Representation To represent fraction we need other representations:Fixed point representationFloating point representation.
38Fixed-Point Representation old positionNew positionBit patternContribution =19.625Radix-point
39Limitation 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.
40Floating Point Representation In decimal notation we can get around thisproblem using scientific notation or floatingpoint notation.NumberScientific notationFloating-point notation1,245,000,000,000
41Base Mantissa Exponent Sign Floating Pointcould be represented asBaseMantissaExponentSign- 159 * 1014When we deal with very large and very small numbers we often resort to using scientific notation.Calculators, for example, often represent the results of large calculations this way because there is an insufficient number of digits to show the result normally159 is called the mantissa 14 the exponent which is the number of places to move the decimal point.* 1015* 1016A calculator might display 159 E14
42Floating point format M B E Sign mantissa or base exponent significandSignExponentMantissa
43Floating 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
44The number will occupy 32 bits IEEE 745 Single PrecisionThe number will occupy 32 bitsThe 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
45Representation in IEEE 754 single precision sign bit:0 for positive and,1 for negative numbers8 biased exponent by 12723 bit normalised mantissaSignExponentMantissa
46Basic ConversionConverting 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 fraction3.Then place the two parts together and normalise.
47IEEE – Example 1 Convert 6.75 to 32 bit IEEE format. = 1102 = 0.112 1. The Mantissa. The Integer first.6 / 2 = 3 r 03 / 2 = 1 r 11 / 2 = 0 r 12. Fraction next..75 * 2 = 1.5.5 * 2 = 1.03. put the two parts together…Now normalise * 22= 1102= 0.112
48IEEE – Example 1 Convert 6.75 to 32 bit IEEE format. = 1102 = 0.112 1. The Mantissa. The Integer first.6 / 2 = 3 r 03 / 2 = 1 r 11 / 2 = 0 r 12. Fraction next..75 * 2 = 1.5.5 * 2 = 1.03. put the two parts together…Now normalise * 22= 1102= 0.112
49IEEE – Example 1 Convert 6.75 to 32 bit IEEE format. = 1102 = 0.112 1. The Mantissa. The Integer first.6 / 2 = 3 r 03 / 2 = 1 r 11 / 2 = 0 r 12. Fraction next..75 * 2 = 1.5.5 * 2 = 1.03. put the two parts together…Now normalise * 22= 1102= 0.112
50IEEE Biased 127 Exponent To generate a biased 127 exponent Take the value of the signed exponent and add 127.Example.216 then = and my value for the exponent would be 143 =So it is simply now an unsigned value ....
52Why Biased ? The smallest exponent 00000000 Only one exponent zeroThe highest exponent isTo increase the exponent by one simply add 1 to the present pattern.
53Back to the example Our original example revisited…. 1.1011 * 22 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 .10006.75 in 32 bit floating point IEEE representation:-sign(1) exponent(8) mantissa(23)
54Representation in IEEE 754 single precision sign bit:0 for positive and,1 for negative numbers8 biased exponent by 12723 bit normalised mantissaSignExponentMantissa
55Example (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 = 12810It is biased by 127, hence the real exponent is128 –127 = 1.The mantissa:It is normalised, hence the true mantissa is1.01 =Finally, the number represented is: x 21 = -2.501
56Single Precision Format The exponent is formatted using excess-127 notation, with an implied base of 2Example:Exponent:Representation: 135 – 127 = 8The stored values 0 and 255 of the exponent are used to indicate special values, the exponential range is restricted to2-126 to 2127The number 0.0 is defined by a mantissa of 0 together with the special exponential value 0The standard allows also values +/-∞ (represented as mantissa +/-0 and exponent 255Allows various other special conditions
57In comparisonThe smallest and largest possible 32-bit integers in two’s complement are only -232 and2017/3/28PITT CS 16215757
58Normalized (positive range; negative is symmetric) Range of numbersNormalized (positive range; negative is symmetric)smallest+2-126× (1+0) = 2-126largest+2127× (2-2-23)2127(2-2-23)2-126Positive overflowPositive underflow2017/3/28PITT CS 16215858
59Representation in IEEE 754 double precision format It uses 64 bits1 bit sign11 bit biased exponent52 bit mantissaSignExponentMantissa
60IEEE 754 double precision Biased = 1023 11-bit exponent with an excess of 1023.For example:If the exponent is -1we then add 1023 to it = 1022We then find the binary representation of 1022Which isThe exponent field will now holdThis means that we just represent -1 with an excess of 1023.
61IEEE 754 Encoding Single Precision Double Precision Represented Object ExponentFractionnon-zero+/- denormalized number1~254anything1~2046+/- floating-point numbers2552047+/- infinityNaN (Not a Number)6161
62Floating Point Representation format (summary) the sign bit represents the sign0 for positive numbers1 for negative numbersThe 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
63Character 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 0See (table 5.1 p56, study guide) for character representation in ASCII.
64ASCII – example Symbol decimal Binary 7 55 00110111 8 56 00111000 :;<=>?@ABC
65Character strings How to represent character strings? A collection of adjacent “words” (bit-string units) can store a sequence of lettersNotation: enclose strings in double quotes"Hello world"Representation convention: null character defines end of stringNull is sometimes written as '\0'Its binary representation is the number 0'H' 'e' 'l' 'l' o' ' ' 'W' 'o' 'r' 'l' 'd' '\0'
66Layered View of Representation Text stringInformationDataSequence ofcharactersInformationDataCharacterBit string
67Working 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.NEED TO PROVIDE PRINTED ASCII TABLES
68SolutionSI‘S’ ‘I’ ‘\0’The colors are intended to help you read it; computers don’t care that all the bits run together.
69exerciseUse the ASCII table to write the ASCII code for the following:CIS1106=2*3
70Unicode - representation ASCII code can represent only 128 = 27 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.
71Colour representation Colours can represented using a sequence of bits.256 colours – how many bits?Hint for calculatingTo 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 => 25624-bit colour – how many possible colors can be represented?Hints16 million possible colours (why 16 millions?)
7224-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.
74Image representationAn 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.
75Bitmap ImagesEach individual pixel (pi(x)cture element) in a graphic stored as a binary numberPixel: A small area with associated coordinate locationExample: each point below is represented by a 4-bit code corresponding to 1 of 16 shades of gray
76Representing Sound Graphically X axis: timeY axis: pressureA: amplitude (volume): wavelength (inverse of frequency = 1/)
77Sampling 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 betterThe size of the word used to represent a sample.
78Digitizing Sound Capture amplitude at these points Lose all variation between data pointsZoomed Low Frequency Signal
79Summary Integer representation Fraction representation Unsigned,Signed,Excess notation, andTwo’s complement.Fraction representationFloating point (IEEE 754 format )Single and double precisionCharacter representationColour representationSound representation
80ExerciseRepresent +0.8 in the following floating-point representation:1-bit sign4-bit exponent6-bit normalised mantissa (significand).Convert the value represented back to decimal.Calculate the relative error of the representation.