1. Brief Introduction
The range of Integer type was asked two days ago, and it was definitely despised. It looks good to come back. Here is a summary.
2. unsigned integer
On a 32-bit general machine, unsigned short int (2 bytes), unsigned int (4 bytes), unsigned long int (or 32 bytes, because int Is two bytes on some previous machines and short is one byte, 4 bytes are called long)
Taking unsinged int as an example, the data size is [0, 2 ^ 32-1] with four bytes and 32 bits. that is, the length of the data range that can be expressed by 32 digits is 2 ^ 32.
For the char type, the range is [0, 2 ^ 8-1].
3. signed integer
The unsigned integer range is simply [0, 2 ^ N-1] Where N = the bit of the data. But it is not that simple for the number of symbols, because the storage of the number of symbols introduces the concept of the original code, reverse code and complement code.
These concepts are introduced to simplify the negative number operation, that is, the negative number is actually stored in the form of a complement code. When adding a negative number to a positive number, you can directly add the binary values on two bits. Why can we add them directly? This is not the focus of the discussion. Here we will focus on the introduction of complement codes and the impact on the storage range of signed numbers.
First, assume that there is no so-called complement code. Both integer and negative numbers represent the original code, that is, except the symbol bit, other bits represent the data, which is similar to the unsigned number.
For example: When the sign bit = 0, indicates a positive number, range [0, 2 ^ (N-1)-1]
When the sign bit is 1, it indicates a negative number, range [-2 ^ (N-1)-1, 0]
This introduces a problem, two 0, that is, a sign bit is a positive number of 0 and the number of negative 0, indicating that the length of the data range is 2 ^ N-1. In fact, after the supplementary code is introduced, the data range is 2 ^ N, where "negative 0" is the smallest integer.
For convenience, let's assume that the data length is 8 bits. For 32 bits of int, and so on.
For positive numbers, it is the same as the unsigned number, that is, values 0, 1,127, and indicate 0 and respectively.
For negative numbers, it is calculated based on the complement code, that is, the complement code of ,0000 1000,0001 1111,1111, And the complement code of a negative number = the original code of a negative number is reversed + 1. Therefore, we require that the original code be subtracted first and then reversed
First subtract one 0111,1111 ,0000 1111,1110
Return the values of 1000,000 0 0111,1111 random and (this is the absolute value of a negative number)
-128-127-1 I .e. [-2 ^ (N-1),-1]
So for signed integers, the range is [-2 ^ (N-1), 2 ^ (N-1)-1]
It is very important that for N-bit data, the range length is 2 ^ N, there is no positive number 0 and negative number 0, and the length of negative number is one more than that of positive number.
4. References
Int type value range http://blog.csdn.net/zabcd117/archive/2007/07/12/1687413.aspx
Original code, reverse code and http://www.cnitblog.com/mantou/archive/2005/08/01/1239.aspx in Computer
Baidu encyclopedia http://baike.baidu.com/view/377340.htm