Content deleted Content added
Guy Harris (talk | contribs) Correctly describe what's being done to negate a value when using two's complement (the second step is addition, which is not not a bitwise operator). |
m →Example: HTTP to HTTPS for Cornell University |
||
(7 intermediate revisions by 5 users not shown) | |||
Line 1:
{{Short description|Mathematical operation on binary numbers, and a number representation based on this operation}}
'''Two's complement''' is the most common [[signed number representations|method of representing signed]] (positive, negative, and zero) [[Integer (computer science)|integers]] on computers,<ref>E.g. "Signed integers are two's complement binary values that can be used to represent both positive and negative integer values", Section 4.2.1 in ''Intel 64 and IA-32 Architectures Software Developer's Manual'', Volume 1: Basic Architecture, November 2006</ref> and more generally, [[Fixed-point arithmetic|fixed point binary]] values.
Unlike the [[ones' complement]] scheme, the two's complement scheme has only one representation for zero, with room for one extra negative number (the range of a 4-bit number is -8 to +7). Furthermore, the same arithmetic implementations can be used on signed as well as unsigned integers<ref>
{{cite book
|first1=Alexandre |last1=Bergel
Line 15:
}}
</ref>
and differ only in the integer overflow situations, since the sum of representations of a positive number and its negative is 0 (with the carry bit set).
== Procedure ==
Line 193:
In summary, the two's complement of any number, either positive, negative, or zero, can be computed in the same ways. In two's complement signed integer representation, the two's complement of any integer is equal to -1 times that integer, except for the most negative integer representable in the given number of bits <math>N</math>, i.e. the integer <math>-2^{N-1}</math>, the two's complement of which is itself (still negative).
===Subtraction from 2<sup>''N''</sup>===
Line 358 ⟶ 357:
{{block indent|{{=}} 160. + 1.}}
{{block indent|{{=}} 161.}}
<div style="overflow-x: auto;">
<pre style="width:25em">
1111 1111 255.
Line 367 ⟶ 368:
1010 0001 (two's complement) 161.
</pre>
</div>
{|class="wikitable floatright" style="width:14em;text-align:center"
|+ Two's complement 4 bit integer values
Line 411 ⟶ 414:
|series=Class notes for CS 104
|publisher=Cornell University |department=Computer Science |place=Ithaca, New York
|url=
}}</ref>
Line 422 ⟶ 425:
===Addition===
Adding two's complement numbers requires no special processing even if the operands have opposite signs; the sign of the result is determined automatically. For example, adding 15 and −5:
<pre style="width:25em">▼
<div style="overflow-x: auto;">
▲<pre style="width:25em;">
0000 1111 (15)
+ 1111 1011 (−5)
Line 428 ⟶ 433:
0000 1010 (10)
</pre>
</div>
Or the computation of 5 − 15 = 5 + (−15):
<div style="overflow-x: auto;">
<pre style="width:25em">
0000 0101 ( 5)
Line 437 ⟶ 444:
1111 0110 (−10)
</pre>
</div>
This process depends upon restricting to 8 bits of precision; a carry to the (nonexistent) 9th most significant bit is ignored, resulting in the arithmetically correct result of 10<sub>10</sub>.
Line 443 ⟶ 451:
In other terms, if the left two carry bits (the ones on the far left of the top row in these examples) are both 1s or both 0s, the result is valid; if the left two carry bits are "1 0" or "0 1", a sign overflow has occurred. Conveniently, an [[XOR]] operation on these two bits can quickly determine if an overflow condition exists. As an example, consider the signed 4-bit addition of 7 and 3:
<div style="overflow-x: auto;">
<pre style="width:25em">
0111 (carry)
Line 450 ⟶ 460:
1010 (−6) invalid!
</pre>
</div>
In this case, the far left two (MSB) carry bits are "01", which means there was a two's-complement addition overflow. That is, 1010<sub>2</sub> = 10<sub>10</sub> is outside the permitted range of −8 to 7. The result would be correct if treated as unsigned integer.
Line 456 ⟶ 468:
===Subtraction===
Computers usually use the [[method of complements]] to implement subtraction. Using complements for subtraction is closely related to using complements for representing negative numbers, since the combination allows all signs of operands and results; direct subtraction works with two's-complement numbers as well. Like addition, the advantage of using two's complement is the elimination of examining the signs of the operands to determine whether addition or subtraction is needed. For example, subtracting −5 from 15 is really adding 5 to 15, but this is hidden by the two's-complement representation:
<div style="overflow-x: auto;">
<pre style="width:25em">
11110 000 (borrow)
Line 463 ⟶ 477:
0001 0100 (20)
</pre>
</div>
Overflow is detected the same way as for addition, by examining the two leftmost (most significant) bits of the borrows; overflow has occurred if they are different.
Another example is a subtraction operation where the result is negative: 15 − 35 = −20:
<div style="overflow-x: auto;">
<pre style="width:25em">
11100 000 (borrow)
Line 473 ⟶ 491:
1110 1100 (−20)
</pre>
</div>
As for addition, overflow in subtraction may be avoided (or detected after the operation) by first sign-extending both inputs by an extra bit.
Line 489 ⟶ 509:
|archive-date = February 13, 2015
}}</ref> For example, take {{math|1=6 × (−5) = −30}}. First, the precision is extended from four bits to eight. Then the numbers are multiplied, discarding the bits beyond the eighth bit (as shown by "{{Mono|x}}"):
<div style="overflow-x: auto;">
<pre style="width:25em">
00000110 (6)
Line 504 ⟶ 526:
xx11100010
</pre>
</div>
This is very inefficient; by doubling the precision ahead of time, all additions must be double-precision and at least twice as many partial products are needed than for the more efficient algorithms actually implemented in computers. Some multiplication algorithms are designed for two's complement, notably [[Booth's multiplication algorithm]]. Methods for multiplying sign-magnitude numbers do not work with two's-complement numbers without adaptation. There is not usually a problem when the multiplicand (the one being repeatedly added to form the product) is negative; the issue is setting the initial bits of the product correctly when the multiplier is negative. Two methods for adapting algorithms to handle two's-complement numbers are common:
|