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
3.5 Two's Complement Representation of Negative Numbers In most computers information is stored in terms of bits where a bit is something that is either 0 or 1 at any given time. There are various ways information is coded in terms of bits. A common way to store integers is to write the integer in binary (base 2) where each digit is either 0 or 1. Then the base 2 digits can be directly represented by bits. In base 2, each bit position represents a power of two with the 20 = 1’s place on the right, the 21 = 2’s place second from the right and so on. One way to convert a number n from decimal to binary, a common method is to divide the number by 2. The remainder, n mod 2¸ is the n low order binary digit. Then one repeats the process with the integer quotient n1 = . 2 n1 mod 2 is the second to the right bit and so on. Example 1. Find the binary representation of 21310 (base 10). Solution. If one divides 213 by 2 then the quotient is 106 with a remainder of 1. So the low order bit is 1. Repeating the process with 106 one gets 53 as a quotient with a remainder of 0. So the next to last bit is 0. 53 divided by 2 is 26 with remainder 1. So the third bit from the right is 1. 26 divided by 2 is 13 with remainder 0. So the next bit is 0. 13 divided by 2 is 6 with a remainder of 1. So 1 is the next bit. 6 divided by 2 is 3 with remainder 0. 0 is the next bit. 3 divided by 2 is 1 with remainder 1. 1 is the next bit. 1 divided by 2 is 0 with remainder 1. So the high order bit is 1. 213 = 128 + 64 + 16 + 4 + 1 = 127 + 126 + 025 + 124 + 023 + 122 + 021 + 120 = 1101 01012 It would take 8 bits to represent 21310 in this fashion. Question. What range of integers can one represent in this fashion with n bits? The answer depends on whether one is representing just positive integers and zero or whether one wants to represent negative integers as well. In either case the answer is related to the number of possible combinations of values of n bits. If we have just one bit, there are two possible values, namely 0 and 1. These represent the numbers 0 and 1. If we have two bits there are four possible values, namely 00, 01, 10 and 11. These represent the numbers 0, 1, 2 and 3 in binary. If we have three bits there are four possible values, namely 000, 001, 010 011 100, 101, 110 and 111. These represent the numbers 0, 1, 2, 3, 4, 5, 6 and 7 in binary. 3.5 - 1 If we have n bits, there are 2n possible values. A methodical way to list them is to take the 2n-1 values of n-1 bits and put a 0 in front of them. Then take the 2n-1 values of n-1 bits and put a 1 in front of them. These represent the numbers 0, 1, 2, …, 2n – 1 in order. In more detail one has 00 … 000 00 … 001 00 … 010 00 … 011 00 … 100 … 01 … 111 10 … 000 … 11 … 110 11 … 111 = = = = = 0 1 2 3 4 = 2n-1 - 1 = 2n = 2n - 2 = 2n - 1 For example, with n = 8 bits one has 0000 0000 0000 0001 0000 0010 0000 0011 0000 0100 … 0111 1111 1000 0000 … 1111 1010 1111 1111 = = = = = 0 1 2 3 4 = 27 – 1 = 127 = 128 = 28 – 2 = 254 = 28 – 1 = 255 In most computer programs integer variables are stored in a certain number of bytes. The most common choices are 1, 2, 4 and 8 bytes. Similarly, most CPU's are equipped to do arithmetic with 1, 2, 4 and 8 byte integers. With one byte one can represent the nonnegative numbers 0 – 255. With two bytes one can represent 0 – 216 – 1 = 64K – 1 = 65,535. With four bytes one can represent 0 – 232 – 1 = 4G – 1 = 4,294,967,295. Example 1. Suppose x, y and z are 1 byte integer variables. Suppose the contents of x and y are the following 8 bits. x = 1101 0101 y = 0111 0001 = 21310 = 11310 3.5 - 2 Now suppose we want to perform the calculation z = x + y. First we add x and y. = 1 1110 x = 1101 + y = 0111 x+y = 1 0100 111 0101 0001 1000 carries Note that the result has 9 bits. However, our intentions are to store the result in the 1 byte variable z. Unless we have made some prior provision for storing the 9th bit somewhere, this bit will be lost when we store the low order 8 bits in z. Since the 9th bit is the 28's bit or 256's bit, the result stored in z will be (x + y) – 256. Another way of saying this is what is stored is (x + y) mod 256 or (x + y) mod 28. 3.5 - 3