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


当前回答

我是这样记得的…… 在十六进制中,一个数字代表4位,所以4 * 8 = 32,所以最大有符号的32位整型是:

0xFFFFFFFF >> 1 # => 2147483647

其他回答

我在c#中做了几个天才的方法,你可以在你的生产环境中利用:

public static int GetIntMaxValueGenius1()
{
    int n = 0;
    while (++n > 0) { }
    return --n;
}

public static int GetIntMaxValueGenius2()
{
    int n = 0;
    try
    {
        while (true)
            n = checked(n + 1);
    }
    catch { }
    return n;
}

在Objective-C (iOS和OSX)中,只需要记住这些宏:

#define INT8_MAX         127
#define INT16_MAX        32767
#define INT32_MAX        2147483647
#define INT64_MAX        9223372036854775807LL

#define UINT8_MAX         255
#define UINT16_MAX        65535
#define UINT32_MAX        4294967295U
#define UINT64_MAX        18446744073709551615ULL

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

2147483647

以下是你需要记住的:

是20亿。 接下来的三个三胞胎是这样增加的:100秒,400秒,600秒 第一个和最后一个三联体需要加3,这样它们就会四舍五入到50(例如147 + 3 = 150 & 647 + 3 = 650) 第二个三联数需要减去3才能四舍五入到80(例如483 - 3 = 480)

因此是2,147,483,647

无论如何,取这个正则表达式(它确定字符串是否包含一个十进制形式的非负整数,也不大于Int32.MaxValue)

[0-9] {1, 9} | [0, 1] [0-9] {1, 8} | 20 [0-9] {1, 8} | 21 [0, 3] [0-9] {1, 7} | 214 [0-6] [0-9] {1, 7} | 2147 [0, 3] [0-9] {1, 6} | 21474 [0] [0-9] {1, 5} | 21 4748 [2-0] [0-9] {1, 4} | 2147483 [0 to 5] [0-9] {1, 3} | 21474836 [0, 3] [0-9] {1, 2} | 214748364 [0]

也许能帮你记起来。