Saturday, December 24, 2011

Two's Compliment



Two's complement
The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two (specifically, from 2N for an N-bit two's complement). The two's complement of the number then behaves like the negative of the original number in most arithmetic, and it can coexist with positive numbers in a natural way.
A two's-complement system, or two's-complement arithmetic, is a system in which negative numbers are represented by the two's complement of the absolute value;[1] this system is the most common method of representing signed integers on computers.[2] In such a system, a number is negated (converted from positive to negative or vice versa) by computing its two's complement and adding one. An N-bit two's-complement numeral system can represent every integer in the range −2N−1 to 2N−1-1 while one's complement can only represent integers in the range −(2N−1−1) to 2N−1−1
The two's-complement system has the advantage of not requiring that the addition and subtraction circuitry examine the signs of the operands to determine whether to add or subtract. This property makes the system both simpler to implement and capable of easily handling higher precision arithmetic. Also, zero has only a single representation, obviating the subtleties associated with negative zero, which exists in ones'-complement systems.
Two's complement is referred to as Binary Number Representation (BNR) in protocols used in Aviation (e.g. ARINC 429).
The method of complements can also be applied in base-10 arithmetic, using ten's complements by analogy with two's complements.

Two's-complement numbers
Two's complement numbers is a way to encode negative numbers into ordinary binary, such that addition still works. Adding −1 + 1 should equal 0, but ordinary addition gives the result of 2 or −2 unless the operation takes special notice of the sign bit and performs a subtraction instead. Two's complement results in the correct sum without this extra step.
A two's-complement number system encodes positive and negative numbers (multiples of 2 − n) in a binary number representation. The bits have a binary radix point and the bits are weighted according to the position of the bit within the array. A convenient notation is the big-endian ordering. In this notation, the bit to the left of the binary point has a bit index of 0 and a weight of 20. The bit indices increase, by one, to the left of the binary point, and decrease, by one, to the right of the binary point. The weight of each bit is a power of two, except for the left-most bit, whose weight is a negative power of two. With this bit numbering, the two's complement number (for a number w) with m integer bits and n fractional bits is represented by an array of bits (where the possible values 0 and 1 for each ai depends on w):
.
The value of this number is given by the following formula.

The left-most bit, also called the most-significant bit (MSB), determines the sign of the number, but, unlike the sign-and-magnitude representation, also has a weight, −2m-1, as shown in the formula above. Because of this weight, it is misleading to call this bit the "sign bit".
The two's complement encoding shown above can represent the following range of numbers
Zero representation is


Making the Two's Complement of a number
In two's complement, positive numbers are represented as binary numbers whose most significant bit am − 1 = 0. Negative numbers are represented with the most-significant bit am − 1 = 1, making use of the left-most bit's negative weight. All radix complement number systems use a fixed-width encoding. Every number x encoded in such a system has a fixed width so the most-significant digit can be examined.
How to create algorithmically for x the two's complement binary value ? (The algorithm is mainly due to )
1. Express the binary value for | x | (important: express ALL coefficients bi)
2. If x < 0 (the case x > 0 is already finished by 1. since x = | x | and bi = ai for all i, especially bm − 1 = am − 1 = 0)
2a. Complement each bi (i.e. replace bi by 1 − bi for )
2b. Add the coefficient 1 (this 1 represents 2 − n) to the latest binary representation. The addition leads to the final representation for x which is
.
In general, for a radix r's complement encoding, with r the base (radix) of the number system, an integer part of m digits and fractional part of n digits, then the r's complement of a number 0≤ N<rm−1−r−n is determined by the formula:
  N** = (rm − N) mod (rm)
The (r−1)'s complement of a number is determined by the formula:
  N* = rm − r−n −N
We can also find the r's complement of a number N by adding r–n to the (r-1)'s complement of the number i.e.,
  N** = N* + r–n


Potential ambiguities in usage
One should be cautious when using the term two's complement, as it can mean either a number format or a mathematical operator. For example 0111 represents 7 in two's-complement notation, but 1001 is the two's complement of 7, which is the two's complement representation of −7. In code notation or conversation the statement "convert x to two's complement" may be ambiguous, as it could describe either the change in representation of x to two's-complement notation from some other format, or else (if the writer really meant "convert x to its two's complement") the calculation of the negated value of x.

No comments:

Post a Comment