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


当前回答

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

其他回答

这里有一个记忆2**31,减去1得到最大整数值的助记符。

a = 1, b = 2, c = 3 d = 4 = 5, f = 6 g = 7, 8 h = = 9

Boys And Dogs Go Duck Hunting, Come Friday Ducks Hide
2    1   4    7  4    8        3    6      4     8

我经常使用2到18的幂来记住它们,但即使是我也没有费心去记住2**31。根据需要计算或使用常数,或估计为2G太容易了。

max_signed_32_bit_num = 1 << 31 - 1;  // alternatively ~(1 << 31)

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

我更喜欢1 << 31 - 1

0x7fffffff因为你不需要计数fs

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

2^(x+y) = 2^x * 2^y

2^10 ~ 1,000
2^20 ~ 1,000,000
2^30 ~ 1,000,000,000
2^40 ~ 1,000,000,000,000
(etc.)

2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
2^8 = 256
2^9 = 512

2^31 (signed int max)等于2^30(约10亿)乘以2^1(2)也就是20亿。2^32等于2^30 * 2^2,大约是40亿。这种近似方法甚至可以精确到2^64左右(误差增长到15%左右)。

如果你需要一个确切的答案,那么你应该打开计算器。

方便的字对齐容量近似:

2^16 ~= 64千// uint16 2^32 ~= 40亿// uint32, IPv4, unixtime 2^64 ~= 16 quintillion(又名160亿billion或1600万trillion) // uint64, "bigint" 2^128 ~= 256quintillion quintillion(又名256trillion trillion万亿)// IPv6, GUID

如果你碰巧记住了你的ASCII表而不是MaxInt: gh6g = 21 47 48 36 47

它是10位数字,所以假装它是一个电话号码(假设你在美国)。214-748-3647。我不建议调用它。