I understand and summarize the calculation of the Data Type value range in C language, and the Data Type Value
First of all, let's talk a little off. This is my first blog. I am a little white. I write a blog to help me understand my knowledge and problems more deeply, I often sum up my learning habits and Methods. At the same time, I can write down my understanding of some problems. I also fantasized that some experts can see and point out my problems !!!
In C, the data types include short, int, long, char, float, and double. Besides the floating point type, only the signed number is supported, other data types include signed and unsigned ). These data types all have a value range. Next I will use the char type to express my understanding of the calculation of the value range of the data type.
As we all know, the char type is 1 byte = 8 binary bits. The value range is-2 ^ 7 ~ 2 ^ 7-1; how can this value range be calculated? First, let's talk about the highest bit of a variable in the computer, indicating that 0 represents an integer, 1 indicates a negative number. The remaining values are the values of this variable. Therefore, in char type, the maximum positive integer is 0111 1111 (2 ^ 7-1: 0111 1111 can be considered as 1000 bytes-1 ). The smallest negative integer is 1111 1111 (-(2 ^ 7-1 )). Success! Didn't we say the smallest one is-2 ^ 7?
When we use a binary original code: the value range is 1111 1111 (-(2 ^ 7-1 ))~ 1000 0000 (-0) and 0000 0000 (+ 0 )~ 0111 1111 (2 ^ 7-1), so we can see from here that the encoding is more than the actual number: 1000 0000 and 0000 0000 indicate the number of 0.
However, in a computer, data is stored in the form of complement code, that is, the positive number encoding remains unchanged, that is, the positive number range is still 0000 0000 (+ 0 )~ 0111 1111 (2 ^ 7-1), while the negative number requires the symbol bit to remain unchanged, the other bits are reversed, and the negative value is added with 1. So let's look at 1000 0000 (-0 ), the complement code is 0000 0000. Okay. Here, we can see that when a computer stores a variable in the form of a complement code, whether it is + 0 or-0, its encoding is changed to 0000 0000, therefore, an additional code of 1000 0000 is provided. However, since no source code can be converted into a complement code of 1000 0000, 1000 0000 is artificially set to-2 ^ 7.
Therefore, the char value range is-2 ^ 7 ~ 2 ^ 7-1;