440 likes | 643 Views
Lecture 2 Data Representation 2. Integers. Readings. Stalling, Computer Architecture & Organization, pp 291-301. Also summarised in Appendix A. A Bit of History. Numeral – a single symbol that represents a quantity or number.
E N D
Lecture 2Data Representation 2 Integers
Readings • Stalling, Computer Architecture & Organization, pp 291-301. Also summarised in Appendix A.
A Bit of History • Numeral – a single symbol that represents a quantity or number. • Number system – a way of assigning meaning to combinations of numerals. • Base 10 (denary) number system used by most cultures (10 fingers). • Mayas, Celts, Aztecs – base 20. • Sumerians and Babylonians – base 60.
Numbers 123.45 = 100 + 20 + 3 + 0.4 + 0.05 = 1*102 + 2*101 + 3*100 + 4*10-1 + 5*10-2 This is a base 10 number (denary) system. It has 10 symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
Numbers (continued) • There are other number systems (bases). • The 'value' of a number is determined by positional notation. • In general, def.xyzb = • d*b2 + e*b1 + f*b0 + x*b-1+ y*b-2+ z*b-3 • where b is the base.
Binary Number System We said the computer is a binary system, i.e. base 2. Thus there are two symbols: 0, 1 110011 = 1*25 + 1*24 + 0*23 + 0*22 + 1*21 + 1*20 = 32 + 16 + 0 + 0 + 2 + 1 = 51 110.011 = 1*22 + 1*21 + 0*20 + 0*2-1 + 1*2-2 + 1*2-3 = 4 + 2 + 0 + 0 + 1/4 + 1/8 = 6.375
Decimal to Binary Conversion • Convert 38.6875 to binary. • For the whole number part, repeatedly divide by 2. Use the remainders from bottom to top.
Decimal to Binary Conversion (continued) Dividend Quotient Remainder 38 19 0 LSB 19 9 1 9 4 1 4 2 0 2 1 0 1 0 1 MSB
Decimal to Binary Conversion (continued) • For the fractional part, multiply by 2. • Split the result into an integer part and a fractional part. • Continue multiplying the fractional part until it is zero or you get tired (run out of space). • Use the integer part from top to bottom.
Decimal to Binary Conversion (continued) Multiplicand Integer Fractional Part Part 0.6875 1 .375 LSB 0.375 0 .750 0.75 1 .500 0.50 1 .000
Decimal to Binary Conversion (continued) From earlier slide: 3810 = 1001102 From previous slide: 0.687510 = 10112 Combining: 38.687510 = 100110.10112
Repeating & Irrational Numbers • If the denominator has factors other than 2 or 5 it will produce a recurring decimal fraction, e.g. 1/3 = 0.3333 … • Irrational numbers, such as √2, and transcendental numbers, such as p, also never terminate. • Thus most decimal fractions will produce recurring binary fractions. • Truncate (or round) these to fit in available storage.
Binary to Decimal Conversion POWER 2P 2-P 0 1 1 2 .5 2 4 .25 3 8 .125 4 16 .0625 5 32 .03125 6 64 7 128 8 256 9 512 10 1,024
Binary to Decimal Conversion (continued) POWER 2P 11 2,048 12 4,096 13 8,192 14 16,384 15 32,768 16 65,536 20 1,048,576 30 1,073,741,824 31 2,147,483,648 32 4,294,967,296
Binary to Decimal Conversion (continued) Work outwards from the binary point: 100110.1011 = 2 + 4 + 32 + .5 + .125 + .0625 = 38.6875
Octal Number System • A base 8 number system. • Uses: 0, 1, 2, 3, 4, 5, 6, 7. • Takes 3 bits. • To convert from binary, group the bits in threes outwards from the binary point.
Octal Number System 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7 101 110 001 011 56138
Hexadecimal Number System • A base 16 number system. • Uses 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. • Takes 4 bits. • Often called hex for short. • Group bits in fours from the binary point.
Hexadecimal Number System (continued) 0000 0 1000 8 0001 1 1001 9 0010 2 1010 A 0011 3 1011 B 0100 4 1100 C 0101 5 1101 D 0110 6 1110 E 0111 7 1111 F 100 1101 1001 1111 4D9F16
decimal binary octal hex Other Conversions Conversions between decimal and octal or hexadecimal are usually done via a binary representation, i.e. :
Introduction to Negative Integers • So far we have concentrated on positive integers. We have to devise some scheme to represent negative numbers. • 4 possible ways: • Excess notation. • Sign and magnitude. • One's complement. • Two's complement.
Excess Notation • Shift the numbers by about half of the range of representation. • For example, assume we have 3 bits to represent integers. That allows for 8 (23) possible values. Let's shift by 4 which is called excess-4 notation.
Excess Notation (continued) Actual Stored Number Number -4 000 -3 001 -2 010 -1 011 0 100 1 101 2 110 3 111
stored number actual number excess notation = + actual number stored number excess notation = - Excess Notation Algorithms Use the following equations to convert between the actual number and the stored number.
Excess NotationExample 1 A computer stores decimal integer values in 8 bits, using excess-128 notation. How would the decimal value -60 be stored? stored number = -60 + 128 = 68 = 64 + 4 = 10001002 or 0100 0100 in a byte
Excess NotationExample 2 The binary number, 0101 1101, is in excess-128 binary notation. What is the number in decimal? 0101 1101 = 64 + 16 + 8 + 4 + 1 = 93 in excess-128 actual number = 93 - 128 = -35
Excess NotationSummary Negation is complicated. Addition and subtraction is easy. One value for zero. Range of integers is (2n-1 - 1) to - (2n-1).
Signed Magnitude Assign the most significant bit as a sign bit instead of a power of two. 0 - positive 1 - negative Using 4 bits: 2 0010 -2 1010 For a byte: 84 0101 0100 -84 1101 0100
Signed Magnitude Summary Negation is easy. Addition and subtraction is complicated. Two values for zero. Range of integers is (2n-1 - 1) to - (2n-1 - 1).
One's Complement If positive, do nothing. If negative, complement (reverse) each bit. 84 0101 0100 -84 1010 1011
One's ComplementSummary Negation is easy. Addition and subtraction is easier. Two values for zero. Range of integers is (2n-1 - 1) to - (2n-1 - 1).
Two's Complement If positive, do nothing. If negative, complement (reverse) each bit, and then add 1 to the result.
Two's Complement Example Convert –84 to two's complement: 84 = 0101 0100 0101 0100 original 1010 1011 each bit complemented + 1 add one 1010 1100 -84 in two's complement
Two's Complement to Decimal If the sign bit is 0, convert the binary number to decimal. If the sign bit is 1, subtract 1 from the binary number, complement (reverse) each bit, convert the binary number to decimal, and put a minus sign in front.
Two's Complement to Decimal Example Convert the 8-bit number 1110 1010 in two's complement to decimal. 1110 1010 - 1 1110 1001 Complement each bit: 0001 0110 0001 0110 = 16 + 4 + 2 = 22 Answer = -22
Two's Complement Summary Negation is relatively easy. Addition and subtraction is easy. One value for zero. Range of integers is (2n-1 - 1) to - (2n-1).
4-Bit Summary Decimal SM 1's 2's +7 0111 0111 0111 +6 0110 0110 0110 +5 0101 0101 0101 +4 0100 0100 0100 +3 0011 0011 0011 +2 0010 0010 0010 +1 0001 0001 0001 0 0000 0000 0000 -0 1000 1111
4-Bit Summary Decimal SM 1's 2's -1 1001 1110 1111 -2 1010 1101 1110 -3 1011 1100 1101 -4 1100 1011 1100 -5 1101 1010 1011 -6 1110 1001 1010 -7 1111 1000 1001 -8 1000
Arithmetic with Negative Numbers (1) • Addition and subtraction in signed magnitude representation requires consideration of the signs of both numbers and consideration of their absolute value. Leads to complex rules (complex logic). • Complement forms take advantage of the continuum of representation.
Arithmetic with Negative Numbers (2) • How to choose between 1's and 2's complement? • 1's complement easier to form. • 2's complement has only one 0 which makes logic for zero comparison easier. • Most machines use 2's complement form.
Two's Complement Example 1 Subtract 5 from 18 in 8-bit, 2's complement. 18 = 0001 0010 -5 = 1111 1011 10000 1101 Ignoring the high-order (9th) bit gives: 0000 1101 = 13
Two's Complement Example 2 Subtract 5 from -18 in 8-bit, 2's complement. -18 = 1110 1110 -5 = 1111 1011 11110 1001 Ignoring the high-order (9th) bit gives: 1110 1001 = -23
Two's Complement Overflow • Over/Underflow occurs if any of the following situations occur (assuming that both A and B are positive): • A + B negative. • A – (-B) negative. • -A – B positive. • -A + -B positive.
Next Lecture Real Numbers (floats and doubles)