This is a problem that has puzzled me for several years. It has left me extremely dissatisfied with the current textbooks and teachers. Since I started to touch my computer N years ago, in almost every C ++ textbook, the 8-Bit Signed value range is-128 ~ + 127, why not-127 ~ + 127 what about the clustering value range of later Java and INT, then 32-bit calculation,-2 ^ 31 ~ + 2 ^ 31-1, but no textbook or teacher has ever explained this question before.
The reason is not met at work or somewhere, so I keep ignoring it, but there is always a thorn in my heart !!!!
Just now, I was bored. I met it again when I was reading the compiled book. But as before, the book directly and intentionally bypasses this problem. It's really awful.
After several twists and turns, I finally figured it out:
In fact, it is determined by the bottom layer of the computer to realize numerical computation. It involves a very basic source code, anti-code, and complement knowledge. Generally (99.9999%) it will not be used.
That's 0.0001%, probably the computer test,
Statement:
If we use 2 ^ 8 to represent an unsigned integer, the world's understanding is 0-255. So what about the signed? Use the highest bit to represent the symbol. If 0 is + and 1 is-, the normal understanding is-127 to + 127.
This is the original code. It is worth mentioning that the weakness of the original code is that there are 2 0, that is, + 0 and-0, and when the difference sign is added or the same number is subtracted, dumb: first determine the absolute value of two numbers, and then perform addition and subtraction. Finally, the symbol of the calculation result must be the same as that of a large number.
As a result, the reverse code is generated. The cause is... slightly. Anyway, it didn't take long before the reverse Code became a filter product, that is, the complement code appeared.
I will not describe the supplemental knowledge, but only about + 127 and-128.
The official definition [-2 ^ (n-1), 2 (n-1)-1]. There is no positive or negative score for 0 of the complement code.
Why? No book says that this is why I am so angry, but through thinking, Google, and thinking, I quickly find the answer:
First of all, it's hard to do anything as an idiot. Just give it a try...
Positive number. The original code is the same as the complement code.
+ 127,011 1 1111
+ 126,011 1 1110
+ 125,011 1 1101
+ 124,011 1 1100
+ 123,011 1 1011
+ 122,011 1 1010
...
+ 4, 0000 0100
+ 3, 0000 0011
+ 2, 0000 0010
+ 1, 0000 0001
0, 0000 0000 (no positive or negative)
Below is a negative number, value, original code, the symbol bit remains unchanged, and the other is reversed, + 1
-1, 1000 0001,111 1 1110,111 1111
-2, 1000 0010,111 1 1101,111 1 1110
-3, 1000 0011,111 1 1100,111 1101
-4, 1000 0100,111 1 1011,111 1 1100
-5, 1000 0101,111 1 1010,111 1 1011
-6, 1000 0110,111 1 1001,111 1 1010
-7, 1000 0111,111 1 1000,111 1 1001
-8, 1000 1000,111 1 0111,111 1 1000
-9, 1000 1001,111 1 0110,111 1 0111
-10,100 0 1010,111 1 0101,111 0110
-11,100 0 1011,111 1 0100,111 0101
-12,100 0 1100,111 1 0011,111 0100
-13,100 0 1101,111 1 0010,111 0011
-14,100 0 1110,111 1 0001,111 0010
-15,100 0 1111,111 1 0000,111 0001
-16,100 1 0000,111 0 1111,111 1 0000
-17,100 1 0001,111 0 1110,111 0 1111
...
-24,100 1 1000,111 0 0111,111 0 1000
...
-99,111 0 0011,100 1 1100,111 0 0100
...
-124,111 1 1100,100 0 0011,111 1 1101
-125,111 1 1101,100 0 0010,100 0 0011
-126,111 1 1110,100 0 0001,100 0 0010
-127,111 1 1111,100 0 0000,100 0 0001
What do you see?
If not, give a prompt and continue. What is the next complement?
Of course yes
-128, skipped first, then skipped, 1000 0000
1000 0000, so what is the original code of it?
The method for obtaining the source code from the source code is the same as that for the source code.
First, retain the symbol bit. Other reverse expressions: 1111 1111, plus 0000, exceeding 8 digits.
Yes, it cannot be expressed here with an 8-digit original code.
The key is here. There is no need to suspect that the Supplementary Code 1000 0000 is-128 (the above example is poor ),
Then, return to the original code, and its original code is also 1000 0000 (the excess is automatically lost ),
What does 1000 0000 indicate in the original code? -0, but the complement Code specifies that 0 has no positive or negative points.
Let's take a look at how computation is performed in the computer:
For negative numbers, first take the absolute value, then reverse, and add one
-128-> 128-> 1000 0000-> 0111 1111-> 1000 0000
Now let's make it clear.
Therefore, the complement expression of the 8-bit signed integer value range
1000 0000 to 0000 0000, and then to 0111 1111
That is,-128 to 0, and then to 127
End-128 ~ + 127
Thanks to Google, and to all the blogs that have been browsed, closed, and forgotten their names, csdn (there are some nice things above)
Over ~