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
Computer Representation of Numbers Binary representation is a pattern of 1’s and 0’s using powers of 2 just like decimal (base 10) uses powers of 10 Ex: 15910 = 1 x 102 + 5 x 101 + 9 x 100 Ex: 110112 = 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 1 x 20 = 16 + 8 + 0 + 2 + 1 = 27 Word size 32 bit, 64 bit, byte = 8 bit (bit = 0 or 1, on or off, etc.) represents the storage size of the number. Converting decimal to binary (two methods) Using powers of 2: 1. List powers of 2 from right to left: 256, 128, 64, 32, 16, 8, 4, 2, 1 This are the first 9 powers of 2 and so will work for converting small decimal numbers to binary. You would need to list higher powers of two for larger decimal numbers. 2. Determine the largest power of two that is less than or equal to the decimal number and write a 1 under that power and subtract the power from the decimal number. 3. Repeat step 2 for the difference obtained until the difference is 0. 4. Fill in any skipped powers of 2 with 0. Example: 18610 186 – 128 = 58 58 – 32 = 26 26 – 16 = 10 10 – 8 = 2 2–2=0 256 128 1 64 0 32 1 16 1 8 1 4 0 2 1 1 0 101110102 Dividing by 2 with Remainder: 1. Write the decimal number as the dividend inside an upside-down "long division" symbol. Write the base of the destination system (in our case, "2" for binary) as the divisor outside the curve of the division symbol. 2. Write the integer answer (quotient) under the long division symbol, and write the remainder (0 or 1) to the right of the dividend. 3. Continue downwards, dividing each new quotient by two and writing the remainders to the right of each dividend. Stop when the quotient is 0. 4. Starting with the bottom remainder, read the sequence of remainders upwards to the top and write the binary translation left to right. Ex.: 18610 = 101110102 2 | 186 2|93 2|46 2|23 2|11 2|5 2|2 2|1 0 Remainder 0 1 0 1 1 1 0 1 Converting binary to decimal (two methods) Using powers of 2: 1. List powers of 2 from right to left: 256, 128, 64, 32, 16, 8, 4, 2, 1 This are the first 9 powers of 2 and so will work for converting any 9 digit binary number to decimal. Additional powers need to be listed for longer (larger) numbers 2. Add up the powers wherever a ‘1’ appears in the number for that power. Ex. 11011102 = 64 + 32 + 8 + 4 + 2 = 11010 Doubling (does not require using the powers of 2): Starting with the left-most digit of the given binary number, for each digit as you move to the right, double your previous total and add the current digit. Ex. 11011102 = 1 + 0*2 = 1 (since for the leftmost digit the previous total is 0) 1 + 1*2 = 3 0 + 3*2 = 6 1 + 6*2 = 13 1 + 13*2 = 27 1 + 27*2 = 55 0 + 55*2 = 110 Binary Addition Algorithm 0 +0 --00 0 +1 --01 1 +0 --01 1 +1 --10 0 0 0 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 1 --------------0 0 0 1 0 0 1 1 carries number to be added (1410) number to be added (510) sum (1910) Storage of Signed Integers Since everything in the computer is represented with stored values of 0 and 1 we must devise a way to indicate the sign (+ or -) of signed integers. We will look at two different ways to do this. Sign-Magnitude The first representation that comes to mind is just to use a bit (usually the leftmost bit) that represents the sign of the integer. 0 would represent a positive integer and 1 would indicate a negative integer. The remaining bits of storage would hold the magnitude. This is called the sign-magnitude concept. We will use an 8-bit storage space to simplify the representation of the concept. Ex. +23 would be stored as 0 00101112 and -23 would be stored as 1 00101112 For an 8-bit storage allocation for integers we could store the numbers -127 to +127 this way. We get a symmetrical representation of a range of positive and negative integers with this method. Why not? 0 0000000 and 1 0000000 would represent a positive and negative zero – two values for zero Binary addition and subtraction algorithms are more complicated (cannot use the simple binary addition algorithm) Ex. Look at the addition of 14 and -5 using the sign-magnitude representation and the simple binary addition algorithm. 0001100 00001110 10000101 10010011 carries (1410) (-510) (-1910) WRONG! 2’s Complement Concept – when you add two integers of the same magnitude but of opposite sign the result is 0. So – what pattern of 1’s and 0’s can accomplish this desired result? Using the binary addition algorithm, what do we need to add to 000000012 to get the pattern 000000002? See below. 00000001 + 11111111 (1)00000000 This works if we ignore the last carry-over of 1 falling outside of the 8-bit defined storage space This means that 11111111 can be used to represent the number -1. It is called the 2’s complement representation. To determine the 2’s complement representation of an integer use the following rules: 1. If the integer is positive then use the normal binary representation of the integer 2. If the integer is negative, flip all the bits of the binary representation as if it were positive and then add 1 discarding any carryover outside the storage size of the integer that may occur. Ex.: -5 in an 8-bit storage allocation would be determined as follows 510 000001012 Flipped bits Add 1 111110102 00000001 2’s complement 11111011 Let’s look at the same addition (14 + -5) from above and see if we get it right (1)1111110 00001110 11111011 00001001 carries (1410) (-510) (910) Hurray! This is the way that negative integers are almost universally represented in today’s computers. It is important to note that computer must determine whether or not ‘overflow’ has occurred when doing addition with 2’s complement representation. This is done by comparing two bits – the carry bit carried into the high order column (left-most) and the carry bit carried out of that high order column. If they are not both 0 or both 1 then overflow has occurred (i.e. The storage size set aside to contain the result of the addition is not sufficient to store the result. The number is either too large for the allocated storage or too small for the allocated storage.) Storage of Real (Floating Point) Numbers Floating point or real numbers have a decimal point. They are stored using a scientific notation format with the sign, mantissa, and exponent stored in a specific number of bits within the designated storage space. This is an example using a 32-bit word to represent a floating-point number: 1 bit ..... 8 bits .............. 23 bits Representing +/- M x 2E: The implied base is 2 (not explicitly shown in the representation). The exponent can be represented in signed 2's complement. The implied decimal point is between the exponent field E and the significand field M. More bits in field E mean larger range of values representable. More bits in field M mean higher precision. Zero is represented by all bits equal to 0 To efficiently use the bits available for the significand, it is shifted to the left until all leading 0's disappear (as they make no contribution to the precision). The value can be kept unchanged by adjusting the exponent accordingly. Example: A binary number can be represented in 14-bit floating-point form in the following ways (1 sign bit, a 4-bit exponent field and a 9-bit significand (mantissa) field): with an implied 1.0: The IEEE (Institute of Electrical and Electronics Engineers) 754 floating-point standard uses 32 bits to represent a floating-point number, including 1 sign bit, 8 exponent bits and 23 bits for the significand. As the implied base is 2, an implied 1 is used, i.e., the significand has effectively 24 bits including 1 implied bit to the left of the decimal point not explicitly represented in the notation.