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


当前回答

试试用Python:

>>> int('1' * 31, base=2)
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;
}

在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)。

首先写出两次47(你喜欢47号特工,对吧?),保持如图所示的空格(每个破折号是一个数字的槽位。先2个,然后4个)

--47----47

认为你手里有12个(因为12 =一打)。将其乘以4,47号特工号的第一位数字,即47,并将结果放在你已经拥有的第一对的右边

12 * 4 = 48
--4748--47 <-- after placing 48 to the right of first 47

然后将12乘以3(为了得到47号特工的数字的第二个数字,即7,你需要7 - 4 = 3),并将结果放在前两对的右边,即最后一个对槽

12 * 3 = 36
--47483647 <-- after placing 36 to the right of first two pairs

最后,从最右边的数字(本例中为2)开始,一个接一个地从您的手中拖动数字,并将它们放在您获得的第一个空槽中

2-47483647 <-- after placing 2
2147483647 <-- after placing 1

你知道了!对于负极限,你可以认为它的绝对值比正极限大1。

练习几次,你就会掌握窍门的!

假设。net -

Console.WriteLine(Int32.MaxValue);
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