Sunteți pe pagina 1din 4

REPRESENTATION OF NEGATIVE NUMBERS There are three methods commonly used to represent negative numbers in binary.

They are: signed magnitude, one's complement, and two's complement. We have already encountered one's complement and two's complement briefly in a previous lecture (lecture 3, SUBTRACTION). Signed magnitude uses the most significant bit (leftmost) as the sign, and the remaining bits as the magnitude. SIGNED MAGNITUDE The most significant bit is usually assigned the value 0 for positive sign, and 1 for negative sign. The remaining bits represent the magnitude. Note that there are two zeros. Numbers in the range -(2 ** (n - 1)) - 1 to +(2 ** (n -1)) - 1 can be represented, where n is the number of bits used to store the number. The following table is for n = 4. Decimal Binary Decimal Binary ------- ------ ------- -----0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 -0 1000 -1 1001 -2 1010 -3 1011 -4 1100 -5 1101 -6 1110 -7 1111

To get the negative of a 4 bit number X, xor it with 1000: -X = X xor -0 ONE'S COMPLEMENT The negative of a number X is obtained by inverting each bit of the number: -X = not(X) This means that negative numbers have 1 as their most significant bit, and positive numbers have 0 as their most significant bit. The range of numbers representable is the same as for signed magnitude. Decimal Binary Decimal Binary ------- ------ ------- -----0 0000 -0 1111

1 0001 -1 1110 2 0010 -2 1101 3 0011 -3 1100 4 0100 -4 1011 5 0101 -5 1010 6 0110 -6 1001 7 0111 -7 1000 Addition of two 1's complement numbers is independent of the signs of the two numbers. It is done by adding the binary representations and then adding 1 if there was a carry. 5 - 2 = 5 + (-2) = 0101 + 1101 ----10010 = 0010 + 0001 because of carry = 0011 = 3 4 - 6 = 4 + (-6) = 0100 + 1001 = ---1101 = -2 TWO'S COMPLEMENT The negative of a number X stored in n bits is obtained by subtracting it from 2 ** n: -X = (2 ** n) - X This is not a very useful definition, as it defines negation in terns of a more complex operation, namely subtraction. A simpler definition is the following: -X = not(X) + 1 which is easily checked using the table below. As for the two representations above, negative numbers are distinguished by having their most significant bit set to 1. This repesentation is different from the other two in that it does not have a negative zero. Numbers in the range -(2 ** (n - 1)) to +(2 ** (n - 1)) - 1 can be represented, where n is the number of bits used to store the number. Decimal Binary Decimal Binary ------- ------ ------- ------

0 0000 1 0001 -1 1111 2 0010 -2 1110 3 0011 -3 1101 4 0100 -4 1100 5 0101 -5 1011 6 0110 -6 1010 7 0111 -7 1001 -8 1000 Addition of two 2's complement numbers is independent of the signs of the two numbers. It is done simply by adding the binary representations. Carry is ignored. 5 - 3 = 5 + (-3) = 0101 + 1101 = ----10010 = 0010 (ignoring carry) = 2 2 - 5 = 2 + (-5) = 0010 + 1011 = ---1101 = -3 If multiplication or division is to be performed on 1's complement or 2's complement negative numbers, it is necessary to take their modulus (convert them to positive numbers) before carrying out the operation.

An Aside : Two's Complement Representation


Two's complement representation is a convention used to represent signed binary integers. In binary representation (positional notation), each bit has a weight which a power of two. The weights increase from right to left. For example
011101 = 0 x 2^5 + 1 x 2^4 + 1 x 2^3 + 1 x 2^2 + 0 x 2^1 + 1 x 2^0 = 16 + 8 + 4 + 1 = 29

The same positional notation is used with decimal numbers, except using powers of 10. With two's complement notation, all integers are represented using a fixed number of bits with the leftmost bit given a negative weight. So 100011 would be

100011 = -1 x 2^5 + 0 x 2^4 + 0 x 2^3 + 0 x 2^2 + 1 x 2^1 + 1 x 2^ 0 = -32 + 2 + 1 = -29

There are a number of nice features to two's complement representation. The first is that the normal rules used in the addition of (unsigned) binary integers still work (throw away any bit carried out of the left-most position). Second, it's easy to negate any integers: simply complement each bit and add 1 to the result (011101 complemented is 100010 plus 1 is 100011; 100011 complemented is 011100 plus 1 is 011101). Finally, the left most bit tells you if the integer is positive (0) or negative (1). The range of integers and the ordering does change. For example, four bits can represent the unsigned integers values 0 (0000) through 15 (1111). However, four bit two's complement representation represents the signed values - 8 through +7 ordered as below
1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7

S-ar putea să vă placă și