Question

Why is not the minimum value like this:

11111111 11111111 11111111 11111111

Please help me understand this.

Était-ce utile?

La solution 2

Let's use a 3-bit example to keep things simple.

The value 0 represented as a 3-bit binary number is 000.

If we subtract 1 from 0, we get -1. If we subtract 1 from 000, we get 111 with a borrow that "falls off" because we only have 3 bits.

If we continue to subtract 1, we get:

-1  111
-2  110
-3  101
-4  100

If we start back at 0 and add 1, we get the positive numbers:

+1  001
+2  010
+3  011

But when we try to add another 1, we get the representation for -4 instead of +4.

This is just like what would happen if you tried to wind back the odometer on your car. Once you reached 0, the next number would be 9999999 (or however many digits there are on an odometer), but this can naturally be thought of as a representation of -1. As you wind back even further, the number will move away from all 9's, but it would represent more negative values. We could say that if the leftmost digit is between 0 and 4, then the number is positive, and if it is between 5 and 9, then it is negative. As we continue to wind backwards, we will eventually reach 5000000, which is the most negative value. Winding back once more causes an overflow because we get 4999999, which we consider to be positive (the most positive value). Incidentally, this is called 10's complement, and was used by Charles Babbage to represent negative numbers in his difference engine.

Autres conseils

11111111 11111111 11111111 11111111
↑
MSB is 1 indicating that the number is negative

Is actually -1. Why?

To calculate the two's complement of a number, you flip all bits and add 1. Doing this to the number you proposed will give:

00000000 00000000 00000000 00000001

Which is 1, but the sign is negative. So you get -1.

You probably want to try:

10000000 00000000 00000000 00000000

If you calculate the above number you'll get:

01111111 11111111 11111111 11111111    flip
00000000 00000000 00000000 00000001 +  add 1
-----------------------------------
10000000 00000000 00000000 00000000

Which is indeed the minimal value.

Think of this in terms of the "place value" of each bit. For the sake of space, let's consider an 8-bit signed integer for a moment, which can have values between -128 and +127.

The place values of each bit in this integer are, from least to most significant:

  • +1 (LSB)
  • +2
  • +4
  • +8
  • +16
  • +32
  • +64 (MSB)
  • -128 (sign bit)

This means that the highest value is represented as 01111111 (+127), and the lowest value is represented as 10000000 (-128). 11111111 represents -1.

The same principle applies to 32-bit signed integers, just with bigger place values (specifically, -2147483648 for the sign bit).

In signed integers, the MSB represent the sign 1=-ve, 0=+ve.
So, the machine actually stores them in the range 0x00000000 - 0xffffffff
But since it is signed, it has to include both the positive as well as the negative nos, and hence the interpretation changes it to [-0x0fffffff + 1, 0x0fffffff] or [0x10000000,0x0fffffff] in signed language.

The range is evenly split - [-2^31,2^31-1]

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top