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


当前回答

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

其他回答

Int32意味着你有32位可用来存储你的数字。最高位是符号位,这表示数字是正还是负。所以正数和负数都有2^31位。

如果0是正数,则得到(前面提到过)的逻辑范围

+2147483647到-2147483648

如果你认为这太小了,请使用Int64:

+9223372036854775807 至 -9223372036854775808

你为什么要记住这个号码?在代码中使用?您应该始终使用Int32。MaxValue或Int32。因为这些是静态值(在.net核心中),因此使用起来比用代码创建一个新的int更快。

我的陈述:如果能记住这个数字。你这是在炫耀!

无论如何,取这个正则表达式(它确定字符串是否包含一个十进制形式的非负整数,也不大于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]

也许能帮你记起来。

我在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;
}

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

0xFFFFFFFF >> 1 # => 2147483647

你会发现在二进制中Int32的最大值是11111111111111111111111111111111111111111 但以10为基数,你会发现它是2147483647或2^31-1或Int32。MaxValue