For char type data, it is generally divided into unsigned and signed two types, here to illustrate the scope of the problem
The first is the unsigned char, which is simple,
0000 0000 ~ 1111 1111 range 0 ~ 255
What about the signed char??
First clear a thing, for the signed number, the highest bit is the sign bit,0 is positive, 1 is negative
So according to the same idea, the scope is:
1111 1111 ~ 0111 1111 I.E. 127 ~ 127
But a little to the microcomputer that book has been carefully read people know that the correct range is 128 ~ 127
This is the above situation ~ ~ ~ wrong??
Yes, it is wrong ~ ~
Here we forget a very important point of knowledge: negative numbers in a computer are stored in the form of a complement.
So the question is, why are negative numbers in the computer stored in the form of complement????
In fact, the simple answer is to facilitate CPU computing, simplify circuit design, improve computational efficiency ~ ~
As an example:-15
Binary: 1000 1111
Anti-code: 1111 0000
Complement: 1111 0001
stored in the compute set is 1111 0001
To give a few more examples:
Digital original code Reverse code complement
-1 1000 0001 1111 1110 1111 1111
-2 1000 0010 1111 1101 1111 1110
-3 1000 0011 1111 1100 1111 1101
...
-127 1111 1111 1000 0000 1000 0001
Have you found any rules?
Find the complement in descending order, then 128 on the basis of 127 minus one, get what???
-128 1000 0000 1111 1111 1000 0000
Maybe you'll find out how to make a 128来 out of thin air.
Good question!!!
Let's just take a look at this form online.
Digital original code Reverse code complement
+1 0000 0001 0000 0001 0000 0001
+00000 0000 0000 0000 0000 0000
-01000 0000 1111 1111 1000 0000
-1 1000 0001 1111 1110 1111 1111
-2 1000 0010 1111 1101 1111 1110
-3 1000 0011 1111 1100 1111 1101
...
-127 1111 1111 1000 0000 1000 0001
Watch the red part ~ ~
+0 and -0!!!
What's the difference? ... There's really no difference. They are equal in calculation, but why should there be two??
So many questions ...
Personal feeling this is probably the only small drawback of the computer using binary computing.
But the predecessors are also very clever, contrast will find that-128 and 0 of the complement is the same, which means 128 instead of -0.
The benefits of doing this are two points:
1: Eliminated-0 and +0 the repetition of this place, saving space consumption
2: Expanded the expression range of signed Char -128~127 altogether can represent 256, compared to -127~127 can be more than a number, why not? ~ ~ ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
On the derivation of the range of unsigned char for -128~127