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


当前回答

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

其他回答

大约是2.1 * 10^9。不需要知道确切的2^{31}- 1 = 2,147,483,647。

C

你可以在C语言中找到它:

#include <stdio.h>
#include <limits.h>

main() {
    printf("max int:\t\t%i\n", INT_MAX);
    printf("max unsigned int:\t%u\n", UINT_MAX);
}

给出(好吧,没有,)

max int:          2,147,483,647
max unsigned int: 4,294,967,295

C + 11 +

std::cout << std::numeric_limits<int>::max() << "\n";
std::cout << std::numeric_limits<unsigned int>::max() << "\n";

Java

你也可以用Java得到这个:

System.out.println(Integer.MAX_VALUE);

但是请记住,Java整数总是有符号的。

Python 2

Python有任意的精确整数。但在python2中,它们被映射为C整数。所以你可以这样做:

import sys
sys.maxint
>>> 2147483647
sys.maxint + 1
>>> 2147483648L

所以当整数大于2^31 -1时,Python会切换为long

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

“如果一个巨大的整数没有被回忆起,你就回忆这个助记符。”

现在数数每个单词中的字母。

一般来说,你可以做一个简单的操作,它反映了Int32的本质,用1填充所有可用的位-这是你可以很容易地保存在你的内存中的东西。它在大多数语言中的工作方式基本相同,但我以Python为例:

max = 0
bits = [1] * 31 # Generate a "bit array" filled with 1's
for bit in bits:
    max = (max << 1) | bit
# max is now 2147483647

对于unsigned Int32,将其设置为32而不是31个1。

但因为有一些更冒险的方法,我开始考虑公式,只是为了好玩…

公式1(如果没有给出运算符,则将数字连在一起)

a = 4 b = 8 巴/ a ab-1 接 ab-a-b ab-1

Python quickcheck

a = 4
b = 8
ab = int('%d%d' % (a, b))
ba = int('%d%d' % (b, a))
'%d%d%d%d%d' % (ba/a, ab-1, ab, ab-a-b, ab-1)
# gives '2147483647'

公式2

X = 48 x / 2 - 3 x - 1 x x * 3/4 x - 1

Python quickcheck

x = 48
'%d%d%d%d%d' % (x/2-3, x-1, x, x*3/4, x-1) 
# gives '2147483647'

最大负值(32位):-2147483648 (1 << 31)

最大正(32位)值:2147483647 ~(1 << 31)

记忆:“醉酒又好色”

drunk ========= Drinking age is 21
AK ============ AK 47
A ============= 4 (A and 4 look the same)
horny ========= internet rule 34 (if it exists, there's 18+ material of it) 

21 47 4(years) 3(years) 4(years)
21 47 48       36       48