How Integral Values Are Represented
In an unsigned type, all the bits represent the value. If a type is defined for a particular machine to use 8 bits, then the unsigned version of this type could hold the values 0 through 255.
The C++(www.cppentry.com) standard does not define how signed types are represented at the bit level. Instead, each compiler is free to decide how it will represent signed types. These representations can affect the range of values that a signed type can hold. We are guaranteed that an 8-bit signed type will hold at least the values from –127 through 127; many implementations allow values from –128 through 127.
Under the most common strategy for representing signed integral types, we can view one of the bits as a sign bit. Whenever the sign bit is 1, the value is negative; when it is 0, the value is either 0 or a positive number. An 8-bit integral signed type represented using a sign-bit can hold values from –128 through 127.
整数和浮点数在计算机内的表示方法并不是本书的重点,读者可参考计算机组成原理或计算机体系结构一类的教材。在通常的硬件平台上,二进制整数用补码(two's complement)表示,浮点数的表示方法由IEEE 754 标准定义。二进制整数的表示与运算遵循相当简单的规律(从硬件实现的角度看),但是这个规律不一定符合直觉。例如,正负整数的表示范围是不对称的(因为有0),16-bitint 的表示范围通常是 32 768~32 767。这意味着 32 768 的绝对值超过了short int 的表示范围,if (s<0){ s = -s; } 并不总是能取到s的绝对值。
Assignment to Integral Types
The type of an object determines the values that the object can hold. This fact raises the question of what happens when one tries to assign a value outside the allowable range to an object of a given type. The answer depends on whether the type is signed or unsigned.
For unsigned types, the compiler must adjust the out-of-range value so that it will fit. The compiler does so by taking the remainder of the value modulo the number of distinct values the unsigned target type can hold. An object that is an 8-bit unsigned char, for example, can hold values from 0 through 255 inclusive. If we assign a value outside this range, the compiler actually assigns the remainder of the value modulo 256. For example, we might attempt to assign the value 336 to an 8-bit signed char. If we try to store 336 in our 8-bit unsigned char, the actual value assigned will be 80, because 80 is equal to 336 modulo 256.
For the unsigned types, a negative value is always out of range. An object of unsigned type may never hold a negative value. Some languages make it illegal to assign a negative value to an unsigned type, but C++(www.cppentry.com) does not. In C++(www.cppentry.com) it is perfectly legal to assign a negative number to an object with unsigned type. The result is the negative value modulo the size of the type. So, if we assign –1 to an 8-bit unsigned char, the resulting value will be 255, which is –1 modulo 256.
When assigning an out-of-range value to a signed type, it is up to the compiler to decide what value to assign. In practice, many compilers treat signed types similarly to how they are required to treat unsigned types. That is, they do the assignment as the remainder modulo the size of the type. However, we are not guaranteed that the compiler will do so for the signed types.
写程序时一般应该在“安全范围”内使用整数,避免溢出(整数的加减乘除运算都有溢出的可能)。在极少数情况下,溢出是意料中的,比如算hash 值时。