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!
==Why it works== Given a set of all possible {{mvar|N}}-bit values, we can assign the lower (by the binary value) half to be the integers from 0 to {{math|(2<sup>''N''βββ1</sup> β 1)}} inclusive and the upper half to be {{math|β2<sup>''N''βββ1</sup>}} to β1 inclusive. The upper half (again, by the binary value) can be used to represent negative integers from {{math|β2<sup>''N''βββ1</sup>}} to β1 because, under addition modulo {{math|2<sup>''N''</sup>}} they behave the same way as those negative integers. That is to say that, because {{math|1= ''i'' + ''j'' mod 2<sup>''N''</sup> = ''i'' + (''j'' + 2<sup>''N''</sup>) mod 2<sup>''N''</sup>}}, any value in the set {{math|1= {β''j'' + ''k''β2<sup>''N''</sup> {{!}} ''k'' is an integerβ}β}} can be used in place of {{mvar|j}}.<ref>{{cite web | url = http://www.cs.uwm.edu/~cs151/Bacon/Lecture/HTML/ch03s09.html | title = 3.9. Two's Complement | work = Chapter 3. Data Representation | date = 2012-12-03 | access-date = 2014-06-22 | publisher = cs.uwm.edu | archive-url=https://web.archive.org/web/20131031093811/http://www.cs.uwm.edu/~cs151/Bacon/Lecture/HTML/ch03s09.html | archive-date=31 October 2013 | url-status=dead }}</ref> For example, with eight bits, the unsigned bytes are 0 to 255. Subtracting 256 from the top half (128 to 255) yields the signed bytes β128 to β1. The relationship to two's complement is realised by noting that {{math|1=256 = 255 + 1}}, and {{math|(255 β ''x'')}} is the [[ones' complement]] of {{mvar|x}}. {|class="wikitable floatright" style="width:14em;" |+ Some special numbers to note !Decimal !Binary (8-bit) |- |style="text-align:right"| 127β||0111 1111 |- |style="text-align:right"| 64β||0100 0000 |- |style="text-align:right"| 1 β||0000 0001 |- |style="text-align:right"| 0 β||0000 0000 |- |style="text-align:right"| β1β||1111 1111 |- |style="text-align:right"| β64β||1100 0000 |- |style="text-align:right"| β127β||1000 0001 |- |style="text-align:right"| β128β||1000 0000 |} ===Example=== {{hatnote|In this subsection, decimal numbers are suffixed with a decimal point "."}} For example, an 8 bit number can only represent every integer from β128. to 127., inclusive, since {{math|(2<sup>8βββ1</sup> {{=}} 128.)}}. {{nowrap|β95. modulo 256.}} is equivalent to 161. since {{block indent|β95. + 256.}} {{block indent|{{=}} β95. + 255. + 1}} {{block indent|{{=}} 255. β 95. + 1}} {{block indent|{{=}} 160. + 1.}} {{block indent|{{=}} 161.}} <pre style="width:25em"> 1111 1111 255. β 0101 1111 β 95. =========== ===== 1010 0000 (ones' complement) 160. + 1 + 1 =========== ===== 1010 0001 (two's complement) 161. </pre> {|class="wikitable floatright" style="width:14em;text-align:center" |+ Two's complement 4 bit integer values !Two's complement !Decimal |- | 0111 ||style="text-align:right"| 7.β |- | 0110 ||style="text-align:right"| 6.β |- | 0101 ||style="text-align:right"| 5.β |- | 0100 ||style="text-align:right"| 4.β |- | 0011 ||style="text-align:right"| 3.β |- | 0010 ||style="text-align:right"| 2.β |- | 0001 ||style="text-align:right"| 1.β |- | 0000 ||style="text-align:right"| 0.β |- | 1111 ||style="text-align:right"| β1.β |- | 1110 ||style="text-align:right"| β2.β |- | 1101 ||style="text-align:right"| β3.β |- | 1100 ||style="text-align:right"| β4.β |- | 1011 ||style="text-align:right"| β5.β |- | 1010 ||style="text-align:right"| β6.β |- | 1001 ||style="text-align:right"| β7.β |- | 1000 ||style="text-align:right"| β8.β |} Fundamentally, the system represents negative integers by counting backward and [[modular arithmetic|wrapping around]]. The boundary between positive and negative numbers is arbitrary, but by [[Convention (norm)|convention]] all negative numbers have a left-most bit ([[most significant bit]]) of one. Therefore, the most positive four-bit number is 0111 (7.) and the most negative is 1000 (β8.). Because of the use of the left-most bit as the sign bit, the absolute value of the most negative number (|β8.| = 8.) is too large to represent. Negating a two's complement number is simple: Invert all the bits and add one to the result. For example, negating 1111, we get {{nowrap|0000 + 1 {{=}} 1}}. Therefore, 1111 in binary must represent β1 in decimal.<ref>{{cite web |first=Thomas |last=Finley |date=April 2000 |title=Two's Complement |series=Class notes for CS 104 |publisher=Cornell University |department=Computer Science |place=Ithaca, New York |url=http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html |access-date=2014-06-22 }}</ref> The system is useful in simplifying the implementation of arithmetic on computer hardware. Adding 0011 (3.) to 1111 (β1.) at first seems to give the incorrect answer of 10010. However, the hardware can simply ignore the left-most bit to give the correct answer of 0010 (2.). Overflow checks still must exist to catch operations such as summing 0100 and 0100. The system therefore allows addition of negative operands without a subtraction circuit or a circuit that detects the sign of a number. Moreover, that addition circuit can also perform subtraction by taking the two's complement of a number (see below), which only requires an additional cycle or its own adder circuit. To perform this, the circuit merely operates as if there were an extra left-most bit of 1.
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)