Reputation: 35
How can I deal with the most negative number in two's complement representation? I will describe the problem in 1 byte representation, just to be easy. The most positive number is
01111111
which is 127 in decimal. Its two's complement is easy to calculate. Just reversing the bits we get
10000000
Then add the unit
10000001
which is -127 in decimal.
The zero number has the particular behaviour, since it two's complement to itself, which is well known. However, I had difficulties when I saw that the most negative number also two's complement to itself. The most negative is -128 which is
10000000
The complement is
01111111
And if we add the unit we get
10000000
which is of course a wrong result. Is this number never used or is it is never reached?
Upvotes: 2
Views: 1840
Reputation: 11
The answer is simple. The leftmost bit is for sign ( 1 for negative and 0 for positive). If we think of a normal positive integer and having that in mind, then we have only seven bits to store 1's:
01111111
Which is 127, or (2e7)-1.
With complements concept in mind, we have:
10000000
Which is -128.
For calculating the number of possibilities in a binary system, for positive numbers, we do have the next formula:
(2e(n-1))-1
And for negatives, we do have:
2e(n-1)
Both formulas follow from the statistical probabilities of only two options: 1's and 0's and the size of the bits used.
Another view is that, in the case of positives, we are to consider all the time from Zero (0) and on, meanwhile negatives from -1 and on, and not from zero.
Upvotes: 1
Reputation: 64904
It is used, but in some sense it is the "strangest number". Sometimes it is chosen not to use it, or to reserve it to indicate error conditions. Those are just conventions that are sometimes used, there is nothing inherently preventing the normal use of the most negative integer. In many cases, a number won't be negated, and then this special property just does not come up.
This strange property of two's complement integers means that in some scenarios where it is customary to take an absolute value and calculate with non-negative numbers, it makes sense to instead take the negative absolute value (which negates positive inputs, rather than negative inputs) and calculate with non-positive numbers. That helps because the normal absolute value has the quirk that the most negative integer has no positive counterpart, but there is no such quirk for positive numbers so they can all be safely negated. This trick is not always applicable.
-128 negating to itself is not always the wrong thing to happen. Despite the seemingly incorrect result, it is still the case (using the standard definition of addition) that x + (-x) = 0
for all values including -128, so it is algebraically sensible. It also interacts at least somewhat reasonably with taking the absolute value: while it is true that abs(x)
can yield a negative result this way (which is often undesirable), reinterpreting the result as an unsigned number of the same size makes the result correct, (unsigned 8 bit)abs(-128) = 128
.
The most negative integer is relatively frequently used in bit-manipulation code. Its arithmetic properties are less important there, making the number "less strange".
Upvotes: 1