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


当前回答

这就是我记住2147483647的方法

在遥远的大草原上,擎天柱三人对着四十个七分咒

2 - To
1 - A
4 - Far
7 - Savannah
4 - Quarter
8 - Optimus
3 - Trio
6 - Hexed
4 - Forty
7 - Septenary

其他回答

请记住,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亿。当然,误差系数越大,但你不需要记住准确的值(如果你需要,你应该使用一个预定义的常量)。这个近似值足够好,可以用来注意到什么时候某样东西可能会危险地接近溢出。

我能想到的最正确的答案是Int32.MaxValue。

在C语言中,在#include <stdint.h>后使用INT32_MAX。 在c++中,在#include <cstdint>后使用INT32_MAX。

或INT_MAX平台特定的大小或UINT32_MAX或UINT_MAX unsigned int。参见http://www.cplusplus.com/reference/cstdint/和http://www.cplusplus.com/reference/climits/。

或运算符(int)。

最简单的方法是查看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;
}

在路径上使用Groovy:

groovy -e " println Integer.MAX_VALUE "

(在Java上下文中,Groovy对于快速参考非常有用。)