我总是记不住电话号码。我需要一个记忆规则。


当前回答

永远不要忘记任何类型的最大值:

如果它有32位,最大的可能值将是带有数字1的32位:

结果将是十进制的4294967295:

但是,由于也有负数的表示,4294967295除以2,得到2147483647。

因此,一个32位整数能够表示-2147483647到2147483647

其他回答

假设。net -

Console.WriteLine(Int32.MaxValue);

最简单的方法是查看std::numeric_limits< int >::max()

例如(来自MSDN),

// numeric_limits_max.cpp

#include <iostream>
#include <limits>

using namespace std;

int main() {
   cout << "The maximum value for type float is:  "
        << numeric_limits<float>::max( )
        << endl;
   cout << "The maximum value for type double is:  "
        << numeric_limits<double>::max( )
        << endl;
   cout << "The maximum value for type int is:  "
        << numeric_limits<int>::max( )
        << endl;
   cout << "The maximum value for type short int is:  "
        << numeric_limits<short int>::max( )
        << endl;
}
max_signed_32_bit_num = 1 << 31 - 1;  // alternatively ~(1 << 31)

编译器无论如何都应该优化它。

我更喜欢1 << 31 - 1

0x7fffffff因为你不需要计数fs

Unsigned (pow(2,31)) - 1,因为你不需要<math.h>

请记住,2^(10*x)大约是10^(3*x) -您可能已经习惯了千字节/千字节等。那就是:

2^10 = 1024                ~= one thousand
2^20 = 1024^2 = 1048576    ~= one million
2^30 = 1024^3 = 1073741824 ~= one billion

由于int型使用31位(符号为+ ~1位),所以只需将2^30乘以2就可以得到大约20亿。对于使用32位的unsigned int,再次翻倍为40亿。当然,误差系数越大,但你不需要记住准确的值(如果你需要,你应该使用一个预定义的常量)。这个近似值足够好,可以用来注意到什么时候某样东西可能会危险地接近溢出。

它是2147483647。最简单的记忆方法就是纹身。