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!
==Most negative number== With only one exception, starting with any number in two's-complement representation, if all the bits are flipped and 1 added, the two's-complement representation of the negative of that number is obtained. Positive 12 becomes negative 12, positive 5 becomes negative 5, zero becomes zero(+overflow), etc. {|class="wikitable floatright" style="width:18em;text-align:center"|" |+ <span class="anchor" id="β128_example_anchor">The two's complement of {{math| β128 }}</span> |- | {{math| β128 }} || 1000 0000 |- | invert bits || 0111 1111 |- | add one || 1000 0000 |- |colspan="2;" |{{small|Result is the same 8 bit binary number.}} |} Taking the two's complement (negation) of the minimum number in the range will not have the desired effect of negating the number. For example, the two's complement of {{math| β128 }} in an eight-bit system is {{math| β128 ,}} as shown in the [[#β128_example_anchor|table to the right]]. Although the expected result from negating {{math| β128 }} is {{math| +128 ,}} there is no representation of {{math| +128 }} with an eight bit two's complement system and thus it is in fact impossible to represent the negation. Note that the two's complement being the same number is detected as an overflow condition since there was a carry into but not out of the most-significant bit. Having a nonzero number equal to its own negation is forced by the fact that zero is its own negation, and that the total number of numbers is even. Proof: there are {{math| 2^n β 1}} nonzero numbers (an odd number). Negation would partition the nonzero numbers into sets of size 2, but this would result in the set of nonzero numbers having even cardinality. So at least one of the sets has size 1, i.e., a nonzero number is its own negation. The presence of the most negative number can lead to unexpected programming bugs where the result has an unexpected sign, or leads to an unexpected overflow exception, or leads to completely strange behaviors. For example, * the unary negation operator may not change the sign of a nonzero number. e.g., {{math| β(β128) βΌ β128 }} (where "{{math|βΌ}}" is read as "becomes"). * an implementation of [[absolute value]] may return a negative number;<ref> {{cite web |title=Math |department=API specification |series=Java Platform SE 7 |url=http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html }} </ref> e.g., {{math| abs(β128) βΌ β128 .}} * Likewise, multiplication by {{math| β1 }} may fail to function as expected; e.g., {{math| (β128) Γ (β1) βΌ β128 .}} * Division by {{math| β1 }} may cause an exception (like that caused by dividing by {{math| 0 }});<ref> {{cite web |first=John |last=Regehr |year=2013 |title=Nobody expects the Spanish inquisition, or INT_MIN to be divided by β1 |website=Regehr.org |type=blog |url=https://blog.regehr.org/archives/887 }} </ref> even calculating the remainder (or [[modulo]]) by {{math| β1 }} can trigger this exception;<ref name=int32-c> {{cite web |first=Robert C. |last=Seacord |year=2020 |title=Ensure that operations on signed integers do not result in overflow |department=Rule INT32-C |series=SEI CERT C Coding Standard |website=wiki.sei.cmu.edu |url=https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow }} </ref> e.g., {{math| (β128) Γ· (β1) βΌ {{small|[{{sc|crash}}]}} ,}} {{math| (β128) % (β1) βΌ {{small|[{{sc|crash}}]}} .}} In the [[C (programming language)|C]] and [[C++]] programming languages, the above behaviours are [[Undefined behavior|undefined]] and not only may they return strange results, but the compiler is free to assume that the programmer has ensured that undefined numerical operations never happen, and make inferences from that assumption.<ref name=int32-c/> This enables a number of optimizations, but also leads to a number of strange bugs in programs with these undefined calculations. This most negative number in two's complement is sometimes called "the weird number", because it is the only exception.<ref> {{cite report |first1=Reynald |last1=Affeldt |first2=Nicolas |last2=Marti |name-list-style=amp |year=2006 |title=Formal verification of arithmetic functions in SmartMIPS Assembly |url=http://www.ipl.t.u-tokyo.ac.jp/jssst2006/papers/Affeldt.pdf |url-status=dead |archive-url=https://web.archive.org/web/20110722080531/http://www.ipl.t.u-tokyo.ac.jp/jssst2006/papers/Affeldt.pdf |archive-date=2011-07-22 }} </ref><ref> {{cite book |first1=David Money |last1=Harris |first2=Sarah L. |last2=Harris |year=2007 |title=Digital Design and Computer Architecture |page=18 |publisher=Morgan Kaufmann |isbn=978-0-08-054706-0 |url=https://books.google.com/books?id=5X7JV5-n0FIC&q=%22weird+number%22+binary&pg=PA19 |via=Google Books }} </ref> Although the number is an exception, it is a valid number in regular two's complement systems. All arithmetic operations work with it both as an operand and (unless there was an overflow) a result.
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)