CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 3: 9/3/2002 Lecture 3: 9/3/2002 CS149D Fall 2002 1 Outline •Fractions in Binary •Storing Integers •Sign and magnitude (covered last lecture) •Two’s complement (covered last lecture) •Excess Notation •Storing Fractions •Representing Text •Representing Images •Data Compression By now, we should have covered sections 1.4-1.8 from Brookshear Text Lecture 3: 9/3/2002 CS149D Fall 2002 2 Fractions In Binary1/3 •Use radix point just like decimal •To the right of radix point positions are numbered –1, -2 , …. 210 -1-2-3 i 101.101 d 101.101 = (1 * 20) + (0 * 21) + (1*22) + (1*2-1) + (0* 2-2) + (1 * 2-3) = Lecture 3: 9/3/2002 5 1 + 4 + ½ + 1/8 = 5 8 CS149D Fall 2002 3 Fractions In Binary2/3 Express the following in binary notation 35 16 •Convert the integer part •Convert the fraction part. Try to map the fraction as a sum of power of 2 fractions using the given denominator as a guide •Put a radix point in between 310 is 112 3 5 11.01012 16 Lecture 3: 9/3/2002 5 1 4 1 1 0.01012 16 16 16 16 4 Make sure that CS149D Fall 2002 11.01012 3 5 16 4 Fractions In Binary3/3 Addition of binary numbers with radix points •Align radix point •Apply binary addition process 10.011 + 100.11 10.011 + 100.11 __________ 111.001 Lecture 3: 9/3/2002 CS149D Fall 2002 5 Storing Integers1/3 •Binary number system not used as is to actually store an integer number •Use a numeric storage technique Sign and Magnitude Problems: Two zeros (+0, and –0), adding +2 and –2 does not give zero. Straightforward binary addition does not apply Two’s complement Advantages: deal with subtractions the same as additions, simpler hardware to perform integer arithmetic operations Excess Notation Lecture 3: 9/3/2002 CS149D Fall 2002 6 Storing Integers2/3 Excess Notation •All positive numbers begin with 1 Excess four notation Bit pattern Value Unsigned Binary •All negative numbers begin with 0 •0 is represented as 100 111 3 7 110 2 6 101 1 5 100 0 4 011 -1 3 010 -2 2 001 -3 1 •Smallest negative number is 000 000 -4 0 •Largest positive number is 111 Lecture 3: 9/3/2002 CS149D Fall 2002 •Unsigned binary value exceeds excess notation by 4, hence the name excess four • Why 4? (2#of bits –1) = 23-1 = 4 7 Storing Integers3/3 What is 101 in excess four notation? 101 if interpreted unsigned is 5 101 in excess four notation is (5-4) = 1 What is 3 represented in excess four notation? Excess four means we need (4-1) = 3 bits for representation Remember unsigned value exceeds excess notation by 4 Then get unsigned value = 3+4 = 7 Represent 7 in 3-bit binary = 111 Lecture 3: 9/3/2002 CS149D Fall 2002 3 in excess four is 111 8 Storing Fractions1/6 Need to represent number and position of radix point Use floating-point notation (Textbook uses one-byte as example) - --- ---- 8 bits = 1-byte 1 bit: Sign bit (0 positive, 1 negative) 3 bits: Exponent (encodes position of radix point) 4 bits: Mantissa (encodes number) Lecture 3: 9/3/2002 CS149D Fall 2002 9 Storing Fractions2/6 Bit pattern 01101011 in floating-point notation is what in decimal? •First bit is 0, then positive •Exponent is 110 mantissa is 1011 •Extract mantissa and place a radix point on its left side 0.1011 •Extract exponent and interpret as excess four notation •110 in excess four is +2 (make sure?) •+2 exponent means move radix point to the right by two bits (a negative exponent means move radix to left) •0.1011 becomes 10.11 10.11 is 23 4 Lecture 3: 9/3/2002 3 01101011 is 2 4 CS149D Fall 2002 10 Storing Fractions3/6 Bit pattern 10111100 in floating point is what in decimal? •First bit is 1, then negative •Exponent is 011 mantissa is 1100 •Extract mantissa and place a radix point on its left side 0.1100 •Extract exponent and interpret as excess four notation •011 in excess four is -1 (make sure?) •-1 exponent means move radix point to the left by 1 bit •0.1100 becomes 0.01100 0.01100 is Lecture 3: 9/3/2002 3 8 3 10111100 is 8 CS149D Fall 2002 11 Storing Fractions4/6 What is the following number stored in floating-point? 11 8 •Express number in binary to obtain 1.001 (make sure?) •Copy bit pattern into mantissa field from left to right starting with the leftmost 1 in binary representation •Mantissa is 1001 •Compute exponent to get 1.001 from .1001 (imagine mantissa with radix point at its left) •need to move radix point to right one bit •Exponent is +1 expressed in excess four notation is 101 (How?) Sign: 0 (positive) Exponent: 101 11 01011001 8 Mantissa: 1001 Lecture 3: 9/3/2002 CS149D Fall 2002 12 Storing Fractions5/6 What is the following number stored in floating-point? 1 4 •Express number in binary to obtain 0.01 (make sure?) •Copy bit pattern into mantissa field from left to right starting with the leftmost 1 in binary representation •Mantissa is 1000 (you append zeros to fill the 4-bit mantissa) •Compute exponent to get 0.01 from 0.1000 •need to move radix point to left one bit •Exponent is -1 expressed in excess four notation is 011 (How?) Sign: 1 (negative) Exponent: 011 1 10111000 4 Mantissa: 1000 Lecture 3: 9/3/2002 CS149D Fall 2002 13 Storing Fractions6/6 What is the following number stored in floating-point? 25 8 •Express number in binary to obtain 10.101 (make sure?) •Copy bit pattern into mantissa field from left to right starting with the leftmost 1 in binary representation •Mantissa is 1010 (we run out of bits!! Rightmost 1 is lost (equivalent to 1/8)) •Compute exponent to get 10.101 from 0.1010 •need to move radix point to right two bits •Exponent is +2 expressed in excess four notation is 110 Sign: 0 (positive) Exponent: 110 25 8 Mantissa: 1010 Lecture 3: 9/3/2002 CS149D Fall 2002 Truncation error Round off error 21 01101010 2 which is really 14 Representing Text1/2 ASCII (adopted by American National Standards Institute ANSI) •American Standard Code for Information Interchange •8-bit to represent each symbol •Upper and lower case letters of English alphabet, punctuation symbols, digits 0 to 9, and other symbols •Can represent 256 (28) different symbols Unicode •16-bit to represent each symbol •Can represent 65,536 (216) different symbols ISO (International Organization for Standardization) •32-bit to represent each symbol •Can represent more than 17 million symbols Lecture 3: 9/3/2002 CS149D Fall 2002 15 Representing Text2/2 ASCII Chart sample •Upper case A is 6510 •Lower case a is 9710 •Difference between lower case and upper case of a letter is always 3210 Lecture 3: 9/3/2002 Decimal Hex ASCII Binary 65 66 67 68 69 41 42 43 44 45 A B C D E 0100 0001 0100 0010 0100 0011 0100 0100 0100 0101 48 49 50 51 52 53 54 55 56 57 30 31 32 33 34 35 36 37 38 39 0 1 2 3 4 5 6 7 8 9 0011 0000 0011 0001 0011 0010 0011 0011 0011 0100 0011 0101 0011 0110 0011 0111 0011 1000 0011 1001 97 98 99 100 101 61 62 63 64 65 a b c d e 0110 0001 0110 0010 0110 0011 0110 0100 0110 0101 CS149D Fall 2002 16 Representing Images Bitmap techniques •Image is a collection of pixels (picture element) •Each pixel can be represented as a number of bits (collection of bits is bitmap) 1 bit/pixel B/W 8 bits/pixel Gray Scale (different shades of gray from black to white) 24 bits/pixel 1-byte for each of the primary colors RGB •Size? (need for compression) Vector techniques •Image represented as collection of lines and curves •Fonts on printers scalable Fonts (True Type) •CAD (Computer Aided Design) •Quality problem Lecture 3: 9/3/2002 CS149D Fall 2002 17 Data Compression1/3 Data compression techniques •run-length encoding replace repeating sequences with a code indicating the value being repeated and the number of times it is repeated, e.g., aaaaa is replaced by a5 •relative encoding record differences between consecutive data blocks, e.g., consecutive frames in a motion picture •Frequency-dependent encoding (variable length code) number of bits to represent an item is inversely related to the frequency of the item’s use, e.g., (e, t, a, and i) in English would be represented by short bit patterns, whereas (z, q, and x) would be represented by longer patterns •Adaptive dictionary encoding •Example is LZ77 (Lempel-Ziv) •abaabcb (5,4,a) when decompressed is abaabcbaabca (see Textbook page 61) Lecture 3: 9/3/2002 CS149D Fall 2002 18 Data Compression2/3 Image Compression GIF (Graphic Interchange Format) •8 bits/pixel (1 byte/pixel) instead of 24 bits/pixel (3 bytes/pixel) •Each of 256 potential pixel values are associated with a RGB combination by means of table known as the palette JPEG (Joint Photographic Experts Group) Lossless mode •store difference between consecutive pixels rather than pixel intensities themselves •Differences encoded using variable-length code •Resulting bit maps not manageable with today’s technology Base standard (lossy) Each pixel represented by a brightness component and 2 color components Lecture 3: 9/3/2002 CS149D Fall 2002 19 Data Compression3/3 Audio and Video compression MPEG (Motion Picture Experts Group) •Start a picture sequence with an image similar to JPEG and then represent rest of sequence using relative encoding techniques •MP3 (MPEG-1 Audio Layer-3) audio compression ratios of 12 to 1 Lecture 3: 9/3/2002 CS149D Fall 2002 20