CSC 2400: Computer Systems Information as Bits
What kinds of data do we need to represent? - Numbers – integers, floating point, … - Text – characters, strings, … - Images – pixels, colors, shapes, … - Sound - Instructions - …
Integers
Unsigned Integers q An n -bit unsigned integer represents 2 n values: from 0 to 2 n -1 q Example for n = 3: 2 2 2 1 2 0 0 0 0 0 0 0 1 1 0 1 0 2 0 1 1 3 1 0 0 4 1 0 1 5 1 1 0 6 7 = 2 3 - 1 1 1 1
Signed Integers q How do computers differentiate between positive and negative integers? - Positive integers have the most significant bit (left bit) 0 - Negative integers have the most significant bit (left bit) 1 q Negative integer representations: 1. Sign-Magnitude 2. One’s Complement 3. Two’s Complement
1. Sign-Magnitude q Reserve the leftmost bit to represent the sign: 0 means positive 1 means negative q Examples 0 0 1 0 1 1 0 0 è 44 1 0 1 0 1 1 0 0 è -44 Magnitude Sign q Hard to do arithmetic this way, so it is rarely used - What is the result of 44 – 44?
Exercise • Assume 8-bit sign-magnitude representation for integers • What is the decimal value of 11010110
Exercise q Assume 8-bit sign-magnitude representation for integers q What is the smallest value you can represent in this system? q What is the largest value you can represent in this system?
1. Sign-Magnitude (contd.) 0 0 1 0 1 1 0 0 è 44 1 0 1 0 1 1 0 0 è -44 Sign Magnitude q For numbers represented on n bits: - Range of positive integers: from 0 to (2 n-1 – 1) - Range of negative integers: from –( 2 n-1 – 1) to – 1
2. One’s Complement q Leftmost bit is 0 for positive numbers 0 0 1 0 1 1 0 0 è 44 q To obtain the corresponding negative number (-44), flip every bit: 1 1 0 1 0 0 1 1 è -44
2. One’s Complement (contd.) q What is the result of 44 – 44? 0 0 1 0 1 1 0 0 ( 44) 1 1 0 1 0 0 1 1 (-44) _______________ q Issue: two different representations for zero
3. Two’s Complement q Leftmost bit is 0 for positive numbers 0 0 1 0 1 1 0 0 è 44 q To obtain the corresponding negative number - 44, add 1 to the one’s complement of 44: 1 1 0 1 0 0 1 1 è one’s complement + 0 0 0 0 0 0 0 1 _______________ 1 1 0 1 0 1 0 0 è two’s complement
3. Two’s Complement (contd.) q What is the result of 44 – 44? 0 0 1 0 1 1 0 0 ( 44) 1 1 0 1 0 1 0 0 (-44) _______________ q Used by most computer systems q For numbers represented on n bits: - Range of positive integers: from 0 to ________ - Range of negative integers: from – 2 n-1 to ________
Exercise • Assume 8-bit two’s complement representation for integers • What is the decimal value of 11010110
Exercises q Assuming 4-bit two's complement representation, what is the decimal value of 1011 (2) ? q Assuming 5-bit two's complement representation, what is the decimal value of 1011 (2) ? q What is -2 in 4-bit two’s complement representation? q What is -2 in 6-bit two’s complement representation?
Exercise q Assume 8-bit 2’s complement representation for integers q What is the smallest value you can represent in this system? q What is the largest value you can represent in this system?
Two’s Complement to Decimal (Method 1) 1. If leading bit is one, take two’s complement to get a positive number n 2 n 2. Convert to decimal: add powers of 2 that 0 1 have “1” in corresponding bit positions 1 2 2 4 3. If original number was negative, 3 8 add a minus sign 4 16 5 32 X = 01101000 (2) 6 64 7 128 = 2 6 +2 5 +2 3(10) = 64+32+8 (10) 8 256 = 104 (10) 9 512 10 1024 Assuming 8-bit two’s complement numbers.
Another Example Assume 8-bit two’s complement numbers. X = 11100110 (2) 1. Leading bit is one, so take two’s complement to get a positive number -X = 00011001 + 00000001 (2) = 00011010 (2) 2. Convert to decimal -X = 2 4 +2 3 +2 1(10) = 16+8+2 (10) = 26 (10) 3. Add a minus sign X = -(-X) = -26 (10)
More Examples n 2 n X = 00100111 two 0 1 = 2 5 +2 2 +2 1 +2 0 = 32+4+2+1 1 2 = 39 ten 2 4 3 8 4 16 X = 11100110 two 5 32 6 64 -X = 00011010 7 128 = 2 4 +2 3 +2 1 = 16+8+2 8 256 = 26 ten 9 512 10 1024 X = -26 ten Assuming 8-bit 2 ’ s complement numbers.
Two’s Complement to Decimal (Method 2) q Most significant (left, sign) bit has weight –2 n-1 q All other bits have positive weights q Example: 4-bit two’s complement -2 3 2 2 2 1 2 0 -2 3 2 2 2 1 2 0 0 0 0 0 0 1 0 0 0 -8 0 0 0 1 1 1 0 0 1 ___ 0 0 1 0 2 1 0 1 0 ___ 0 0 1 1 3 1 0 1 1 ___ 0 1 0 0 4 1 1 0 0 ___ 0 1 0 1 5 1 1 0 1 ___ 0 1 1 0 6 1 1 1 0 ___ 0 1 1 1 7 1 1 1 1 ___
Two’s Complement to Decimal (Method 2) q Most significant (left, sign) bit has weight –2 n-1 q All other bits have positive weights q Example: 4-bit two’s complement -2 3 2 2 2 1 2 0 -2 3 2 2 2 1 2 0 0 0 0 0 0 1 0 0 0 -8 0 0 0 1 1 1 0 0 1 -7 0 0 1 0 2 1 0 1 0 -6 0 0 1 1 3 1 0 1 1 -5 0 1 0 0 4 1 1 0 0 -4 0 1 0 1 5 1 1 0 1 -3 0 1 1 0 6 1 1 1 0 -2 0 1 1 1 7 1 1 1 1 -1
Binary Number Representation Summary q Leftmost bit 0 indicates positive number q Leftmost bit 1 indicates negative number q To negate a binary value: - sign-magnitude: flip the sign bit - one’s complement: take the one’s complement - two’s complement: take the two’s complement q Binary to decimal (two’s complement): - normal conversion from binary to decimal, with the most significant bit having negative weight
Fill in the Table Bit Value Value Value Pattern (Sign Magnitude) (One’s Complement) (Two’s Complement) 000 001 010 011 100 101 110 111
Fill in the Table Bit Value Value Value Pattern (Sign Magnitude) (One’s Complement) (Two’s Complement) 000 001 010 011 100 -0 -3 -4 101 -1 -2 -3 110 -2 -1 -2 111 -3 -0 -1
Sign Extension q Assuming 8-bit two’s complement numbers, write -5 in binary. q Method 1: - Represent 5 (10) = ________ (2) - Take the two’s complement: ________ (2) + 1 = ________ (2) q Method 2: - Represent -5 = -8+2+1 on a minimum number of bits: 1011 - Use sign extension to fill in all 8 bits (value does not change): 11111011
Question q What decimal value does 10011001 (2) represent?
Floating-Point Numbers IEEE Floating-Point Standard
Floating-Point Numbers q Decimal System: 11.625 analyzed as 10 1 10 0 10 -1 10 -2 10 -3 1 1 . 6 2 5 11.625 = (1 x 10) + 1 + (6 x 10 -1 ) + (2 x 10 -2 ) + (5 x 10 -3 ) q Binary System:
Floating-Point Numbers You try it: 10010.01001 (2) = ____________________________ (10)
How to Store Floating-Point Numbers? q We have no way to store the point separating the whole part from the fractional part! q Standard committees (IEEE) came up with a way to store floating point numbers
Floating-Point Normalization q Every floating-point binary number ( except for zero ) can be normalized by choosing the exponent so that the radix point falls to the right of the leftmost 1 bit 37.25 (10) = 100101.01 (2) = 1.0010101 x 2 5 7.625 (10) = 111.101 (2) = 1.11101 x 2 2 0.3125 (10) = 0.0101 (2) = 1. 01 x 2 -2 exponent fraction mantissa significand
IEEE Floating-Point Standard (Single Precision, 32 bits) q Sign-Magnitude: sign bit S , exponent E and fraction F q The binary exponent is not stored directly. Instead, E is the sum of the true exponent and 127. This biased exponent is always non-negative (seen as magnitude only). q The fraction part assumes a normalized significand in the form 1.F (so we get the extra leading bit for free)
IEEE Floating-Point Standard (Single Precision, 32 bits) q Sign-Magnitude: sign bit S , exponent E and fraction F S × 1.fraction × 2 exponent − 127 , 1 ≤ exponent ≤ 254 N = − 1 q Special values: - E = 0, F = 0 represents 0.0 - Exponent with all bits 1 (value 255) is reserved to represent ±infinity (if F = 0) and NaN (Not a Number, if F != 0)
How would 15213.0 be stored? q First, 15213 (10) = 11101101101101 (2) q Normalize to 1.1101101101101 (2) x 2 13 - The true exponent is 13, so the biased E is E = 13 + 127 (Bias) = 140 (10) = 10001100 (2) - The fraction is F = 11011011011010000000000 (2) Floating Point Representation: 4 6 6 D B 4 0 0 Hex: 0100 0110 0110 1101 1011 0100 0000 0000 Binary:
How would 15213.5 be stored? q First, 15213.5 (10) = 11101101101101.1 (2) q Normalize to 1.11011011011011 (2) x 2 13 - The true exponent is 13, so the biased E is E = 13 + 127 (Bias) = 140 (10) = 10001100 (2) - The fraction is F = 11011011011011000000000 (2) Floating Point Representation: 4 6 6 D B 5 0 0 Hex: 0100 0110 0110 1101 1011 0110 0000 0000 Binary:
How would 23.75 be stored? q First, 23.75 (10) = 10111.11 (2) q Normalize to 1.011111 (2) x 2 4 • The true exponent is 4, so the biased E is E = 4 + 127 (Bias) = 131 (10) = 10000011 (2) q The fraction is F = 01111100000000000000000 (2) Floating-Point Representation: 4 6 6 D B 4 0 0 Hex: 0100 0001 1011 1110 0000 0000 0000 0000 Binary:
How would -23.75 be stored? q Just change the sign bit: 4 6 6 D B 4 0 0 Hex: 1100 0001 1011 1110 0000 0000 0000 0000 Binary: q Do not take the two’s complement!
Exercise 1 q Find the IEEE representation of 40.0
Recommend
More recommend