For signed integers, that is, int type, it is related to the word length of the compiler. Only on 16-bit compiler, the int type accounts for 2 bytes, and its representation range is-32768 ~ 32767; For 32-bit and 64-bit compilers, the int type accounts for 4 bytes, representing the range-2147483648 ~ 2147483647.
Taking 16 bit compiler as an example, this paper introduces its principle, which is similar to 32-bit and 64-bit compilers.
Int is a signed integer, accounting for 2 bytes in 16-bit compiler, and 16-bit compiler.
For signed numbers, when expressed in a computer, the highest bit is the sign bit, which is positive when the sign bit is 0 and negative when the sign bit is 1.
So the part that really represents the value is the remaining 15 bits, which111111.
Convert to decimal, that is, 0~32767.
So the range of positive numbers is 1~32767.
There is a special value, which is the value of 0. When the sign bits are 1 and 0, the value is 0, so it is undoubtedly a waste to represent the same number in two forms. When the sign bit is 1, -32768 is represented by all zeros, which is why the range of negative numbers is more than that of positive numbers 1.
To sum up, for any bit, whether it is 8-bit, 16-bit, 32-bit or even 64-bit integer type, the calculation formula is:
If the total number of numbers is n, then the range of signed numbers is
-2^(n- 1)~ 2^(n- 1)- 1
The representation range of unsigned numbers is:
0~2^n- 1
In this formula, 0 stands for power. 2 n is the n power of 2.