我总是记不住那个数字,我需要一个记忆规则。
记忆这些规则的最佳方法是:
21 (神奇数字!)
47 (只需记住)
48 (顺序! )
36 (21 + 15,两个神奇数字!)
再次47
此外,记住5对比记住10个数字更容易。
最简单的方法是看 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;
}
Iwannagohome
比 298347829
更容易记忆。然而,没有理由给它打-1。 - glglglin
+.
+ma
+回车。 - Michael请记住,2^(10*x)大约等于10^(3*x)——你可能已经在使用千字节/千字节等单位时习惯了这种情况。也就是说:
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 位的无符号 int,再次倍增可得到 40 亿。当然,随着数字越来越大,误差因素会变得更高,但你不需要记住精确值(如果需要,应该使用预定义的常量)。近似值足以发现某些可能接近溢出的情况。
这是我记住2,147,483,647的方法
2 - To
1 - A
4 - Far
7 - Savannah
4 - Quarter
8 - Optimus
3 - Trio
6 - Hexed
4 - Forty
7 - Septenary
#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
Int32表示你有32位可用于存储数字。最高位是符号位,这表示数字是正数还是负数。因此,你有2^31位用于正数和负数。
零被视为正数,因此你得到的逻辑范围是:
+2147483647到-2147483648
如果你认为这太小了,可以使用Int64:
+9223372036854775807到-9223372036854775808
你为什么要记住这个数字?用于代码中吗?在代码中,你应该始终使用Int32.MaxValue或Int32.MinValue,因为它们是静态值(在.net core中),因此比使用代码创建新整数更快。
我的说法是:如果你能记住这个数字,那只是在炫耀!