Vous êtes sur la page 1sur 2

Twos Complement Notation

A Primer

This article assumes that the reader is familiar with the basics of unsigned binary notation, including conversion to and from base-10 notation and simple unsigned binary addition. All text and examples in this article are based on source material from my class notes. Within a binary computer architecture, a binary representation of any form is useless without a context for the data. As an example, the binary string 110011112 might be an unsigned short (an 8bit integer) with the decimal value 20710 , the high byte of a 16-bit word, or the high byte of the high word of a 32-bit DWORD (double-word), or even a segment of a memory structure that might not have a binary equivalent. Conversion between positive integer values and unsigned binary notation is outside the scope of this article, however, this article builds upon the principles of unsigned binary conversion. There are several methods for representing negative base-10 integers, and the most intuitive-seeming is the sign-magnitude format. An 8-bit number represents unsigned values from 010 to 25510 . Signmagnitude uses the MSB (most signicant bit) of the binary representation to indicate the sign of the number, and the seven least-signicant bits represent the magnitude of the number 1. (1)10011112 (with the sign bit in parentheses) would therefore be: (1) negative, since the MSB is set. (2) (-79), since the magnitude bits encode the integer value 7910 . One problem with this approach is the complication presented by two representations of zero. (1)00000002 and 000000002 are both valid interpretations of zero, but the presence of two notations for zero complicates arithmetic and comparison operations - the designer would have to implement checking for both values of zero. An additional problem is that this approach complicates arithmetic. To compute the addition of two signed-magnitude numbers requires the use of an operation known as end-around carry, which is more complex than is necessary. Some computer architectures however did use the sign-magnitude notation, but most modern computers rely on the twos complement format for signed representation. At rst glance, twos complement notation seems identical to sign-magnitude notation. The easy method of deducing if a twos complement representation is negative is to check if the MSB is set, in which case the base10 representation will be negative. Twos complement, however, avoids the problem of multiple representations of zero; 0x00hex (000000002 ) is the sole notation for zero. Conversion to and from twos complement is a straightforward process. To convert a positive base10 integer is the same process as an unsigned conversion a positive twos complement number is equivalent to a positive unsigned binary number. A four-bit twos complement number 00102 is the same as the unsigned binary notation for 210 , 00102 A negative integer base10 representation is slightly more complicated2. The process is summarized as follows. (1) Calculate an unsigned binary representation of the number. (2) Invert all the bits. (3) Add 12 to the inversion. For the twos complement representation of -115: (1) Obtain the unsigned binary representation of 115 (notice that the rst step requires the magnitude of the number.) In this case, the binary representation will be 011100112 (2) Invert all the bits. Simply replace each set bit (1) with an unset bit (0). The result will be 100011002 . (3) Add 12 to the result, which returns 100011012 . To verify the conversion process (and to convert the twos complement number to base10 ): Multiply the base10 value of the MSB (nth bit) by (-1)10 , and add that value to the positive base10 value of the LSBs (bits 0-(n-1)). Treat the MSB as a representation of a negative integer. e.g: 100011012 27 (1) + 26 0 + 25 0 + 24 0 + 23 1 + 22 1 + 1 0 + 20 1 = (115) 2 The conversion process might seem long on paper, but the two steps of converting an unsigned binary value to a twos complement equivalent are

1http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/Data/signedmag.html 2http://www.cs.cornell.edu/ tomf/notes/cps104/twoscomp.html


easy to do in any logic architecture. Inversion is a one-step process, and most adder circuits have a carry-in input, which makes the conversion process easy. This makes implementing a simple ALU (arithmetic and logic unit) easy, since with one adder, both addition and subtraction can be performed with the use of only one adder circuit (hint: subtraction is an addition of a positive and a negative number.) Twos complement addition: Adding two twos complement numbers is straightforward; just add them as you would add any two binary numbers. Begin with the two (unsigned binary or twos complement - it is possible to add any combination of the two types) operands. The two operands in this example will be four bits each 11002 and 01002 , both twos complement. We set up the addition as follows: 11002 00112 11112

than the computer can represent in memory. For example, adding the two signed twos complement four-bit numbers 10002 and 10002 (810 ) results in 00002 , which is clearly wrong. Detecting overow in signed twos complement is easy: if the MSBs of the operands are the same and the MSB of the result is different, then an overow has occurred. If the MSBs of the operands are different, then the operands are of different signs, and by mathematical logic cannot exceed the bounds of the twos complement representation. A Verilog statement for a simple eight-bit signed overow detector is shown below, where a, b, and c refer to the operands A and B and the result C.
assign Ovf = (a[7] & b[7] & ~c[7]) || (~a[7] & b[7] & c[7]); ~

(4)10 (+3)10 (1)10

An overow condition may occur, however, which means that the binary representation is now larger

Multiplication and division still works identical to base10 multiplication and division, however, for two operands of length n to be multiplied together, the memory allocated for the answer needs to be at least 2n bits wide to prevent overow. Details of such a process, however, is outside the scope of this article. This concludes the primer on twos complement notation.