CMPS 375 Class Notes Chapter 2 Data Representation in Computer Systems 1. Number Systems System Radix Allowable Digits --------------------------------------------------------------------Decimal 10 0,1,2,3,4,5,6,7,8,9 Binary 2 0,1 Hexadecimal 16 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F A base-r number is represented by the following series of coefficients: ( anan-1 … a3a2a1a0 . a-1a-2a-3 ... )r where r = 2,3,4, ..., 8,9,10, ...16, ... Examples: (705)10 (01011000001)2 (2C1)16 (0.8125) 10 (0.1101)2 (0.D)16 (705.8125) 10 (01011000001.1101)2 (2C1.D)16 There are two important groups of number base conversions: 1. Conversion of decimal numbers to base-r numbers 2. Conversion of base-r numbers to decimal numbers 2. Conversion of base-10 numbers to base-r numbers ( )10 ---> ( )r where r = 2,3,4, ..., 8, ...16, ... A decimal number containing an integer part and a fraction part can be converted to a base-r number by converting these two parts separately and then combining the two answers. 2-1. Conversion of a base-10 integer to a base-r integer The Division Algorithm (The radix divide technique) Divide the given base-10 integer successively by the required base-r, noting the remainders at each step. The quotient at each step becomes the new dividend for subsequent division. Stop division when the quotient reaches zero. The series of remainders in reverse order is the equivalent base-r integer. Examples a. (705)10 ---> ( 705/2 = 352/2 = 176/2 = 88/2 = 44/2 = 22/2 = 11/2 = 5/2 = 2/2 = 1/2 = )2 quotient remainder coefficient --------- ------------- ------------352 1 a0 = 1 176 0 a1 = 0 88 0 a2 = 0 44 0 a3 = 0 22 0 a4 = 0 11 0 a5 = 0 5 1 a6 = 1 2 1 a7 = 1 1 0 a8 = 0 0 1 a9 = 1 a10 = 0 Therefore, ( 705)10 = (01011000001)2 b. ( 705)10 ---> ( )16 quotient remainder coefficient --------- ------------- ------------705/16 = 44 1 a0 = 1 44/16 = 2 C a1 = C 2/16 = 0 2 a2 = 2 Therefore, (705)10 = (2C1)16 2-2. Conversion of a decimal fraction to a base-r fraction The Multiplication Algorithm (The radix multiply technique) Multiply the base-10 fraction by the required base-r, noting the integer portion of the product at each step. Use the fractional part of the product as the multiplicand for subsequent steps. Stop when the fraction either reaches 0 or the required number of digits has been obtained. The series of integer digits at each step from first to last is the equivalent base-r fraction. Examples a. (0.8125)10 ---> ( )2 integer fraction -------- --------0.8125x2 = 1 .625 0.625x2 = 1 .25 0.25x2 = 0 .5 0.5x2 = 1 .0 coefficient -----------a-1 = 1 a-2 = 1 a-3 = 0 a-4 = 1 Therefore, (0.8125)10 = (.1101)2 b. ( 705.8125)10 ---> (01011000001.1101)2 c. (0.8125)10 ---> ( )8 integer fraction -------- --------0.8125x8 = 6 .5 0.5x8 = 4 .0 coefficient -----------a-1 = 6 a-2 = 4 Therefore, (0.8125)10 = (.64)8 3. Conversion of base-r numbers to decimal numbers ( )r ---> ( )10 where r = 2,3,4, ..., 8, ...16, ... The positional value method The decimal equivalent of a base-r number is obtained as described below: a. Each coefficient (aj) is multiplied by its positional value (rj). b. Add all the ajrj as shown below: an rn + ...+ a3r3 + a2r2 + a1r1 + a0r0 + a-1r-1 + a-2 r-2 + ...+ a-m r-m Examples a. (011001.101)2 ----> ( )10 1x24 + 1x23 + 1x20 + 1x2-1 + 1x2-3 = 16 + 8 + 1 + 0.5 + 0.125 = 25.625 Therefore, (011001.101)2 = (25.625 )10 b. (37B)16 ----> ( )10 3x162 + 7x161 + 12x160 = 3x256 + 7x16 + 12x1 = 768 + 112 +12 = 892 Therefore, (37B)16 ----> (892)10 4. Conversion between Binary and Hexadecimal Numbers ( )2 <----> ( )16 Conversion of a binary number to its hexadecimal equivalent or a hexadecimal number to its binary equivalent can easily be accomplished by merely inspecting the number. The following conversion table should be memorized for effective use of the inspection method. Hex Binary digit digits -----------------0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 Hex Binary digit digits ------------------8 1000 9 1001 A 1010 B 1011 C 1100 D 1101 E 1110 F 1111 4-1. Binary-to-Hexadecimal Conversion To convert a binary number to hexadecimal, start at the binary point and work in both directions, collecting the bits in 4-bit groups. Then convert each group to its hexadecimal equivalent. Examples a. (10101111011)2 ---> ( )16 10101111011 101 0111 1011 = 5 7 B Therefore, (10101111011)2 = (57B)16 b. (1011100101.1010011)2 ---> ( )16 1011100101.1010011 = 0010 1110 0101 . 1010 0110 = 2 E 5 . A 6 Therefore, (1011100101.1010011)2 = (2E5.A6)16 4-2. Hexadecimal-to-Binary Conversion To convert a hexadecimal number to binary, simply translate each hexadecimal digit to its 4-bit binary equivalent. Then eliminate any leading zeros in integer and any trailing zeros in the fraction. 5. Complement Systems 5.1 Radix Complement and Diminished Radix Complement Complements are used in digital computers for simplifying the subtraction operation and logical manipulations. Since A – B = A + (-B) and complement systems provide a convenient way of representing negative numbers, we can convert a subtraction to an addition. When implemented with digital hardware, it is more efficient to use complements than to use direct subtraction. Since multiplication and division correspond to repeated addition and subtraction, respectively, it is possible to perform the four basic arithmetic operations using only the hardware for addition when negative numbers are represented in complement form. There are two types of complements for each base-r system: (1) radix complement and (2) diminished radix complement. The first is referred to as the r’s complement and the second as the (r-1)’s complement as shown in the following examples. In the decimal system (r=10), the radix complement is 10’s complement and the diminished radix complement is 9’s complement. In the octal system (r=8), the radix complement is 8’s complement and the diminished radix complement is 7’s complement. In the binary system (r=2), the radix complement is 2’s complement and the diminished radix complement is 1’s complement. Given a number N in base r, or (N)r, it is easier to find the diminished complement than to find the radix complement. The diminished complement of a base r number can easily be found by subtracting each digit of the number from (r-1). The radix complement can then be obtained by adding 1 to the diminished radix complement. A. Decimal system (r=10) The radix complement is 10’s complement and the diminished radix complement is 9’s complement. The 9's complement of a decimal number is obtained by subtracting each digit from 9. B. Octal system (r=8) The radix complement is 8’s complement and the diminished radix complement is 7’s complement. The 7's complement of an octal number is obtained by subtracting each digit from 7. C. Binary system (r=2) The radix complement is 2’s complement and the diminished radix complement is 1’s complement. The 1's complement of a binary number is obtained by subtracting each digit from 1. That is, inverting each bit of the number. Examples a. Find the 10’s complement and the 9's complement of (546700)10 (546700)10 = (453299)10 = (453300)10 9's complement 10’s complement b. Find the 8's complement and the 7's complement of (526071)8 (526071)8 = (251706)8 = (251707)8 7's complement 8's complement c. Find the 2's complement the 1's complement of (00011010)2 (00011010)2 = (11100101)2 = (11100110)2 1's complement 2's complement 5.2 Subtraction with Complement The subtraction of two n-digit unsigned numbers M – N in base r can be done as follows: 1. Add the r’s complement of the subtrahend, N to the minuend, M. This performs M+(-N). 2. If M >= N, the sum will produce an end carry and the end carry is discarded. 3. If M < N, the sum does not produce an end carry. To obtain the answer in a familiar form, take the r’s complement of the sum and place a negative sign in front. Example 1. Using base-10 integers and10’s complement, subtract (214)10 – (39)10 214 + (-39) ---------- 214 + 961 (-39 =10’s complement of 039 = 961) -----------Sum = 1175 Answer= 175 (end carry is discarded) Example 2. Using base-10 integers and 10’s complement, subtract (28)10 – (174)10 28 + (-174) ----------- 028 + 826 (-174 =1 0’s complement of 174 = 826) -----------Sum = 854 Answer= -146 (10’s complement of 854 = -146) Example 3. Using base-2 integers and 2’s complement, subtract (214) – (39) 214 + (-39) ------------- 011010110 + 111011001 ------------------Sum = 1010101111 Answer = 010101111 = (175) 10 (-39 = 2’ complement of 39 = 111011001) (end carry is discarded) Example 4. Using base-2 integers and 2’s complement, subtract (28)10 – (174)10 28 + (-174) ------------- 000011100 + 101010010 -----------------Sum = 101101110 If M (28) < N (174), the sum does not produce an end carry. To obtain the answer, take the 2’s complement of the sum and place a negative sign in front. Answer = - (010010010) 2 = - 146 6. Internal Representation of Integer Type Data 6-1. Positive Numbers a.. Decimal 26 in an 8-bit byte (26)10 = (00011010)2 = (1A)16 b. Decimal 26 in an 16-bit byte (26)10 = (0000000000011010)2 = (001A)16 6-2. Negative Numbers a. Decimal -26 in an 8-bit byte -26 = -(26)10 = -(00011010)2 = (11100101)2 = (11100110)2 = (E6)16 1's complement 2's complement 2's complement b. Decimal -26 in a 16-bit word -26 = -(26)10 = -(0000000000011010)2 = (1111111111100101)2 1's complement = (1111111111100110)2 2's complement = (FFE6)16 6-3. Interpretation of Internal Integer Type Data The leftmost bit of a memory byte or word of a number indicates whether the number is positive or negative. If the bit is 0, the number is positive. If the bit is 1, the number is negative and is in the 2’s complement. The conversion of the number to its decimal equivalent is illustrated in the examples below. Examples a. (7C)16 = ( ? )10 (7C)16 = (0111 1100)2 = (124)10 b. (8B)16 = ( ? )10 (8B)16 = (10001011)2 = (10001010)2 = -(01110101)2 = -(117)10 or (8B)16 = (10001011)2 = - (01110101)2 = - (117)10 (2’s complement) (1’s complement) (1’s complement of 10001010 is 01110101) (2’s complement) (2’s complement of 10001011 is 01110101) 7. Internal Representation of Floating-Point Type Data 2.5.1. A Simple Model (Textbook: pp.56-59) Floating-Point Representation 1 bit 5 bits Sign bit Exponent 8 bits Mantissa (a) Unbiased exponent 00000 for actual exponent 20 00101 for actual exponent 25 (b) Biased exponent 10000 for actual exponent 20 100100 for actual exponent 24 It is more efficient to use a biased exponent and the biased exponent with a bias of 16 (excess-16) will be used in this floating-point format. Example: Internal representation of (10.25)10 (10.25)10 = (1010.01) 2 = (1010.01) 2 x 20 = (101.001) 2 x 21 : = (.101001) 2 x 24 = (.0101001) 2 x 25 = (.00101001) 2 x 26 Internal representation of (10.25)10 is (Un-normalized form) . . (Normalized form) (Un-normalized form) . 0 10100 10100100 A normalized form is used for storing a floating-point number in memory. Note that a normalized form is a floating-point representation where the leftmost bit of the mantissa will always be a 1. 2.5.3. Floating-Point Error 8. Internal Representation of Character Type Data ASCII Character Code Character binary hexadecimal ------------------------------------------ . bell LF FF CR . . space . . 0 1 . . A B . . a b . 00000111 00001010 00001100 00001101 07 0A 0C 0D 00100000 20 00110000 00110001 30 31 01000001 01000010 41 42 01100001 01100010 61 62 9. Binary Integer Arithmetic Since A – B = A + (-B), we can accomplish subtraction by adding the complement of B to A. The complement method is more efficient with digital hardware. a. Binary implementation of (13)10 + (6) 10. 13 + 6 ---------19 00001101 + 00000110 -----------------00010011 b. Binary implementation of (13)10 - (6) 10. 13 + (-6) ----------7 00001101 + 11111010 -----------------00000111 (-6 in 2’s complement) Note that the overflow bit (1) is discarded in the 2’s complement arithmetic. 10. Error-Detection Code Errors may occur whenever data are transmitted between two devices. One of the most common ways to facilitate error detection is by means of a parity bit. A parity bit is an extra bit included as part of the data to make the total number of 1s transmitted either odd or even. a. Odd Parity Method b. Even Parity Method Examples: 8-bit Odd Parity Generator --------------------------------Data P ------------ -----00100000 0 00110000 1 01100101 1 8-bit Even Parity Generator ---------------------------------Data P ----------- -----00100000 1 00110000 0 01100101 0