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!
== Procedure == The following is the procedure for obtaining the two's complement representation of a given ''negative'' number in binary digits: * Step 1: starting with the absolute binary representation of the number, with the leading bit being a sign bit;<ref>{{cite web|url=https://www.rit.edu/academicsuccesscenter/sites/rit.edu.academicsuccesscenter/files/documents/math-handouts/DM3_TwosComplement_BP_9_22_14.pdf|title=Two's Complement|website=University of Rochester Academic Success Center}}</ref> * Step 2: inverting (or flipping) all bits β changing every 0 to 1, and every 1 to 0; * Step 3: adding 1 to the entire inverted number, ignoring any [[Integer overflow|overflow]]. Accounting for overflow will produce the wrong value for the result. For example, to calculate the [[decimal]] number '''β6''' in binary from the number '''6''': * Step 1: ''+6'' in decimal is ''0110'' in binary; the leftmost significant bit (the first 0) is the [[Sign (mathematics)|sign]] (just 110 in binary would be β2 in decimal). * Step 2: flip all bits in ''0110'', giving ''1001''. * Step 3: add the place value 1 to the flipped number ''1001'', giving ''1010''. To verify that ''1010'' indeed has a value of ''β6'', add the place values together, but ''subtract'' the sign value from the final calculation. Because the most significant value is the sign value, it must be subtracted to produce the correct result: '''1010''' = '''β'''('''1'''Γ2<sup>3</sup>) + ('''0'''Γ2<sup>2</sup>) + ('''1'''Γ2<sup>1</sup>) + ('''0'''Γ2<sup>0</sup>) = '''1'''Γβ8 + '''0''' + '''1'''Γ2 + '''0''' = β6. {| class="wikitable" |Bits: |1 |0 |1 |0 |- |Decimal bit value: | '''β'''8 |4 |2 |1 |- |Binary calculation: |'''β'''('''1'''Γ2<sup>3</sup>) |('''0'''Γ2<sup>2</sup>) |('''1'''Γ2<sup>1</sup>) |('''0'''Γ2<sup>0</sup>) |- |Decimal calculation: |'''β'''('''1'''Γ8) |'''0''' |'''1'''Γ2 |'''0''' |} Note that steps 2 and 3 together are a valid method to compute the [[additive inverse]] <math>-n</math> of any (positive or negative) integer <math>n</math> where both input and output are in two's complement format. An alternative to compute <math>-n</math> is to use subtraction <math>0-n</math>. See below for subtraction of integers in two's complement format.
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)