Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Two's complement
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Theory== Two's complement is an example of a [[Method of complements|radix complement]]. The 'two' in the name refers to the number {{math|2<sup>''N''</sup>}} - "two to the power of N", which is the value in respect to which the complement is calculated in an {{mvar|N}}-bit system (the only case where exactly 'two' would be produced in this term is {{math|''N'' {{=}} 1}}, so for a 1-bit system, but these do not have capacity for both a sign and a zero). As such, the precise definition of the ''two's complement'' of an {{mvar|N}}-bit number is the [[method of complements|complement]] of that number with respect to {{math|2<sup>''N''</sup>}}. The defining property of being a ''complement to a number with respect to {{math|2<sup>N</sup>}}'' is simply that the summation of this number with the original produce {{math|2<sup>''N''</sup>}}. For example, using binary with numbers up to three bits (so {{math|''N'' {{=}} 3}} and {{math|2<sup>''N''</sup> {{=}} 2<sup>3</sup> {{=}} 8 {{=}} 1000<sub>2</sub>}}, where '<sub>2</sub>' indicates a binary representation), a two's complement for the number 3 ({{math|011<sub>2</sub>}}) is 5 ({{math|101<sub>2</sub>}}), because summed to the original it gives {{math|2<sup>3</sup> {{=}} 1000<sub>2</sub> {{=}} 011<sub>2</sub> + 101<sub>2</sub>}}. Where this correspondence is employed for representing negative numbers, it effectively means, using an analogy with decimal digits and a number-space only allowing eight non-negative numbers 0 through 7, dividing the number-space into two sets: the first four of the numbers 0 1 2 3 remain the same, while the remaining four encode negative numbers, maintaining their growing order, so making 4 encode β4, 5 encode β3, 6 encode β2 and 7 encode β1. A binary representation has an additional utility however, because the most significant bit also indicates the group (and the sign): it is 0 for the first group of non-negatives, and 1 for the second group of negatives. The tables at right illustrate this property. {|class="wikitable sortable floatright" style="text-align: center;" |+ Three-bit integers ! Bits !Unsigned value !Signed value<br />(Two's complement) |- | 000 |0 | 0 |- | 001 |1 | 1 |- | 010 |2 | 2 |- | 011 |3 | 3 |- | 100 |4 | β4 |- | 101 |5 | β3 |- | 110 |6 | β2 |- | 111 |7 | β1 |} {|class="wikitable sortable floatright" style="text-align: center;" |+ Eight-bit integers ! Bits !Unsigned value !Signed value <br />(Two's complement) |- | 0000 0000 |0 | 0 |- | 0000 0001 |1 | 1 |- | 0000 0010 |2 | 2 |- | 0111 1110 |126 | 126 |- | 0111 1111 |127 | 127 |- | 1000 0000 |128 | β128 |- | 1000 0001 |129 | β127 |- | 1000 0010 |130 | β126 |- | 1111 1110 |254 | β2 |- | 1111 1111 |255 | β1 |} Calculation of the binary two's complement of a positive number essentially means subtracting the number from the {{math|2<sup>''N''</sup>}}. But as can be seen for the three-bit example and the four-bit {{math|1000<sub>2</sub>}} ({{math|2<sup>3</sup>}}), the number {{math|2<sup>''N''</sup>}} will not itself be representable in a system limited to {{mvar|''N''}} bits, as it is just outside the {{mvar|''N''}} bits space (the number is nevertheless the reference point of the "Two's complement" in an {{mvar|''N''}}-bit system). Because of this, systems with maximally {{mvar|''N''}}-bits must break the subtraction into two operations: first subtract from the maximum number in the {{mvar|''N''}}-bit system, that is {{math|2<sup>''N''</sup>β1}} (this term in binary is actually a simple number consisting of 'all 1s', and a subtraction from it can be done simply by inverting all bits in the number also known as [[Bitwise operation#NOT|the bitwise NOT operation]]) and then adding the one. Coincidentally, that intermediate number before adding the one is also used in computer science as another method of signed number representation and is called a [[ones' complement]] (named that because summing such a number with the original gives the 'all 1s'). Compared to other systems for representing signed numbers (e.g., [[ones' complement]]), the two's complement has the advantage that the fundamental arithmetic operations of [[addition]], [[subtraction]], and [[multiplication]] are identical to those for unsigned binary numbers (as long as the inputs are represented in the same number of bits as the output, and any [[integer overflow|overflow]] beyond those bits is discarded from the result). This property makes the system simpler to implement, especially for higher-precision arithmetic. Additionally, unlike ones' complement systems, two's complement has no representation for [[Signed zero|negative zero]], and thus does not suffer from its associated difficulties. Otherwise, both schemes have the desired property that the sign of integers can be reversed by taking the complement of its binary representation, but two's complement has an exception β the lowest negative, as can be seen in the tables.<ref>{{cite book |first1=David J. |last1=Lilja |first2=Sachin S. |last2=Sapatnekar |title=Designing Digital Computer Systems with Verilog |publisher=Cambridge University Press |date=2005 |url=https://books.google.com/books?id=5BvW0hYhxkQC&dq=%22two%27s+complement+arithmetic%22&pg=PA37 |isbn=9780521828666}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)