Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 3 Number Representation ©Brooks/Cole, 2003 Number representation It is not efficient to represent numbers in the same way as symbols representation. For example representing 67854 as symbols using ASCII requires 5 bytes each representing a digit. And it is obvious That as the number of digits increases the number of bytes Increases. Therefore we need a more efficient way to represent numbers. ©Brooks/Cole, 2003 3.1 DECIMAL AND BINARY ©Brooks/Cole, 2003 Figure 3-1 Decimal system ©Brooks/Cole, 2003 Figure 3-2 Binary system ©Brooks/Cole, 2003 3.2 CONVERSION ©Brooks/Cole, 2003 Figure 3-3 Binary to decimal conversion ©Brooks/Cole, 2003 Figure 3-4 Decimal to binary conversion ©Brooks/Cole, 2003 3.4 INTEGER REPRESENTATION ©Brooks/Cole, 2003 Figure 3-5 Range of integers •Integers are whole numbers without a fraction. For example,134 is an integer but 134.987 is not. • Integers can be positive and negative. ©Brooks/Cole, 2003 Figure 3-6 Taxonomy of integers ©Brooks/Cole, 2003 Unsigned integers • The maximum unsigned integer depends on the number of bits allocated in the computer to store an unsigned integer. • For N number of bits, the maximum integer to be represented is (2N -1) # of Bits --------8 16 Range ------------------------------------0 255 0 65,535 ©Brooks/Cole, 2003 Table 3.2 Example of storing unsigned integers in two different computers Decimal 8-bit allocation 16-bit allocation -----------7 234 258 24,760 1,245,678 -----------00000111 11101010 overflow overflow overflow -----------------------------0000000000000111 0000000011101010 0000000100000010 0110000010111000 overflow ©Brooks/Cole, 2003 Applications (unsigned) • Counting • Addressing ©Brooks/Cole, 2003 Signed integer: 1.Sign and magnitude • In this format one bit is needed to represent the sign of integer number (0 for positive integer and 1 for negative) • For N number of bits allocated for the signed number, the range of integer numbers is given by: -(2N-1 – 1) …… (2N-1 -1) ©Brooks/Cole, 2003 Sign-and-magnitude integers # of Bits ---------8 16 32 Range ------------------------------------------------------+127 -127 -0 +0 +32767 -32767 -0 +0 +2,147,483,647 -2,147,483,647 -0 +0 ©Brooks/Cole, 2003 Sign and Magnitude Representation 1. Convert the magnitude to binary 2. Add 0s to the left to make the total number of bits N-1. 3. If number is positive add 0 to the left; if number is negative add 1 to the left ©Brooks/Cole, 2003 Table 3.4 Example of storing sign-and-magnitude integers in two computers Decimal 8-bit allocation 16-bit allocation -----------+7 -124 +258 -24,760 -----------00000111 11111100 overflow overflow -----------------------------0000000000000111 1000000001111100 0000000100000010 1110000010111000 ©Brooks/Cole, 2003 Sign and Magnitude Interpertation 1. Ignore the first (liftmost) bit, 2. Change the following bits to decimal. 3. Attach + or – sign based on the left most bit. ©Brooks/Cole, 2003 Advantages, Disadvantages, and Application (sign and magnitude) 1. Disadvantages: two zeros 2. Advantages: easy transformation 3. Application: data communication. ©Brooks/Cole, 2003 Signed integer: 2. One’s Complement • In this format the negative number is the complement of the positive number, for example -7 is represented as the binary complement of +7. • The number’s complement is obtained by changing all 0s to 1s and all 1s to 0s. • The left most digit is assigned for the number’s sign (0 for positive, 1 for negative). ©Brooks/Cole, 2003 Range of one’s complement integers For N number of bits allocated for the signed number, the range of integer numbers is given by: -(2N-1 – 1) …… (2N-1 -1) # of Bits --------8 16 32 Range -------------------------------------------------------127 -0 +0 +127 -32767 -0 +0 +32767 -2,147,483,647 -0 +0 +2,147,483,647 ©Brooks/Cole, 2003 One’s Complements Representation 1. Convert the magnitude to binary 2. If number is positive, add 0s to the left to make the total number of bits N. 3. If number is negative add complement the positive representation (step 2) ©Brooks/Cole, 2003 Table 3.6 Example of storing one’s complement integers in two different computers Decimal 8-bit allocation 16-bit allocation -----------+7 -7 +124 -124 +24,760 -24,760 -----------00000111 11111000 01111100 10000011 overflow overflow -----------------------------0000000000000111 1111111111111000 0000000001111100 1111111110000011 0110000010111000 1001111101000111 ©Brooks/Cole, 2003 One’s Complement Interpretation • If the leftmost 0 (positive number) 1. Change the entire number to decimal 2. Put (+) in front of the number. • If the leftmost is 1 (negative number) 1. Complement the entire number. 2. Change the entire number to decimal. 3. Put (-) in front of the number ©Brooks/Cole, 2003 Disadvantages, and Application (One’s Complement) 1. Disadvantages: two zeros 2. Application: error detection and correction. ©Brooks/Cole, 2003 Signed integer: 3. Two’s Complement • Two’s complement solves the problems of two zeros. • Two’s complement is widely used representation of integers today. • For N number of bits allocated for the signed number, the range of integer numbers is given by: -(2N-1) …… (2N-1 -1) • The left most digit shows the number’s sign (0 for positive, 1 for negative). ©Brooks/Cole, 2003 Range of two’s complement integers # of Bits --------8 16 32 Range ------------------------------------------------------0 +127 -128 0 +32,767 -32,768 0 +2,147,483,647 -2,147,483,648 ©Brooks/Cole, 2003 Two’s Complements Representation 1. Convert the magnitude to binary 2. Add 0s to the left to make the total number of bits equal to N. 3. If the number is positive no action is needed. If the number is negative, complement after the first 1 from the right of the positive representation. ©Brooks/Cole, 2003 Table 3.8 Example of storing two’s complement integers in two different computers Decimal 8-bit allocation 16-bit allocation -----------+7 -7 +124 -124 +24,760 -24,760 -----------00000111 11111001 01111100 10000100 overflow overflow -----------------------------0000000000000111 1111111111111001 0000000001111100 1111111110000100 0110000010111000 1001111101001000 ©Brooks/Cole, 2003 Two’s Complement Interpretation • If the leftmost 0 (positive number) 1. Change the entire number to decimal 2. Put (+) in front of the number. • If the leftmost is 1 (negative number) 1. Complement after the first 1 from the right. 2. Change the entire number to decimal. 3. Put (-) in front of the number ©Brooks/Cole, 2003 Note: Two’s complement can be achieved by reversing all bits except the rightmost bits up to the first 1 (inclusive). If you two’s complement a positive number, you get the corresponding negative number. If you two’s complement a negative number, you get the corresponding positive number. If you two’s complement a number twice, you get the original number. ©Brooks/Cole, 2003 Application (Tow’s Complement) Two’s complement is used in today’s computer ©Brooks/Cole, 2003 Table 3.9 Summary of integer representation Contents of Memory -----------0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Unsigned -----------0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Sign-andMagnitude --------+0 +1 +2 +3 +4 +5 +6 +7 -0 -1 -2 -3 -4 -5 -6 -7 One’s Complement --------+0 +1 +2 +3 +4 +5 +6 +7 -7 -6 -5 -4 -3 -2 -1 -0 Two’s Complement -------+0 +1 +2 +3 +4 +5 +6 +7 -8 -7 -6 -5 -4 -3 -2 -1 ©Brooks/Cole, 2003 Signed integer: Excess System • Excess system is used to store signed integer numbers in computer. • In Excess system a positive number called magic number is used for this format. • The magic number is either 2N-1 or 2N-1-1. For example if N = 8 the magic number is either 128 (called excess_128) or 127 (called excess_127) ©Brooks/Cole, 2003 Excess System Representation 1. Add the magic number to the integer. 2. Change the result to binary 3. Add 0s to the left to make total number of bits equal to N. ©Brooks/Cole, 2003 Excess System Interpretation 1. Change the number to decimal 2. Subtract the magic number form the integer. ©Brooks/Cole, 2003 Excess System Range 1. For Magic number equal to 2N-1 the range is - (2N-1)….. +(2N-1-1) # of Bits --------8 16 32 Range ------------------------------------------------------0 +127 -128 0 +32,767 -32,768 0 +2,147,483,647 -2,147,483,648 ©Brooks/Cole, 2003 Excess System Range 1. For Magic number equal to 2N-1 -1 the range is - (2N-1 -1)….. +(2N-1) # of Bits --------8 16 32 Range ------------------------------------------------------0 +128 -127 0 +32,768 -32,767 0 +2,147,483,648 -2,147,483,647 ©Brooks/Cole, 2003 Advantages, Disadvantages and Application (Excess System) • Advantages: Easy transformation. • Disadvantages: operation on excess system is very complicated. • Applications: used in storing exponential values of fraction. ©Brooks/Cole, 2003 FLOATING-POINT REPRESENTATION ©Brooks/Cole, 2003 Floating Point Number • Floating point number is a number containing an integer and a fraction • For example in floating point number 21.345, the integer part is 21 and the fraction is 0.345 ©Brooks/Cole, 2003 Floating Point Representation 1. Convert the integer part to binary 2. Convert the fraction part to binary by repetitively multiply the fraction part by 2. The binary digit is the integer part of the result of multiplication. Stop whenever the fraction part becomes 0 or when you reach the maximum number of bits you can use. 3. Put a decimal point between the two parts. ©Brooks/Cole, 2003 Figure 3-7 Changing fractions to binary ©Brooks/Cole, 2003 Floating Point Interpretation 1. Convert the integer part to decimal 2. Convert the fraction part to decimal by: 1. multiplying each digit by its weight. 2. Add altogether 3. Put a decimal point between the two parts. ©Brooks/Cole, 2003 Figure 3-3 0 Binary floating to decimal conversion . 2-1 2-2 2-3 2-4 2-5 2-6 2-7 0 + 0.25 + 0 + 0.0625 + 0.03125 +0 +0.0078125 0.3515625 ©Brooks/Cole, 2003 Normalization • Normalization is a standard way to write floating point binary number. • Normalization is achieved by moving the decimal point so that there is only one 1 to the left of it. • To indicate how many digits the decimal point has moved, multiply the number by 2e which indicates the direction and number of digits moved. • If : – e is positive, decimal point moved to the left – e is negative, decimal point moved to the right ©Brooks/Cole, 2003 Table 3.10 Example of normalization Original Original Number Number Move ---------------------------------6 +1010001.1101 +1010001.1101 +26 2 -111.000011 -111.000011 -22 6 +0.00000111001 +0.00000111001 +2-6 3 -001110011 -0.001110011 -2-3 Normalized -----------x 1.01000111001 x 1.11000011 x 1.11001 x 1.110011 ©Brooks/Cole, 2003 Sign, Exponent and Mantissa • After the number normalization you store only three pieces of information about the number: sign, exponent and mantissa. – Sign is the number sign (stored in 1 bit) – Exponent (power of 2) shows direction and magnitude of decimal point movement. – Mantissa is the bits to the right of the decimal point. ©Brooks/Cole, 2003 Normalization (example) For the number: + 1000111.0101 Normalized Number: + 26 x 1.0001110101 Sign: + Exponent: 6 Mantissa: 0001110101 ©Brooks/Cole, 2003 Figure 3-8 IEEE standards for floating-point representation ©Brooks/Cole, 2003 Single Precision Representation • • • • Store the sign as 0 (positive) or 1 (negative) Store the exponent (power of 2) as Excess_127 Store the mantissa as an unsigned integer. Add 0s to the right of the mantissa to make the total number of mantissa bits equal to 23 ©Brooks/Cole, 2003 Table 3.11 Example of floating-point representation -22 +2-6 -2-3 Number -----------x 1.11000011 x 1.11001 x 1.110011 Sign ---1 0 1 Exponent ----------10000001 01111001 01111100 Mantissa ------------------------------11000011000000000000000 11001000000000000000000 11001100000000000000000 ©Brooks/Cole, 2003 Floating point Interpretation (Single Precision) • The sign is the leftmost bit • Change the next 8 bits to decimal and subtract 127 from it. • add 1 to the left of the mantissa (next 23 bits) and discard the zeros to the left of it. • Move the decimal point to its original position. • Convert the integer and fraction to decimal ©Brooks/Cole, 2003 Hexadecimal Notation • Floating point numbers can be written using hexadecimal notation. • For example : 81.125, using IEEE standards is 01000010101000111001000000000000 And in Hexadecimal notation: x42A39000 ©Brooks/Cole, 2003