550 likes | 565 Views
Learn about decimal and binary systems, conversion methods, integer representation schemes like unsigned, sign-and-magnitude, one’s complement, and two’s complement with examples and applications.
E N D
Chapter 3 Number Representation
Number 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. It is not efficient to represent numbers in the same way as symbols representation.
3.1 DECIMAL AND BINARY
Figure 3-1 Decimal system
Figure 3-2 Binary system
3.2 CONVERSION
Figure 3-3 Binary to decimal conversion
Figure 3-4 Decimal to binary conversion
3.4 INTEGER REPRESENTATION
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.
Figure 3-6 Taxonomy of integers
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
Table 3.2 Example of storing unsigned integers in two different computers 8-bit allocation ------------ 00000111 11101010 overflow overflow overflow 16-bit allocation ------------------------------ 0000000000000111 0000000011101010 0000000100000010 0110000010111000 overflow Decimal------------ 7 234 258 24,760 1,245,678
Applications (unsigned) • Counting • Addressing
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)
Sign-and-magnitude integers # of Bits ---------- 8 16 32 Range ------------------------------------------------------- -127 -0 -32767 -0 -2,147,483,647-0 +0 +127 +0 +32767 +0 +2,147,483,647
Sign and Magnitude Representation • Convert the magnitude to binary • Add 0s to the left to make the total number of bits N-1. • If number is positive add 0 to the left; if number is negative add 1 to the left
Table 3.4 Example of storing sign-and-magnitude integers in two computers 8-bit allocation ------------ 00000111 11111100 overflow overflow 16-bit allocation ------------------------------ 0000000000000111 1000000001111100 0000000100000010 1110000010111000 Decimal------------ +7 -124 +258 -24,760
Sign and Magnitude Interpertation • Ignore the first (liftmost) bit, • Change the following bits to decimal. • Attach + or – sign based on the left most bit.
Advantages, Disadvantages, and Application (sign and magnitude) • Disadvantages: two zeros • Advantages: easy transformation • Application: data communication.
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).
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) Range ------------------------------------------------------- # of Bits --------- 8 16 32 -127 -0 -32767 -0 -2,147,483,647-0 +0 +127 +0 +32767 +0 +2,147,483,647
One’s Complements Representation • Convert the magnitude to binary • If number is positive, add 0s to the left to make the total number of bits N. • If number is negative add complement the positive representation (step 2)
Table 3.6 Example of storing one’s complement integers in two different computers 8-bit allocation ------------ 00000111 11111000 01111100 10000011 overflow overflow 16-bit allocation ------------------------------ 0000000000000111 1111111111111000 0000000001111100 1111111110000011 0110000010111000 1001111101000111 Decimal------------ +7 -7 +124 -124 +24,760 -24,760
One’s Complement Interpretation • If the leftmost 0 (positive number) • Change the entire number to decimal • Put (+) in front of the number. • If the leftmost is 1 (negative number) • Complement the entire number. • Change the entire number to decimal. • Put (-) in front of the number
Disadvantages, and Application (One’s Complement) • Disadvantages: two zeros • Application: error detection and correction.
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).
Range of two’s complement integers # of Bits --------- 8 16 32 Range ------------------------------------------------------- -128 -32,768 -2,147,483,648 0 +127 0 +32,767 0 +2,147,483,647
Two’s Complements Representation • Convert the magnitude to binary • Add 0s to the left to make the total number of bits equal to N. • 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.
Table 3.8 Example of storing two’s complement integers in two different computers 8-bit allocation ------------ 00000111 11111001 01111100 10000100 overflow overflow 16-bit allocation ------------------------------ 0000000000000111 1111111111111001 0000000001111100 1111111110000100 0110000010111000 1001111101001000 Decimal------------ +7 -7 +124 -124 +24,760 -24,760
Two’s Complement Interpretation • If the leftmost 0 (positive number) • Change the entire number to decimal • Put (+) in front of the number. • If the leftmost is 1 (negative number) • Complement after the first 1 from the right. • Change the entire number to decimal. • Put (-) in front of the number
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.
Application (Tow’s Complement) Two’s complement is used in today’s computer
Table 3.9 Summary of integer representation Unsigned ------------ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Sign-and-Magnitude --------- +0 +1 +2 +3 +4 +5 +6 +7 -0 -1 -2 -3 -4 -5 -6 -7 One’sComplement --------- +0 +1 +2 +3 +4 +5 +6 +7 -7 -6 -5 -4 -3 -2 -1 -0 Two’sComplement -------- +0 +1 +2 +3 +4 +5 +6 +7 -8 -7 -6 -5 -4 -3 -2 -1 Contents of Memory------------ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
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)
Excess System Representation • Add the magic number to the integer. • Change the result to binary • Add 0s to the left to make total number of bits equal to N.
Excess System Interpretation • Change the number to decimal • Subtract the magic number form the integer.
Excess System Range • For Magic number equal to 2N-1 the range is - (2N-1)….. +(2N-1-1) # of Bits --------- 8 16 32 Range ------------------------------------------------------- -128 -32,768 -2,147,483,648 0 +127 0 +32,767 0 +2,147,483,647
Excess System Range • For Magic number equal to 2N-1 -1 the range is - (2N-1 -1)….. +(2N-1) # of Bits --------- 8 16 32 Range ------------------------------------------------------- -127 -32,767 -2,147,483,647 0 +128 0 +32,768 0 +2,147,483,648
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.
FLOATING-POINT REPRESENTATION
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
Floating Point Representation • Convert the integer part to binary • 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. • Put a decimal point between the two parts.
Figure 3-7 Changing fractions to binary
Floating Point Interpretation • Convert the integer part to decimal • Convert the fraction part to decimal by: • multiplying each digit by its weight. • Add alltogether • Put a decimal point between the two parts.
Figure 3-3 Binary floating to decimal conversion . 0 2-5 2-6 2-7 2-1 2-2 2-3 2-4 0 + 0.25 + 0 + 0.0625 + 0.03125 +0 +0.0078125 0.3515625
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
Table 3.10 Example of normalization Move------------ 6 2 6 3 Original Number------------ +1010001.1101 -111.000011 +0.00000111001 -001110011 Original Number------------ +1010001.1101 -111.000011 +0.00000111001 -0.001110011 Normalized ------------ +26x 1.01000111001 -22x 1.11000011 +2-6 x 1.11001 -2-3x 1.110011
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.
Normalization (example) For the number: + 1000111.0101 Normalized Number: + 26 x 1.0001110101 Sign: + Exponent: 6 Mantissa: 0001110101