Two's complement: Difference between revisions

Content deleted Content added
From the ones' complement: Get rid of extra blank lines.
Bender the Bot (talk | contribs)
m Example: HTTP to HTTPS for Cornell University
 
(6 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. Two'sAs complement useswith the [[Most significant bit|binary digit with the ''greatest'ones' valuecomplement]] asand the ''[[sign''-magnitude]] tosystems, indicatetwo's whethercomplement the binary number is positive or negative; whenuses the [[most significant bit]] isas the ''1sign'' theto numberindicate ispositive signed(0) asor negative and(1) whennumbers, theand mostnonnegative significantnumbers bitare isgiven ''0''their theunsigned numberrepresentation (6 is signed0110, aszero positive.is As0000); ahowever, resultin two's complement, non-negative numbers are represented asby themselves:taking 6the is[[bit 0110,complement]] zeroof istheir 0000,magnitude and then adding one (−6 is 1010). (theThe resultnumber of applyingbits in the bitwiserepresentation NOTmay operatorbe toincreased 6by andpadding addingall 1).additional However,high whilebits theof numberpositive ofor binarynegative bitsnumbers iswith fixed1's throughoutor a0's, computationrespectively, itor isdecreased otherwiseby arbitraryremoving additional leading 1's or 0's.
 
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 357:
{{block indent|{{=}} 160. + 1.}}
{{block indent|{{=}} 161.}}
 
<div style="overflow-x: auto;">
<pre style="width:25em">
1111 1111 255.
Line 366 ⟶ 368:
1010 0001 (two's complement) 161.
</pre>
</div>
 
{|class="wikitable floatright" style="width:14em;text-align:center"
|+ Two's complement 4&nbsp;bit integer values
Line 410 ⟶ 414:
|series=Class notes for CS&nbsp;104
|publisher=Cornell University |department=Computer Science |place=Ithaca, New York
|url=httphttps://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html |access-date=2014-06-22
}}</ref>
 
Line 421 ⟶ 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 427 ⟶ 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 436 ⟶ 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 442 ⟶ 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 449 ⟶ 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 455 ⟶ 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 462 ⟶ 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&nbsp;−&nbsp;35 = −20:
 
<div style="overflow-x: auto;">
<pre style="width:25em">
11100 000 (borrow)
Line 472 ⟶ 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 488 ⟶ 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 503 ⟶ 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: